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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
|
/*
* XLogo4Schools - A Logo Interpreter specialized for use in schools, based on XLogo by Loic Le Coq
* Copyright (C) 2013 Marko Zivkovic
* Contact Information: marko88zivkovic at gmail dot com
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version. This program is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the
* GNU General Public License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
* This Java source code belongs to XLogo4Schools, written by Marko Zivkovic
* during his Bachelor thesis at the computer science department of ETH Zurich,
* in the year 2013 and/or during future work.
* It is a reengineered version of XLogo written by Loic Le Coq, published
* under the GPL License at http://xlogo.tuxfamily.org/
* Contents of this file were entirely written by Marko Zivkovic
*/
package xlogo.storage;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import xlogo.interfaces.Observable;
import xlogo.interfaces.Observable.PropertyChangeListener;
import xlogo.storage.workspace.Serializer;
import xlogo.utils.Utils;
/**
* The container class for anything that must be stored persistently.
* @author Marko Zivkovic
*/
public class StorableObject<T extends Observable<E>, E extends Enum<E>> extends Storable {
private static final long serialVersionUID = -3394846264634066188L;
private static Logger logger = LogManager.getLogger(StorableObject.class.getSimpleName());
public static final String DEFAULT_PLAIN_NAME_PREFIX = "X4S_";
public static final File DEFAULT_LOCATION = new File(System.getProperty("user.home"));
private Class<T> targetClass = null;
private T object = null;
private Serializer<T> serializer = null;
private Initializer<T> creationInitilizer = null;
private Initializer<T> loadInitilizer = null;
private transient final PropertyChangeListener objectDirtyListener = new PropertyChangeListener(){
@Override
public void propertyChanged() {
makeDirty();
}
};
/*
* PATH BUILDERS
*/
/**
* @param c
* @return X4S_ClassName.ser
*/
public static String getDefaulPlainName(Class<?> c) {
return DEFAULT_PLAIN_NAME_PREFIX + c.getSimpleName();
}
public static String getFileName(String plainName){
return "." + plainName;
}
public static String getFileName(Class<?> c){
return getFileName(getDefaulPlainName(c));
}
public static File getFilePath(File dir, Class<?> c) {
return new File(dir.toString() + File.separator + getFileName(c));
}
/*
* Constructors
*/
/**
* @param c - The class of the object to be stored / loaded
*/
public StorableObject(Class<T> c) {
this(c, null, getDefaulPlainName(c), false);
}
/**
* @param c - The class of the object to be stored / loaded
* @param dir - The location where the object is stored
*/
public StorableObject(Class<T> c, File dir) {
this(c, dir, getDefaulPlainName(c), false);
}
/**
* @param c - The class of the object to be stored / loaded
* @param dir - The location where the object is stored
* @param isDeferred - A deferred persistent object is not immediately persisted after creation, but after the next call to {@link #store()}
*/
public StorableObject(Class<T> c, File dir, boolean isDeferred) {
this(c, dir, getDefaulPlainName(c), isDeferred);
}
/**
* @param c - The class of the object to be stored / loaded
* @param dir - The location where the object is stored
* @param plainName - The object's file name without extensions or dots
*/
public StorableObject(Class<T> c, File dir, String plainName) {
this(c, dir, plainName, false);
}
/**
* @param c - The class of the object to be stored / loaded
* @param dir - The location where the object is stored
* @param plainName - The object's file name without extensions or dots
* @param isDeferred - A deferred persistent object is not immediately persisted after creation, but after the next call to {@link #store()}
*/
public StorableObject(Class<T> c, File dir, String plainName, boolean isDeferred) {
this.targetClass = c;
setLocation(dir);
setPlainName(plainName);
setStoringDeferred(isDeferred);
}
/*
* Create, Store & Load
*/
public StorableObject<T, E> createOrLoad() throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, SecurityException {
File file = getFilePath();
if (file.exists()) {
try {
load();
}
catch (Exception e) {
logger.warn("Could not load object from file " + e.toString());
file.delete();
}
}
if(!file.exists() || object == null){
create();
store();
}
return this;
}
public StorableObject<T, E> create() throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, SecurityException {
set(getTargetClass().getConstructor().newInstance());
if (getCreationInitilizer() != null) {
getCreationInitilizer().init(get());
}
store();
return this;
}
public StorableObject<T, E> load() throws IOException, ClassNotFoundException, ClassCastException{
setPersisted(false);
if (getSerializer() != null){
set(loadObject(getFilePath(), getSerializer()));
} else {
set(loadObject(getFilePath(), getTargetClass()));
}
if (getLoadInitilizer() != null) {
getLoadInitilizer().init(get());
}
makeClean();
setPersisted(true);
return this;
}
@Override
public void storeCopyToFile(File file) throws IOException, IllegalArgumentException {
if (file == null)
throw new IllegalArgumentException("file must not be null.");
logger.trace("Storing Object to " + file.getAbsolutePath());
file.getParentFile().mkdirs();
if (!isVirtual()) {
if (getSerializer() != null) {
Utils.storeFile(file, getSerializer().serialize2String(get()));
}
else {
Utils.store(file, get());
}
}
makeClean();
}
/**
* Load a Storable object from the specified file
* @param file
* @param c
* @return Load an object of the given class from a file in Java's own byte representation
* @throws IOException
* @throws ClassNotFoundException
* @throws ClassCastException
*/
public static <T> T loadObject(File file, Class<T> c) throws IOException, ClassNotFoundException, ClassCastException {
logger.trace("Loading Object from " + file.getAbsolutePath());
return Utils.readObject(file, c);
}
/**
* @param file
* @param serializer
* @return The object persistent in the file as interpreted by the serializer
* @throws IOException
*/
public static <T> T loadObject(File file, Serializer<T> serializer) throws IOException {
logger.trace("Loading Object from " + file.getAbsolutePath() + " with " + serializer.toString());
String serialized = Utils.readFile(file);
T object = serializer.deserialize(serialized);
return object;
}
/*
* Getters & Setters
*/
/**
* If this exists on the file system, that file will be renamed. <p>
* If plainName already existed, it is deleted first.
* @param newFileName - if null, {@link #getDefaulPlainName(Class)} is taken
* @throws IllegalArgumentException - If the provided name is not legal.
*/
@Override
public void setPlainName(String plainName){
if (plainName == null) {
plainName = getDefaulPlainName(targetClass);
}
super.setPlainName(plainName);
}
@Override
public String getFileNamePrefix(){
return ".";
}
@Override
public String getFileNameExtension() {
return "";
}
public Class<T> getTargetClass(){
return targetClass;
}
public Serializer<T> getSerializer() {
return this.serializer;
}
public void setSerializer(Serializer<T> serializer){
this.serializer = serializer;
}
/**
* Get the persistent object
* @return
*/
public T get() {
return this.object;
}
/**
* Set the persistent object
* @param object
*/
public void set(T object) {
if (this.object == object){
return;
}
if (this.object != null){
this.object.removePropertyChangeListener(null, objectDirtyListener);
} else {
makeDirty();
}
this.object = object;
if (this.object != null){
object.addPropertyChangeListener(null, objectDirtyListener);
}
}
public Initializer<T> getCreationInitilizer() {
return creationInitilizer;
}
public void setCreationInitilizer(Initializer<T> firstTimeInitilizer) {
this.creationInitilizer = firstTimeInitilizer;
}
public Initializer<T> getLoadInitilizer() {
return loadInitilizer;
}
public void setLoadInitilizer(Initializer<T> loadTimeInitilizer) {
this.loadInitilizer = loadTimeInitilizer;
}
/*
* Chainable setters
*/
/**
* Convenience method for {@link #setSerializer(Initializer)}
* that allows chaining with other methods such as {@link #createOrLoad()}
* @param serializer
* @return
*/
public StorableObject<T, E> withSerializer(Serializer <T> serializer){
setSerializer(serializer);
return this;
}
/**
* Convenience method for {@link #setCreationInitilizer(Initializer)}
* that allows chaining with other methods such as {@link #createOrLoad()}
* @param firstTimeInitializer
* @return
*/
public StorableObject<T, E> withCreationInitializer(Initializer<T> firstTimeInitializer){
setCreationInitilizer(firstTimeInitializer);
return this;
}
/**
* Convenience method for {@link #setLoadInitilizer(Initializer)}
* that allows chaining with other methods such as {@link #createOrLoad()}
* @param loadTimeInitializer
* @return
*/
public StorableObject<T, E> withLoadInitializer(Initializer<T> loadTimeInitializer){
setLoadInitilizer(loadTimeInitializer);
return this;
}
/*
* Interfaces
*/
public interface Initializer<T> {
public void init(T target);
}
}
|