01: package org.skunk.config;
02:
03: import java.beans.PropertyDescriptor;
04: import java.io.BufferedInputStream;
05: import java.io.BufferedOutputStream;
06: import java.io.File;
07: import java.io.FileInputStream;
08: import java.io.FileOutputStream;
09: import java.io.IOException;
10: import java.io.ObjectInputStream;
11: import java.io.ObjectOutputStream;
12: import java.util.HashMap;
13: import org.skunk.trace.Debug;
14:
15: /**
16: * a simple implementation of ConfigStore that stores
17: * config data in a serialized object in the filesystem.
18: * this imposes the restriction, naturally, that properties be
19: * serializable.
20: */
21: public class LocalConfigStore extends InMemoryConfigStore {
22: String filename;
23:
24: public LocalConfigStore(String filename) {
25: super ();
26:
27: //recover configMap
28: this .filename = filename;
29: File f = new File(filename);
30: if (f.exists()) {
31: try {
32: load();
33: } catch (Exception e) {
34: if (Debug.DEBUG)
35: Debug.trace(this , Debug.DP3, e);
36: }
37: }
38: }
39:
40: private void load() throws IOException, ClassNotFoundException {
41: FileInputStream fizz = new FileInputStream(filename);
42: BufferedInputStream biss = new BufferedInputStream(fizz);
43: ObjectInputStream oyster = new ObjectInputStream(biss);
44: setConfigMap((HashMap) oyster.readObject());
45: if (Debug.DEBUG)
46: Debug.trace(this , Debug.DP4, "config map is {0}",
47: getConfigMap());
48: oyster.close();
49: }
50:
51: private void save() throws IOException {
52: FileOutputStream foster = new FileOutputStream(filename);
53: BufferedOutputStream buster = new BufferedOutputStream(foster);
54: ObjectOutputStream ouster = new ObjectOutputStream(buster);
55: ouster.writeObject(getConfigMap());
56: ouster.close();
57: if (Debug.DEBUG)
58: Debug.trace(this , Debug.DP3, "saving configMap");
59: }
60:
61: public void setConfigValue(Class configClass, String propertyName,
62: Object configValue) {
63: super .setConfigValue(configClass, propertyName, configValue);
64: try {
65: save();
66: } catch (IOException oyVeh) {
67: if (Debug.DEBUG)
68: Debug.trace(this , Debug.DP3, oyVeh);
69: }
70: }
71: }
72:
73: /* $Log: LocalConfigStore.java,v $
74: /* Revision 1.4 2001/02/06 00:11:18 smulloni
75: /* struggle, perhaps futile, with the TextEditorCustomizer and other implicated
76: /* classes
77: /*
78: /* Revision 1.3 2000/11/09 23:34:46 smullyan
79: /* log added to every Java file, with the help of python. Lock stealing
80: /* implemented, and treatment of locks made more robust.
81: /* */
|