|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| package org.jconfig; |
|
8 |
| |
|
9 |
| import java.io.Serializable; |
|
10 |
| import java.util.Enumeration; |
|
11 |
| import java.util.HashMap; |
|
12 |
| import java.util.Iterator; |
|
13 |
| import java.util.Properties; |
|
14 |
| import java.util.Set; |
|
15 |
| import java.util.SortedMap; |
|
16 |
| import java.util.StringTokenizer; |
|
17 |
| import java.util.TreeMap; |
|
18 |
| import java.util.Vector; |
|
19 |
| import javax.swing.event.EventListenerList; |
|
20 |
| |
|
21 |
| import org.jconfig.event.CategoryChangedEvent; |
|
22 |
| import org.jconfig.event.CategoryListener; |
|
23 |
| import org.jconfig.event.ConfigurationChangedEvent; |
|
24 |
| import org.jconfig.event.ConfigurationChangedEventImpl; |
|
25 |
| import org.jconfig.event.ConfigurationListener; |
|
26 |
| import org.jconfig.event.PropertyChangedEvent; |
|
27 |
| import org.jconfig.event.PropertyListener; |
|
28 |
| import org.jconfig.utils.IncludeEntry; |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| public class DefaultConfiguration implements Serializable,Configuration { |
|
39 |
| |
|
40 |
| protected static final VariableManager vm = VariableManager.getInstance(); |
|
41 |
| |
|
42 |
| protected String configName; |
|
43 |
| |
|
44 |
| protected HashMap categories; |
|
45 |
| |
|
46 |
| protected String mainCategory; |
|
47 |
| |
|
48 |
| private EventListenerList configurationListenerList = new EventListenerList(); |
|
49 |
| |
|
50 |
| private boolean created = true; |
|
51 |
| |
|
52 |
| private String encoding; |
|
53 |
| |
|
54 |
| protected String baseConfigName; |
|
55 |
| |
|
56 |
| private boolean dirtyFlag = false; |
|
57 |
| |
|
58 |
| private Vector includes = new Vector(); |
|
59 |
| |
|
60 |
0
| protected DefaultConfiguration() {
|
|
61 |
| } |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
89
| public DefaultConfiguration(String configName) {
|
|
70 |
89
| categories = new HashMap();
|
|
71 |
89
| this.configName = configName;
|
|
72 |
89
| setCategory("general", true);
|
|
73 |
89
| created = true;
|
|
74 |
| } |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
58
| public void setCategory(String name) {
|
|
83 |
58
| setCategory(name, false);
|
|
84 |
| } |
|
85 |
| |
|
86 |
| |
|
87 |
| |
|
88 |
| |
|
89 |
| |
|
90 |
| |
|
91 |
| |
|
92 |
| |
|
93 |
| |
|
94 |
| |
|
95 |
120
| public void setCategory(String name, boolean main) {
|
|
96 |
120
| if (name != null) {
|
|
97 |
120
| if (main) {
|
|
98 |
65
| mainCategory = name;
|
|
99 |
| } |
|
100 |
120
| if (!categories.containsKey(name)) {
|
|
101 |
109
| Category category = new DefaultCategory(name);
|
|
102 |
109
| category.setConfigurationName(configName);
|
|
103 |
109
| category.addCategoryListener(new MyCategoryListener());
|
|
104 |
109
| categories.put(name, category);
|
|
105 |
109
| markDirty();
|
|
106 |
109
| category.fireCategoryChangedEvent(
|
|
107 |
| new ConfigurationChangedEventImpl(ConfigurationChangedEvent.CATEGORY_ADDED, category, null, null, null )); |
|
108 |
109
| fireConfigurationChangedEvent(
|
|
109 |
| new ConfigurationChangedEventImpl(ConfigurationChangedEvent.CATEGORY_ADDED, category, null, null, null )); |
|
110 |
| } |
|
111 |
| } |
|
112 |
| } |
|
113 |
| |
|
114 |
5
| public void setCategory(Category category) {
|
|
115 |
5
| if (!categories.containsKey(category.getCategoryName())) {
|
|
116 |
3
| category.setConfigurationName(configName);
|
|
117 |
3
| category.addCategoryListener(new MyCategoryListener());
|
|
118 |
3
| categories.put(category.getCategoryName(), category);
|
|
119 |
3
| category.fireCategoryChangedEvent(
|
|
120 |
| new ConfigurationChangedEventImpl(ConfigurationChangedEvent.CATEGORY_ADDED, category, null, null, null )); |
|
121 |
3
| fireConfigurationChangedEvent(
|
|
122 |
| new ConfigurationChangedEventImpl(ConfigurationChangedEvent.CATEGORY_ADDED, category, null, null, null )); |
|
123 |
| } |
|
124 |
| } |
|
125 |
| |
|
126 |
| |
|
127 |
| |
|
128 |
| |
|
129 |
| |
|
130 |
| |
|
131 |
2
| public String getMainCategoryName() {
|
|
132 |
2
| return mainCategory;
|
|
133 |
| } |
|
134 |
| |
|
135 |
| |
|
136 |
| |
|
137 |
| |
|
138 |
| |
|
139 |
| |
|
140 |
| |
|
141 |
11
| public String[] getCategoryNames() {
|
|
142 |
11
| return getCategoryNames(true);
|
|
143 |
| } |
|
144 |
| |
|
145 |
14
| protected String[] getCategoryNames(boolean includeParent) {
|
|
146 |
14
| Set allCategories = categories.keySet();
|
|
147 |
14
| Vector all = new Vector(allCategories);
|
|
148 |
14
| if ( baseConfigName != null && includeParent ) {
|
|
149 |
0
| Configuration cfg = ConfigurationManager.getConfiguration(baseConfigName);
|
|
150 |
0
| String[] parentCategories = cfg.getCategoryNames();
|
|
151 |
0
| for ( int i = 0 ; i < parentCategories.length ; i++) {
|
|
152 |
0
| if ( all.indexOf(parentCategories[i]) == -1 ) {
|
|
153 |
0
| all.add(parentCategories[i]);
|
|
154 |
| } |
|
155 |
| } |
|
156 |
| } |
|
157 |
14
| return (String[]) all.toArray(new String[0]);
|
|
158 |
| } |
|
159 |
| |
|
160 |
| |
|
161 |
| |
|
162 |
| |
|
163 |
| |
|
164 |
| |
|
165 |
| |
|
166 |
| |
|
167 |
| |
|
168 |
| |
|
169 |
14
| public String getProperty(String key) {
|
|
170 |
14
| return getProperty(key, null, null);
|
|
171 |
| } |
|
172 |
| |
|
173 |
| |
|
174 |
| |
|
175 |
| |
|
176 |
| |
|
177 |
| |
|
178 |
| |
|
179 |
| |
|
180 |
| |
|
181 |
| |
|
182 |
| |
|
183 |
1
| public String getProperty(String key, String defaultValue) {
|
|
184 |
1
| return getProperty(key, defaultValue, null);
|
|
185 |
| } |
|
186 |
| |
|
187 |
| |
|
188 |
| |
|
189 |
| |
|
190 |
| |
|
191 |
| |
|
192 |
| |
|
193 |
| |
|
194 |
| |
|
195 |
| |
|
196 |
| |
|
197 |
| |
|
198 |
| |
|
199 |
| |
|
200 |
49
| public String getProperty(String key,String defaultValue,String categoryName) {
|
|
201 |
49
| boolean isMainCat = false;
|
|
202 |
49
| if (key == null) {
|
|
203 |
0
| return defaultValue;
|
|
204 |
| } |
|
205 |
49
| if (!categories.containsKey(categoryName)) {
|
|
206 |
12
| isMainCat = true;
|
|
207 |
12
| categoryName = mainCategory;
|
|
208 |
| } |
|
209 |
49
| Category category = getCategory(categoryName);
|
|
210 |
49
| if ( category.getCategoryName().equals(mainCategory)) {
|
|
211 |
24
| isMainCat = true;
|
|
212 |
| } |
|
213 |
49
| String tmp = category.getProperty(key);
|
|
214 |
| |
|
215 |
| |
|
216 |
49
| if ( tmp == null && !isMainCat) {
|
|
217 |
3
| category = getCategory(mainCategory);
|
|
218 |
3
| tmp = category.getProperty(key);
|
|
219 |
| } |
|
220 |
49
| if ( tmp == null ) {
|
|
221 |
8
| if ( baseConfigName != null ) {
|
|
222 |
1
| Configuration cfg = ConfigurationManager.getConfiguration(baseConfigName);
|
|
223 |
1
| tmp = cfg.getProperty(key,defaultValue,categoryName);
|
|
224 |
| } |
|
225 |
| else { |
|
226 |
7
| tmp = defaultValue;
|
|
227 |
| } |
|
228 |
| } |
|
229 |
49
| return tmp;
|
|
230 |
| } |
|
231 |
| |
|
232 |
| |
|
233 |
| |
|
234 |
| |
|
235 |
| |
|
236 |
| |
|
237 |
| |
|
238 |
| |
|
239 |
33
| public void setProperty(String name, String value) {
|
|
240 |
33
| setProperty(name, value, null);
|
|
241 |
| } |
|
242 |
| |
|
243 |
| |
|
244 |
| |
|
245 |
| |
|
246 |
| |
|
247 |
| |
|
248 |
| |
|
249 |
| |
|
250 |
| |
|
251 |
| |
|
252 |
215
| public void setProperty(String name, String value, String categoryName) {
|
|
253 |
215
| if (name != null) {
|
|
254 |
215
| if (categoryName == null) {
|
|
255 |
33
| categoryName = mainCategory;
|
|
256 |
| } |
|
257 |
215
| Category category = getCategory(categoryName);
|
|
258 |
215
| category.setProperty(name, value);
|
|
259 |
215
| markDirty();
|
|
260 |
215
| fireConfigurationChangedEvent(
|
|
261 |
| new ConfigurationChangedEventImpl(ConfigurationChangedEvent.PROPERTY_CHANGED, category, name, value,null )); |
|
262 |
| } |
|
263 |
| } |
|
264 |
| |
|
265 |
| |
|
266 |
| |
|
267 |
| |
|
268 |
| |
|
269 |
| |
|
270 |
| |
|
271 |
1
| public void removeProperty(String name) {
|
|
272 |
1
| if (name != null) {
|
|
273 |
1
| removeProperty(name, null);
|
|
274 |
| } |
|
275 |
| } |
|
276 |
| |
|
277 |
| |
|
278 |
| |
|
279 |
| |
|
280 |
| |
|
281 |
| |
|
282 |
| |
|
283 |
| |
|
284 |
| |
|
285 |
3
| public void removeProperty(String name, String category) {
|
|
286 |
3
| if (category == null) {
|
|
287 |
1
| category = mainCategory;
|
|
288 |
| } |
|
289 |
3
| if (name != null) {
|
|
290 |
| |
|
291 |
3
| Category cat = getCategory(category);
|
|
292 |
3
| String tmp = cat.getProperty(name,null);
|
|
293 |
3
| cat.setProperty(name, null);
|
|
294 |
3
| fireConfigurationChangedEvent(
|
|
295 |
| new ConfigurationChangedEventImpl(ConfigurationChangedEvent.PROPERTY_REMOVED,cat, name, tmp,null )); |
|
296 |
3
| markDirty();
|
|
297 |
| } |
|
298 |
| |
|
299 |
| } |
|
300 |
| |
|
301 |
| |
|
302 |
| |
|
303 |
| |
|
304 |
| |
|
305 |
| |
|
306 |
4
| public int getNumberOfCategories() {
|
|
307 |
4
| return getCategoryNames().length;
|
|
308 |
| } |
|
309 |
| |
|
310 |
| |
|
311 |
| |
|
312 |
| |
|
313 |
| |
|
314 |
| |
|
315 |
1
| public void removeCategory(String category) {
|
|
316 |
1
| if (categories.containsKey(category)) {
|
|
317 |
1
| Category cat = getCategory(category);
|
|
318 |
1
| categories.remove(category);
|
|
319 |
1
| cat.fireCategoryChangedEvent(
|
|
320 |
| new ConfigurationChangedEventImpl(ConfigurationChangedEvent.CATEGORY_REMOVED, cat, null, null, null)); |
|
321 |
1
| fireConfigurationChangedEvent(
|
|
322 |
| new ConfigurationChangedEventImpl(ConfigurationChangedEvent.CATEGORY_REMOVED,cat, null, null,null )); |
|
323 |
1
| cat = null;
|
|
324 |
1
| markDirty();
|
|
325 |
| } |
|
326 |
| } |
|
327 |
| |
|
328 |
1
| public Properties getProperties(String category) {
|
|
329 |
1
| return getProperties(category,true);
|
|
330 |
| } |
|
331 |
| |
|
332 |
1
| public Properties getProperties() {
|
|
333 |
1
| return getProperties(mainCategory,true);
|
|
334 |
| } |
|
335 |
| |
|
336 |
30
| protected Properties getProperties(String category,boolean includeParent) {
|
|
337 |
30
| Category cat = getCategory(category);
|
|
338 |
30
| Properties props = new Properties();
|
|
339 |
30
| if ( cat != null ) {
|
|
340 |
30
| props = (Properties)cat.getProperties().clone();
|
|
341 |
| } |
|
342 |
30
| if ( baseConfigName != null && includeParent ) {
|
|
343 |
1
| Configuration cfg = ConfigurationManager.getConfiguration(baseConfigName);
|
|
344 |
1
| Properties parentProps = cfg.getProperties(category);
|
|
345 |
1
| Enumeration num = parentProps.keys();
|
|
346 |
1
| while ( num.hasMoreElements() ) {
|
|
347 |
5
| String name = (String)num.nextElement();
|
|
348 |
5
| if ( !props.contains(name) ) {
|
|
349 |
5
| props.setProperty(name, (String)parentProps.get(name));
|
|
350 |
| } |
|
351 |
| } |
|
352 |
| } |
|
353 |
30
| return props;
|
|
354 |
| } |
|
355 |
| |
|
356 |
| |
|
357 |
| |
|
358 |
| |
|
359 |
| |
|
360 |
| |
|
361 |
| |
|
362 |
| |
|
363 |
1
| public String[] getPropertyNames(String category) {
|
|
364 |
1
| if ( containsCategory(category) ) {
|
|
365 |
| |
|
366 |
0
| Properties properties = getProperties(category);
|
|
367 |
0
| if (properties != null) {
|
|
368 |
0
| Enumeration en = properties.propertyNames();
|
|
369 |
0
| Vector keys = new Vector();
|
|
370 |
0
| while ( en.hasMoreElements() ) {
|
|
371 |
0
| String nm = (String)en.nextElement();
|
|
372 |
0
| keys.add(nm);
|
|
373 |
| } |
|
374 |
| |
|
375 |
| |
|
376 |
| |
|
377 |
0
| return (String[]) keys.toArray(new String[0]);
|
|
378 |
| } |
|
379 |
| } |
|
380 |
1
| return null;
|
|
381 |
| } |
|
382 |
| |
|
383 |
| |
|
384 |
| |
|
385 |
| |
|
386 |
| |
|
387 |
| |
|
388 |
| |
|
389 |
79
| public void setVariable(String name, String value) {
|
|
390 |
79
| if (name != null && value != null) {
|
|
391 |
79
| vm.addVariable(name, value,configName);
|
|
392 |
| } |
|
393 |
| } |
|
394 |
| |
|
395 |
| |
|
396 |
| |
|
397 |
| |
|
398 |
| |
|
399 |
| |
|
400 |
| |
|
401 |
3
| public HashMap getVariables() {
|
|
402 |
3
| return vm.getVariables(configName);
|
|
403 |
| } |
|
404 |
| |
|
405 |
14
| public String getVariable(String name) {
|
|
406 |
14
| return vm.getVariable(configName, name);
|
|
407 |
| } |
|
408 |
| |
|
409 |
| |
|
410 |
| |
|
411 |
| |
|
412 |
| |
|
413 |
| |
|
414 |
| |
|
415 |
4
| public int getIntProperty(String name, int defaultValue) {
|
|
416 |
4
| return getIntProperty(name, defaultValue, mainCategory);
|
|
417 |
| } |
|
418 |
| |
|
419 |
| |
|
420 |
| |
|
421 |
| |
|
422 |
| |
|
423 |
| |
|
424 |
| |
|
425 |
7
| public int getIntProperty(String name, int defaultValue, String category) {
|
|
426 |
7
| String value = getProperty(name,null,category);
|
|
427 |
7
| if (value == null) {
|
|
428 |
0
| return defaultValue;
|
|
429 |
| } else { |
|
430 |
7
| try {
|
|
431 |
7
| return Integer.parseInt(vm.replaceVariables(value,configName));
|
|
432 |
| } catch (NumberFormatException nfe) { |
|
433 |
0
| return defaultValue;
|
|
434 |
| } |
|
435 |
| } |
|
436 |
| } |
|
437 |
| |
|
438 |
| |
|
439 |
| |
|
440 |
| |
|
441 |
| |
|
442 |
| |
|
443 |
| |
|
444 |
| |
|
445 |
| |
|
446 |
2
| public boolean getBooleanProperty(String name, boolean defaultValue) {
|
|
447 |
2
| return getBooleanProperty(name, defaultValue, mainCategory);
|
|
448 |
| } |
|
449 |
| |
|
450 |
| |
|
451 |
| |
|
452 |
| |
|
453 |
| |
|
454 |
| |
|
455 |
| |
|
456 |
| |
|
457 |
| |
|
458 |
5
| public boolean getBooleanProperty(String name,boolean defaultValue,String categoryName) {
|
|
459 |
5
| String value = getProperty(name,null,categoryName);
|
|
460 |
5
| if ( value == null ) {
|
|
461 |
1
| |