1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jdiagnose.config;
17
18 import java.util.ArrayList;
19 import java.util.Iterator;
20 import java.util.List;
21 import java.util.Map;
22
23 /***
24 * @author jmccrindle
25 */
26 public class DefaultConfig implements Config {
27
28 private ConfigDataService dataService = null;
29 private Map defaultValues = null;
30 private ArrayList listeners = new ArrayList();
31
32 public DefaultConfig(ConfigDataService dataService, Map defaultValues) {
33 this.dataService = dataService;
34 this.defaultValues = defaultValues;
35 }
36
37
38
39
40 public Object get(String key) {
41 Object result = dataService.get(key);
42 if(result == null) {
43 result = defaultValues.get(key);
44 }
45 System.out.println("!!!!!!!!!!!!!!!!!!!!!! key " + key + " result " + result);
46 return result;
47 }
48
49
50
51
52 public void put(String key, Object newValue) throws ConfigException {
53 Object oldValue = dataService.get(key);
54 dataService.put(key, newValue);
55 List changeExceptions = null;
56 for (Iterator listenerIterator = listeners.iterator(); listenerIterator.hasNext();) {
57 ConfigChangeListener listener = (ConfigChangeListener) listenerIterator.next();
58 try {
59 listener.onConfigChange(new ConfigChangeEvent(this, this, key,
60 oldValue, newValue));
61 } catch (ConfigException e) {
62 if(changeExceptions == null) {
63 changeExceptions = new ArrayList();
64 }
65 changeExceptions.add(e);
66 }
67 }
68 if(changeExceptions != null) {
69 throw new ConfigChangeException(changeExceptions);
70 }
71 }
72
73
74
75
76 public Object getDefaultValue(String key) {
77 return defaultValues.get(key);
78 }
79
80 public void setDefaultValues(Map defaultValues) {
81 this.defaultValues = defaultValues;
82 }
83
84 public void setDataService(ConfigDataService dataService) {
85 this.dataService = dataService;
86 }
87
88 public void setListeners(List listeners) {
89 this.listeners = new ArrayList(listeners);
90 }
91
92 }