1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.cosylab.application.state;
21
22 import com.cosylab.application.state.impl.CDBStateStorage;
23 import com.cosylab.application.state.impl.DefaultState;
24 import com.cosylab.application.state.impl.SerialStateStorage;
25 import com.cosylab.application.state.impl.TextStateStorage;
26 import com.cosylab.application.state.impl.XMLStateStorage;
27
28 import java.io.File;
29 import java.io.FilenameFilter;
30
31 import java.util.HashMap;
32 import java.util.Iterator;
33
34
35
36
37
38
39
40
41
42 public final class StateFactory
43 {
44
45 public static final String STORAGE_BIN = "BIN";
46
47
48 public static final String STORAGE_TXT = "TXT";
49
50
51 public static final String STORAGE_XML = "XML";
52
53
54 public static final String STORAGE_CDB = "CDB";
55
56
57 public static final String STORAGE_DAT = "DAT";
58
59
60 public static HashMap stateStorages = null;
61
62
63 public static String defaultstateStorage = null;
64
65 static {
66
67 stateStorages = new HashMap(5);
68
69
70 registerStateStorage(STORAGE_BIN, SerialStateStorage.class.getName());
71 registerStateStorage(STORAGE_TXT, TextStateStorage.class.getName());
72 registerStateStorage(STORAGE_XML, XMLStateStorage.class.getName());
73 registerStateStorage(STORAGE_CDB, CDBStateStorage.class.getName());
74 setDefaultStateStorage(STORAGE_XML);
75
76
77 String userTypeClass = System.getProperty("StateStorage");
78
79 if(userTypeClass != null) {
80 registerStateStorage(STORAGE_DAT, userTypeClass);
81
82
83 setDefaultStateStorage(STORAGE_DAT);
84 }
85
86
87 String defaultSS = System.getProperty("DefaultStateStorage");
88
89 if(defaultSS != null) {
90
91 if(stateStorages.get(defaultSS) != null) {
92 setDefaultStateStorage(defaultSS);
93 }
94 }
95 }
96
97
98
99
100
101
102
103 public static void registerStateStorage(final String type,
104 final String className)
105 {
106 stateStorages.put(type, className);
107 }
108
109
110
111
112
113
114 public static void setDefaultStateStorage(final String type)
115 {
116 defaultstateStorage = type;
117 }
118
119 protected static boolean typeExists(final File[] files, final String type)
120 {
121 if(files == null) {
122 return false;
123 }
124
125 for(int i = 0; i < files.length; i++) {
126 if(files[i].getName().endsWith(type)
127 || files[i].getName().endsWith(type.toLowerCase())) {
128 return true;
129 }
130 }
131
132 return false;
133 }
134
135
136
137
138
139
140
141
142
143 public static StateStorage createStateStorage(final String filePath,
144 final String appName)
145 {
146
147 try {
148
149 File file = new File(filePath);
150 File[] files = file.listFiles(new FilenameFilter() {
151 public boolean accept(File dir, String name)
152 {
153 if(name.startsWith(appName)) {
154 return true;
155 }
156
157 return false;
158 }
159 });
160
161
162 if(!typeExists(files, defaultstateStorage)) {
163 Iterator iter = stateStorages.keySet().iterator();
164
165 while(iter.hasNext()) {
166 String type = (String)iter.next();
167
168 if(typeExists(files, type)) {
169
170 StateStorage ss = createStateStorage(type);
171 Class[] paramTypes = { StateStorage.class };
172 Object[] params = { ss };
173
174 return (StateStorage)Class.forName((String)stateStorages
175 .get(defaultstateStorage)).getConstructor(paramTypes)
176 .newInstance(params);
177 }
178 }
179 }
180 } catch(Exception e) {
181
182 }
183
184 return createStateStorage();
185 }
186
187
188
189
190
191
192 public static StateStorage createStateStorage()
193 {
194 return createStateStorage(defaultstateStorage);
195 }
196
197
198
199
200
201
202
203
204
205
206 public static StateStorage createStateStorage(final String type)
207 {
208 String className = (String)stateStorages.get(type);
209
210 if(className == null) {
211 return null;
212 }
213
214 try {
215 Object obj = Class.forName(className).newInstance();
216
217 return (StateStorage)obj;
218 } catch(Exception e) {
219 throw new IllegalArgumentException(e.getMessage());
220 }
221 }
222
223
224
225
226
227
228 public static State createState()
229 {
230 return new DefaultState();
231 }
232 }
233
234