View Javadoc

1   /*
2    * This file is a part of CAST project.
3    * (c) Copyright 2007, AGH University of Science & Technology
4    * https://caribou.iisg.agh.edu.pl/trac/cast
5    *
6    * Licensed under the Eclipse Public License, Version 1.0 (the "License").
7    * You may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    * http://www.eclipse.org/legal/epl-v10.html
10   */
11  /*
12   * File: SerializationUtil.java
13   * Created: 2007-00-00
14   * Author: klewandowski, awos
15   * $Id: SerializationUtil.java 3020 2009-07-17 14:41:19Z kpietak $
16   */
17  
18  package pl.edu.agh.cast.backward.resources;
19  
20  import java.io.BufferedInputStream;
21  import java.io.BufferedOutputStream;
22  import java.io.File;
23  import java.io.FileInputStream;
24  import java.io.FileOutputStream;
25  import java.io.IOException;
26  import java.io.ObjectInputStream;
27  import java.io.ObjectOutputStream;
28  
29  import org.apache.log4j.Logger;
30  import org.eclipse.core.resources.IFile;
31  import org.eclipse.core.resources.IResource;
32  import org.eclipse.core.runtime.CoreException;
33  
34  import pl.edu.agh.cast.Activator;
35  import pl.edu.agh.cast.model.visual.backward.IDiagram;
36  import pl.edu.agh.cast.model.visual.backward.IDiagramSettings;
37  
38  /**
39   * Utility class for model serialization.
40   *
41   * @author AGH CAST Team
42   */
43  public final class SerializationUtil {
44  
45  	private static final Logger LOG = Activator.getLogger();
46  
47  	/**
48  	 * Reads only diagram settings from a file with serialized diagram.
49  	 *
50  	 * @param file
51  	 *            diagram file to read from
52  	 * @return diagram settings read, or null in case of an exception
53  	 */
54  	public static IDiagramSettings readDiagramSettings(IFile file) {
55  		ObjectInputStream ois = null;
56  		try {
57  			ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(new File(file.getLocationURI()))));
58  			IDiagramSettings settings = (IDiagramSettings)ois.readObject();
59  			ois.close();
60  			ois = null;
61  			return settings;
62  		} catch (Exception e) {
63  			LOG.error("Error occured while reading diagram settings from file " //$NON-NLS-1$
64  			        + file.getName(), e);
65  		} finally {
66  			closeDiagramInput(file, ois);
67  		}
68  		return null;
69  	}
70  
71  	/**
72  	 * Closes diagram input file after reading.
73  	 *
74  	 * @param file
75  	 * @param ois
76  	 */
77  	private static void closeDiagramInput(IFile file, ObjectInputStream ois) {
78  		if (ois != null) {
79  			try {
80  				ois.close();
81  			} catch (IOException e) {
82  				LOG.error("Error occured while closing diagram file after reading " //$NON-NLS-1$
83  				        + file.getName(), e);
84  			}
85  		}
86  	}
87  
88  	/**
89  	 * Reads the whole diagram from a serialized file.
90  	 *
91  	 * @param file
92  	 *            diagram file to read from
93  	 * @return the diagram, or null in case of an exception
94  	 */
95  	public static IDiagram readDiagram(IFile file) {
96  		ObjectInputStream ois = null;
97  		try {
98  			ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(new File(file.getLocationURI()))));
99  			/*
100 			 * read and ignore IDiagramSettings - they're saved at the beginning and inside the diagram
101 			 */
102 			ois.readObject();
103 			// read diagram
104 			IDiagram diagram = (IDiagram)ois.readObject();
105 			ois.close();
106 			ois = null;
107 			return diagram;
108 		} catch (Exception e) {
109 			LOG.error("Error occured while reading diagram " //$NON-NLS-1$
110 			        + file.getName(), e);
111 		} finally {
112 			closeDiagramInput(file, ois);
113 		}
114 		return null;
115 	}
116 
117 	/**
118 	 * Serializes given model.
119 	 *
120 	 * @param model
121 	 *            model to serialize
122 	 * @throws IOException
123 	 */
124 	public static void serializeToFile(final IDiagram model) throws IOException {
125 		serializeDiagram(model);
126 	}
127 
128 	/**
129 	 * Serializes diagram from given {@link DiagramEditorInput}.
130 	 *
131 	 * @param input
132 	 *            diagram editor input to serialize from
133 	 * @throws IOException
134 	 */
135 	public static void serializeToFile(DiagramEditorInput input) throws IOException {
136 		input.getDiagram().setFile(input.getFile());
137 		serializeDiagram(input.getDiagram());
138 	}
139 
140 	/**
141 	 * Serializes saved models to file.
142 	 *
143 	 * @param procjet
144 	 *            Project to serialize.
145 	 */
146 	private static void serializeDiagram(final IDiagram diagram) throws IOException {
147 
148 		File f = new File(diagram.getFile().getLocationURI());
149 		f.delete();
150 
151 		ObjectOutputStream oos = null;
152 		try {
153 			oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(f)));
154 			oos.writeObject(diagram.getSettings());
155 			oos.writeObject(diagram);
156 			oos.close();
157 			oos = null;
158 		} catch (Exception e) {
159 			LOG.error("Error occured while writing diagram to file " //$NON-NLS-1$
160 			        + f.getName(), e);
161 		} finally {
162 			if (oos != null) {
163 				try {
164 					oos.close();
165 				} catch (IOException e) {
166 					LOG.error("Error occured while closing diagram file after writing " //$NON-NLS-1$
167 					        + f.getName(), e);
168 				}
169 			}
170 		}
171 
172 		try {
173 			diagram.getFile().refreshLocal(IResource.DEPTH_ZERO, null);
174 		} catch (CoreException e) {
175 			LOG.error("Error refreshing file " + f.getName(), e); //$NON-NLS-1$
176 		}
177 	}
178 
179 }