1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.testdoc.core.adapter;
17
18 import java.io.BufferedInputStream;
19 import java.io.BufferedOutputStream;
20 import java.io.File;
21 import java.io.FileInputStream;
22 import java.io.FileNotFoundException;
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.ObjectInputStream;
27 import java.io.ObjectOutputStream;
28 import java.io.OutputStream;
29
30 import javax.tools.FileObject;
31
32 import org.apache.commons.io.IOUtils;
33
34 import de.smartics.testdoc.core.doc.UnitTestDoc;
35
36
37
38
39
40
41
42
43 public final class PersistenceUtils
44 {
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72 public void serialize(final Object instance, final OutputStream out)
73 throws IllegalStateException
74 {
75 ObjectOutputStream output = null;
76 try
77 {
78 output = new ObjectOutputStream(new BufferedOutputStream(out));
79 output.writeObject(instance);
80 }
81 catch (final IOException e)
82 {
83 throw new IllegalStateException("Cannot persist instance of type '"
84 + instance.getClass().getName() + "'.", e);
85 }
86 finally
87 {
88 IOUtils.closeQuietly(output);
89 }
90 }
91
92
93
94
95
96
97
98
99 public void serialize(final Object instance, final FileObject file)
100 throws IllegalStateException
101 {
102 try
103 {
104 removeFile(file);
105 serialize(instance, file.openOutputStream());
106 }
107 catch (final IOException e)
108 {
109 throw new IllegalStateException(
110 "Cannot load instance from APT filer source output folder.", e);
111 }
112 }
113
114 private void removeFile(final FileObject file)
115 {
116 try
117 {
118 file.delete();
119 }
120 catch (final IllegalStateException e)
121 {
122
123
124
125
126
127
128 }
129 }
130
131
132
133
134
135
136
137
138 public void serialize(final UnitTestDoc testDoc, final File file)
139 throws IllegalStateException
140 {
141 try
142 {
143 final OutputStream output = new FileOutputStream(file);
144 serialize(testDoc, output);
145 }
146 catch (final FileNotFoundException e)
147 {
148 throw new IllegalStateException("Cannot write instance of type '"
149 + testDoc.getClass().getName()
150 + "' to file '" + file.getAbsolutePath()
151 + "'.", e);
152 }
153 }
154
155
156
157
158
159
160
161 public Object deserialize(final InputStream in)
162 {
163 ObjectInputStream input = null;
164 try
165 {
166 input = new ObjectInputStream(new BufferedInputStream(in));
167 final Object instance = input.readObject();
168 return instance;
169 }
170 catch (final IOException e)
171 {
172 throw new IllegalStateException("Cannot load instance from stream.", e);
173 }
174 catch (final ClassNotFoundException e)
175 {
176 throw new IllegalStateException(
177 "Cannot find class for instance to read serialized form from stream.",
178 e);
179 }
180 finally
181 {
182 IOUtils.closeQuietly(input);
183 }
184 }
185
186
187
188
189
190
191
192 public Object deserialize(final File file)
193 {
194 try
195 {
196 final InputStream input = new FileInputStream(file);
197 return deserialize(input);
198 }
199 catch (final FileNotFoundException e)
200 {
201 throw new IllegalStateException("Cannot load instance from file '"
202 + file.getAbsolutePath() + "'.", e);
203 }
204 }
205
206
207
208
209
210
211
212 public Object deserialize(final FileObject file)
213 {
214 try
215 {
216 final InputStream input = file.openInputStream();
217 return deserialize(input);
218 }
219 catch (final IOException e)
220 {
221 throw new IllegalStateException("Cannot load instance from file '"
222 + file.getName() + "'.", e);
223 }
224 }
225
226
227
228 }