1 | /** |
2 | * |
3 | */ |
4 | package de.uka.ipd.sdq.sensorframework.dao.file; |
5 | |
6 | import java.io.File; |
7 | import java.io.FileInputStream; |
8 | import java.io.FileOutputStream; |
9 | import java.io.IOException; |
10 | import java.io.InputStream; |
11 | import java.io.ObjectInputStream; |
12 | import java.io.ObjectOutputStream; |
13 | import java.io.OutputStream; |
14 | import java.io.Serializable; |
15 | import java.util.ArrayList; |
16 | |
17 | import de.uka.ipd.sdq.sensorframework.entities.dao.IDAOFactory; |
18 | import de.uka.ipd.sdq.sensorframework.storage.lists.BackgroundMemoryList; |
19 | |
20 | /** |
21 | * @author Ihssane El-Oudghiri |
22 | * @author Steffen Becker |
23 | * |
24 | * This class provides methods to deal with files. Especially to serialize and |
25 | * deserialize an object to a file. |
26 | */ |
27 | public class FileManager { |
28 | |
29 | /** |
30 | * Directory in which this file provider stores its data |
31 | */ |
32 | private String rootDirectory; |
33 | |
34 | /** |
35 | * The parent DAOFactory which stores its data using this class |
36 | */ |
37 | private FileDAOFactory factory; |
38 | |
39 | /** |
40 | * A registry of open BackgroundMemoryLists. Used to finally close all open lists on closing this |
41 | * file provider |
42 | */ |
43 | private ArrayList<BackgroundMemoryList<?>> openLists = new ArrayList<BackgroundMemoryList<?>>(); |
44 | |
45 | public FileManager(String rootDirectory, FileDAOFactory factory) { |
46 | checkPath(rootDirectory); |
47 | this.rootDirectory = rootDirectory; |
48 | this.factory = factory; |
49 | } |
50 | |
51 | /** Test whether the given path exists and is a directory |
52 | * @param path The path to test |
53 | */ |
54 | private void checkPath(String path) { |
55 | File f = new File(path); |
56 | if (!f.isDirectory()) |
57 | throw new IllegalArgumentException("Error: " + path |
58 | + " is not a directory!"); |
59 | } |
60 | |
61 | /** Delete the given file |
62 | * @param filename Name of the file to delete |
63 | * @return true if deletion was successful |
64 | */ |
65 | public boolean removeFile(String filename) { |
66 | File path = new File(new File(this.rootDirectory), filename |
67 | .endsWith(".ser") ? filename : filename + ".ser"); |
68 | return path.delete(); |
69 | } |
70 | |
71 | /** Write the given serializable into the given file using Java's object serialisation mechanism |
72 | * @param filename Name of the file in which the data is written |
73 | * @param ser The object to persist |
74 | */ |
75 | public void serializeToFile(String filename, Serializable ser) { |
76 | OutputStream fos = null; |
77 | File path = new File(new File(this.rootDirectory), filename |
78 | + FileDAOFactory.SUFFIX); |
79 | try { |
80 | fos = new FileOutputStream(path); |
81 | ObjectOutputStream o = new ObjectOutputStream(fos); |
82 | o.writeObject(ser); |
83 | } catch (IOException e) { |
84 | throw new RuntimeException("Serialisation of DAO failed.", e); |
85 | } finally { |
86 | try { |
87 | fos.close(); |
88 | } catch (Exception e) { |
89 | throw new RuntimeException("Serialisation of DAO failed.", e); |
90 | } |
91 | } |
92 | } |
93 | |
94 | /** Read the object stored in the given file |
95 | * @param fileName The file from which to read the data |
96 | * @return The object persisted in the given file |
97 | */ |
98 | public Serializable deserializeFromFile(String fileName) { |
99 | File path = new File(new File(this.rootDirectory), fileName + FileDAOFactory.SUFFIX); |
100 | return deserializeFromFile(path); |
101 | } |
102 | |
103 | /** Read the object stored in the given file |
104 | * @param file The file from which to read the data |
105 | * @return The object persisted in the given file |
106 | */ |
107 | public Serializable deserializeFromFile(File file) { |
108 | Serializable result = null; |
109 | InputStream fis = null; |
110 | if (file.exists()) { |
111 | try { |
112 | fis = new FileInputStream(file); |
113 | ObjectInputStream o = new ObjectInputStream(fis); |
114 | result = (Serializable) o.readObject(); |
115 | } catch (IOException e) { |
116 | throw new RuntimeException("Sensorframework File Provider failed loading an entity",e); |
117 | } catch (ClassNotFoundException e) { |
118 | throw new RuntimeException("Sensorframework File Provider failed loading an entity",e); |
119 | } finally { |
120 | try { |
121 | fis.close(); |
122 | } catch (Exception e) { |
123 | throw new RuntimeException("Sensorframework File Provider failed loading an entity",e); |
124 | } |
125 | } |
126 | } |
127 | return result; |
128 | } |
129 | |
130 | public String getRootDirectory() { |
131 | return rootDirectory; |
132 | } |
133 | |
134 | /** Add a new BackgroundMemoryList to this class's registry of open lists |
135 | * @param list The list to add to the registry for closing on termination |
136 | */ |
137 | public void addOpenList(BackgroundMemoryList<?> list) { |
138 | openLists.add(list); |
139 | } |
140 | |
141 | /** |
142 | * Closes all lists registered in this class' registry of open background lists |
143 | */ |
144 | public void closeAllLists() { |
145 | for (BackgroundMemoryList<?> list : openLists) { |
146 | try { |
147 | list.close(); |
148 | } catch (IOException e) { |
149 | } |
150 | } |
151 | openLists.clear(); |
152 | } |
153 | |
154 | public IDAOFactory getDAOFactory() { |
155 | return this.factory; |
156 | } |
157 | |
158 | /** |
159 | * Flushes all open lists so that they use only little memory. |
160 | */ |
161 | public void flush(){ |
162 | for (BackgroundMemoryList<?> list : openLists) { |
163 | try { |
164 | list.flush(); |
165 | } catch (IOException e) { |
166 | } |
167 | } |
168 | } |
169 | |
170 | } |