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