1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package de.smartics.maven.issues.cache;
18
19 import java.io.BufferedInputStream;
20 import java.io.BufferedOutputStream;
21 import java.io.Closeable;
22 import java.io.File;
23 import java.io.FileInputStream;
24 import java.io.FileNotFoundException;
25 import java.io.FileOutputStream;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.OutputStream;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.eclipse.mylyn.tasks.core.data.TaskData;
33
34
35
36
37
38
39
40
41 public abstract class AbstractFileTaskDataStore implements TaskDataStore
42 {
43
44
45
46
47
48
49
50 private static final Log LOG = LogFactory
51 .getLog(AbstractFileTaskDataStore.class);
52
53
54
55
56
57
58 protected final File persistentStoreDirectory;
59
60
61
62
63
64 protected final String fileNameExtension;
65
66
67
68
69
70
71
72
73
74
75
76
77 protected AbstractFileTaskDataStore(final File persistentStoreDirectory,
78 final String fileNameExtension)
79 {
80 this.persistentStoreDirectory = persistentStoreDirectory;
81 this.fileNameExtension = fileNameExtension;
82 }
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97 public TaskData load(final String id) throws PersistenceException
98 {
99 final File file = createFile(id);
100 if (file.exists())
101 {
102 final TaskData task = readTask(file);
103 return task;
104 }
105 return null;
106 }
107
108
109
110
111
112
113
114 protected File createFile(final String id)
115 {
116 return new File(this.persistentStoreDirectory, id + fileNameExtension);
117 }
118
119
120
121
122
123
124
125
126
127 protected TaskData readTask(final File file) throws PersistenceException
128 {
129 InputStream in = null;
130 try
131 {
132 in = new BufferedInputStream(new FileInputStream(file));
133 final TaskData task = readTask(in);
134 return task;
135 }
136 catch (final Exception e)
137 {
138 final String message =
139 "Cannot read task from file '" + file.getAbsolutePath() + "'.";
140 LOG.warn(message);
141 throw new PersistenceException(message, e);
142 }
143 finally
144 {
145 close(file, in);
146 }
147 }
148
149
150
151
152
153
154
155
156
157 protected abstract TaskData readTask(InputStream inputStream)
158 throws PersistenceException;
159
160
161
162
163 public void persist(final TaskData task) throws PersistenceException
164 {
165 final String id = task.getTaskId();
166 final File file = createFile(id);
167 writeTask(file, task);
168 }
169
170
171
172
173
174
175
176
177
178
179 protected void writeTask(final File file, final TaskData task)
180 throws PersistenceException
181 {
182 OutputStream out = null;
183 try
184 {
185 out = new BufferedOutputStream(new FileOutputStream(file));
186 writeTask(out, task);
187 }
188 catch (final FileNotFoundException e)
189 {
190 final String message =
191 "Cannot persist task data with ID '" + task.getTaskId()
192 + "' to file '" + file.getAbsolutePath() + "'.";
193 LOG.warn(message, e);
194 throw new PersistenceException(message, e);
195 }
196 finally
197 {
198 close(file, out);
199 }
200 }
201
202
203
204
205
206
207 protected static void close(final Closeable closeable)
208 {
209 close(null, closeable);
210 }
211
212
213
214
215
216
217
218 protected static void close(final File file, final Closeable closeable)
219 {
220 if (closeable != null)
221 {
222 try
223 {
224 closeable.close();
225 }
226 catch (final IOException e)
227 {
228
229 if (LOG.isErrorEnabled())
230 {
231 final String message =
232 file != null ? "Cannot close stream to file '" + file
233 + "'. Continue..."
234 : "Cannot close stream. Continue...";
235 LOG.error(message, e);
236 }
237 }
238 }
239 }
240
241
242
243
244
245
246
247
248
249 protected abstract void writeTask(OutputStream outputStream, TaskData task)
250 throws PersistenceException;
251
252
253
254 }