1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package org.treetank.access.conf;
28
29 import static com.google.common.base.Objects.toStringHelper;
30
31 import java.io.File;
32 import java.io.FileReader;
33 import java.io.FileWriter;
34 import java.io.IOException;
35 import java.lang.reflect.Constructor;
36 import java.lang.reflect.InvocationTargetException;
37 import java.util.ArrayList;
38 import java.util.List;
39 import java.util.Objects;
40 import java.util.Properties;
41
42 import org.treetank.access.Session;
43 import org.treetank.api.IMetaEntryFactory;
44 import org.treetank.api.IDataFactory;
45 import org.treetank.exception.TTIOException;
46 import org.treetank.io.IBackend;
47 import org.treetank.io.IBackend.IBackendFactory;
48 import org.treetank.io.bytepipe.ByteHandlerPipeline;
49 import org.treetank.io.bytepipe.Encryptor;
50 import org.treetank.io.bytepipe.IByteHandler;
51 import org.treetank.io.bytepipe.IByteHandler.IByteHandlerPipeline;
52 import org.treetank.revisioning.IRevisioning;
53
54 import com.google.gson.stream.JsonReader;
55 import com.google.gson.stream.JsonWriter;
56 import com.google.inject.Inject;
57 import com.google.inject.assistedinject.Assisted;
58
59
60
61
62
63
64
65
66
67
68
69
70 public final class ResourceConfiguration {
71
72
73
74
75 public enum Paths implements IConfigurationPath {
76
77
78 Data(new File("data"), true),
79
80 TransactionLog(new File("log"), true),
81
82 ConfigBinary(new File("ressetting.obj"), false);
83
84
85 private final File mFile;
86
87
88 private final boolean mIsFolder;
89
90
91
92
93
94
95
96
97
98 private Paths(final File pFile, final boolean pIsFolder) {
99 this.mFile = pFile;
100 this.mIsFolder = pIsFolder;
101 }
102
103
104
105
106
107
108 public File getFile() {
109 return mFile;
110 }
111
112
113
114
115
116
117 public boolean isFolder() {
118 return mIsFolder;
119 }
120
121 }
122
123
124
125 public final IBackend mBackend;
126
127
128 public final IRevisioning mRevision;
129
130
131 public final Properties mProperties;
132
133
134 public final IDataFactory mDataFac;
135
136
137 public final IMetaEntryFactory mMetaFac;
138
139
140
141
142
143
144
145
146
147
148
149 @Inject
150 public ResourceConfiguration(@Assisted Properties pProperties, IBackendFactory pBackend,
151 IRevisioning pRevision, IDataFactory pDataFac, IMetaEntryFactory pMetaFac) {
152
153 this(pProperties, pBackend.create(pProperties), pRevision, pDataFac, pMetaFac);
154 }
155
156
157
158
159
160
161
162
163
164
165 public ResourceConfiguration(Properties pProperties, IBackend pStorage, IRevisioning pRevisioning,
166 IDataFactory pDataFac, IMetaEntryFactory pMetaFac) {
167 mProperties = pProperties;
168 mBackend = pStorage;
169 mRevision = pRevisioning;
170 mDataFac = pDataFac;
171 mMetaFac = pMetaFac;
172 }
173
174
175
176
177
178
179
180
181
182 public static interface IResourceConfigurationFactory {
183
184
185
186
187
188
189
190
191
192 ResourceConfiguration create(Properties pProperties);
193 }
194
195 private static final String[] JSONNAMES = {
196 "metaentryClass", "revisioningClass", "DataFactoryClass", "byteHandlerClasses", "storageClass",
197 "properties"
198 };
199
200 public static void serialize(final ResourceConfiguration pConfig) throws TTIOException {
201 try {
202
203 final File file =
204 new File(new File(new File(pConfig.mProperties.getProperty(ConstructorProps.STORAGEPATH),
205 StorageConfiguration.Paths.Data.getFile().getName()), pConfig.mProperties
206 .getProperty(ConstructorProps.RESOURCE)), Paths.ConfigBinary.getFile().getName());
207
208 FileWriter fileWriter = new FileWriter(file);
209 JsonWriter jsonWriter = new JsonWriter(fileWriter);
210 jsonWriter.beginObject();
211
212 jsonWriter.name(JSONNAMES[0]).value(pConfig.mMetaFac.getClass().getName());
213
214 jsonWriter.name(JSONNAMES[1]).value(pConfig.mRevision.getClass().getName());
215
216 jsonWriter.name(JSONNAMES[2]).value(pConfig.mDataFac.getClass().getName());
217
218 IByteHandlerPipeline byteHandler = pConfig.mBackend.getByteHandler();
219 jsonWriter.name(JSONNAMES[3]);
220 jsonWriter.beginArray();
221 for (IByteHandler handler : byteHandler) {
222 jsonWriter.value(handler.getClass().getName());
223 }
224 jsonWriter.endArray();
225
226 jsonWriter.name(JSONNAMES[4]).value(pConfig.mBackend.getClass().getName());
227 jsonWriter.name(JSONNAMES[5]);
228 jsonWriter.beginObject();
229 for (String key : pConfig.mProperties.stringPropertyNames()) {
230 jsonWriter.name(key).value(pConfig.mProperties.getProperty(key));
231 }
232 jsonWriter.endObject();
233 jsonWriter.endObject();
234 jsonWriter.close();
235 fileWriter.close();
236 } catch (final IOException exc) {
237 throw new TTIOException(exc);
238 }
239 }
240
241
242
243
244
245
246
247
248
249
250 public static ResourceConfiguration deserialize(final File pFile, final String pResource)
251 throws TTIOException {
252 try {
253
254 final File file =
255 new File(new File(new File(pFile, StorageConfiguration.Paths.Data.getFile().getName()),
256 pResource), Paths.ConfigBinary.getFile().getName());
257
258 FileReader fileReader = new FileReader(file);
259 JsonReader jsonReader = new JsonReader(fileReader);
260 jsonReader.beginObject();
261
262 jsonReader.nextName().equals(JSONNAMES[0]);
263 Class<?> metaBucketClazz = Class.forName(jsonReader.nextString());
264
265 jsonReader.nextName().equals(JSONNAMES[1]);
266 Class<?> revClazz = Class.forName(jsonReader.nextString());
267
268 jsonReader.nextName().equals(JSONNAMES[2]);
269 Class<?> dataFacClazz = Class.forName(jsonReader.nextString());
270
271
272 List<IByteHandler> handlerList = new ArrayList<IByteHandler>();
273 if (jsonReader.nextName().equals(JSONNAMES[3])) {
274 jsonReader.beginArray();
275 while (jsonReader.hasNext()) {
276 Class<?> handlerClazz = Class.forName(jsonReader.nextString());
277 Constructor<?> handlerCons = handlerClazz.getConstructors()[0];
278 if (handlerClazz.getName().equals(Encryptor.class.getName())) {
279 handlerList.add((IByteHandler)handlerCons.newInstance(StandardSettings.KEY));
280 } else {
281 handlerList.add((IByteHandler)handlerCons.newInstance());
282 }
283
284 }
285 jsonReader.endArray();
286 }
287 ByteHandlerPipeline pipeline =
288 new ByteHandlerPipeline(handlerList.toArray(new IByteHandler[handlerList.size()]));
289
290 jsonReader.nextName().equals(JSONNAMES[4]);
291 Class<?> storageClazz = Class.forName(jsonReader.nextString());
292 jsonReader.nextName().equals(JSONNAMES[5]);
293 Properties props = new Properties();
294 jsonReader.beginObject();
295 while (jsonReader.hasNext()) {
296 props.setProperty(jsonReader.nextName(), jsonReader.nextString());
297 }
298 jsonReader.endObject();
299 jsonReader.endObject();
300 jsonReader.close();
301 fileReader.close();
302
303 Constructor<?> metaBucketCons = metaBucketClazz.getConstructors()[0];
304 IMetaEntryFactory metaFac = (IMetaEntryFactory)metaBucketCons.newInstance();
305
306 Constructor<?> dataFacCons = dataFacClazz.getConstructors()[0];
307 IDataFactory dataFactory = (IDataFactory)dataFacCons.newInstance();
308
309 Constructor<?> revCons = revClazz.getConstructors()[0];
310 IRevisioning revObject = (IRevisioning)revCons.newInstance();
311
312 Constructor<?> storageCons = storageClazz.getConstructors()[0];
313 IBackend backend = (IBackend)storageCons.newInstance(props, dataFactory, metaFac, pipeline);
314
315 return new ResourceConfiguration(props, backend, revObject, dataFactory, metaFac);
316
317 } catch (IOException | ClassNotFoundException | InstantiationException | IllegalAccessException
318 | InvocationTargetException exc) {
319 throw new TTIOException(exc);
320 }
321 }
322
323
324
325
326 @Override
327 public int hashCode() {
328 return Objects.hash(mBackend, mRevision, mProperties, mDataFac);
329 }
330
331
332
333
334 @Override
335 public final boolean equals(final Object pObj) {
336 return this.hashCode() == pObj.hashCode();
337 }
338
339
340
341
342 @Override
343 public String toString() {
344 return toStringHelper(this).add("mBackend", mBackend.getClass()).add("mRevision", mRevision).add(
345 "mProperties", mProperties).add("mDataFac", mDataFac.getClass().getName()).toString();
346 }
347 }