|
1 |
| package org.jconfig; |
|
2 |
| |
|
3 |
| import java.util.HashMap; |
|
4 |
| import org.jconfig.error.ErrorReporter; |
|
5 |
| import org.jconfig.event.FileListener; |
|
6 |
| import org.jconfig.event.FileListenerEvent; |
|
7 |
| import org.jconfig.handler.AbstractHandler; |
|
8 |
| import org.jconfig.handler.ConfigurationHandler; |
|
9 |
| import org.jconfig.handler.InputStreamHandler; |
|
10 |
| import org.jconfig.bootstrap.*; |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| public class ConfigurationManager { |
|
28 |
| |
|
29 |
| |
|
30 |
| private static ConfigurationManager cm = null; |
|
31 |
| |
|
32 |
| private static HashMap configurations = null; |
|
33 |
| |
|
34 |
| private static HashMap handlerMapping; |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
1
| private ConfigurationManager() {
|
|
41 |
1
| configurations = new HashMap();
|
|
42 |
1
| handlerMapping = new HashMap();
|
|
43 |
1
| BootstrapLoader bl = BootstrapLoaderFactory.getLoader();
|
|
44 |
1
| try {
|
|
45 |
1
| LoadedItem[] configs = bl.load();
|
|
46 |
1
| for ( int i = 0; i < configs.length;i++) {
|
|
47 |
1
| LoadedItem li = configs[i];
|
|
48 |
1
| Configuration config = li.getConfig();
|
|
49 |
1
| ConfigurationHandler handler = li.getHandler();
|
|
50 |
1
| String name = li.getName();
|
|
51 |
1
| configurations.put(name, config);
|
|
52 |
1
| handlerMapping.put(name, handler);
|
|
53 |
| } |
|
54 |
| } |
|
55 |
| catch (Exception e) { |
|
56 |
0
| ErrorReporter.getErrorHandler().reportError("Exception while trying to load configuratins at startup:",e);
|
|
57 |
| } |
|
58 |
| } |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
122
| public static synchronized ConfigurationManager getInstance() {
|
|
67 |
122
| if (cm == null) {
|
|
68 |
1
| cm = new ConfigurationManager();
|
|
69 |
| } |
|
70 |
122
| return cm;
|
|
71 |
| } |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
6
| public static Configuration getConfiguration() {
|
|
79 |
6
| return getConfiguration("default");
|
|
80 |
| } |
|
81 |
| |
|
82 |
| |
|
83 |
| |
|
84 |
| |
|
85 |
| |
|
86 |
| |
|
87 |
| |
|
88 |
| |
|
89 |
| |
|
90 |
| |
|
91 |
| |
|
92 |
| |
|
93 |
| |
|
94 |
| |
|
95 |
| |
|
96 |
| |
|
97 |
| |
|
98 |
| |
|
99 |
| |
|
100 |
| |
|
101 |
| |
|
102 |
| |
|
103 |
54
| public static Configuration getConfiguration(String name) {
|
|
104 |
54
| getInstance();
|
|
105 |
54
| if ( name == null ) {
|
|
106 |
0
| return new DefaultConfiguration(name);
|
|
107 |
| } |
|
108 |
54
| if (configurations.containsKey(name)) {
|
|
109 |
19
| return (Configuration) configurations.get(name);
|
|
110 |
| } else { |
|
111 |
35
| String fileName = "config.xml";
|
|
112 |
35
| if ( !name.equals("default") ) {
|
|
113 |
34
| fileName = name+"_config.xml";
|
|
114 |
| } |
|
115 |
35
| InputStreamHandler ish = new InputStreamHandler(fileName);
|
|
116 |
35
| try {
|
|
117 |
35
| Configuration config = ish.load(name);
|
|
118 |
30
| if ( config != null ) {
|
|
119 |
| |
|
120 |
30
| configurations.put(name, config);
|
|
121 |
30
| handlerMapping.put(name, ish);
|
|
122 |
30
| getInstance().addFileListener(name,new ConfigurationFileListener(name));
|
|
123 |
| } |
|
124 |
30
| return config;
|
|
125 |
| } catch (ConfigurationManagerException e) { |
|
126 |
5
| ErrorReporter.getErrorHandler().reportError("Problem while trying to read configuration. Returning new configuration.",e);
|
|
127 |
5
| return new DefaultConfiguration(name);
|
|
128 |
| } |
|
129 |
| } |
|
130 |
| } |
|
131 |
| |
|
132 |
| |
|
133 |
| |
|
134 |
| |
|
135 |
| |
|
136 |
| |
|
137 |
| |
|
138 |
| |
|
139 |
2
| public void load(ConfigurationHandler configurationHandler,String configurationName) throws ConfigurationManagerException {
|
|
140 |
2
| Configuration config = configurationHandler.load(configurationName);
|
|
141 |
2
| if(configurationHandler instanceof AbstractHandler) {
|
|
142 |
2
| ((AbstractHandler)configurationHandler).addFileListener(new ConfigurationFileListener(configurationName));
|
|
143 |
| } |
|
144 |
2
| if (config != null) {
|
|
145 |
| |
|
146 |
2
| configurations.put(configurationName, config);
|
|
147 |
2
| handlerMapping.put(configurationName, configurationHandler);
|
|
148 |
| } |
|
149 |
| } |
|
150 |
| |
|
151 |
| |
|
152 |
| |
|
153 |
| |
|
154 |
| |
|
155 |
| |
|
156 |
| |
|
157 |
| |
|
158 |
1
| public void save(String configName) throws ConfigurationManagerException {
|
|
159 |
1
| ConfigurationHandler handler = (ConfigurationHandler)handlerMapping.get(configName);
|
|
160 |
1
| if ( handler == null ) {
|
|
161 |
0
| throw new ConfigurationManagerException("No handler assigned to this configuration");
|
|
162 |
| } |
|
163 |
1
| Configuration config = (Configuration)configurations.get(configName);
|
|
164 |
1
| if ( config == null ) {
|
|
165 |
0
| throw new ConfigurationManagerException("No configuration found with the given name");
|
|
166 |
| } |
|
167 |
1
| save(handler, config);
|
|
168 |
| } |
|
169 |
| |
|
170 |
30
| public void addFileListener(String configName,FileListener fl) {
|
|
171 |
30
| ConfigurationHandler handler = (ConfigurationHandler)handlerMapping.get(configName);
|
|
172 |
30
| if ( handler != null ) {
|
|
173 |
30
| ((AbstractHandler)handler).addFileListener(fl);
|
|
174 |
| } |
|
175 |
| } |
|
176 |
| |
|
177 |
| |
|
178 |
| |
|
179 |
| |
|
180 |
| |
|
181 |
| |
|
182 |
| |
|
183 |
| |
|
184 |
| |
|
185 |
| |
|
186 |
| |
|
187 |
1
| public void save(ConfigurationHandler handler, Configuration config) throws ConfigurationManagerException {
|
|
188 |
1
| handler.store(config);
|
|
189 |
1
| configurations.put(config.getConfigName(), config);
|
|
190 |
1
| handlerMapping.put(config.getConfigName(), handler);
|
|
191 |
| } |
|
192 |
| |
|
193 |
| |
|
194 |
| |
|
195 |
| |
|
196 |
| |
|
197 |
| |
|
198 |
33
| public void removeConfiguration(String configName) {
|
|
199 |
33
| if ( configName != null ) {
|
|
200 |
33
| configurations.remove(configName);
|
|
201 |
33
| handlerMapping.remove(configName);
|
|
202 |
| } |
|
203 |
| } |
|
204 |
| |
|
205 |
| |
|
206 |
| |
|
207 |
| |
|
208 |
| |
|
209 |
| |
|
210 |
| |
|
211 |
0
| public void reload(String name) throws ConfigurationManagerException {
|
|
212 |
0
| if (configurations.containsKey(name)) {
|
|
213 |
0
| Configuration cfg = getConfiguration(name);
|
|
214 |
0
| ConfigurationHandler handler = getConfigurationHandler(name);
|
|
215 |
0
| if (handler == null) {
|
|
216 |
0
| throw new ConfigurationManagerException("There is no handler associated for this configuration");
|
|
217 |
| } |
|
218 |
0
| cfg = handler.load(name);
|
|
219 |
0
| configurations.put(name, cfg);
|
|
220 |
| } else { |
|
221 |
0
| throw new ConfigurationManagerException("There is no configuration with this name");
|
|
222 |
| } |
|
223 |
| } |
|
224 |
| |
|
225 |
| |
|
226 |
| |
|
227 |
| |
|
228 |
| |
|
229 |
| |
|
230 |
| |
|
231 |
| |
|
232 |
0
| public ConfigurationHandler getConfigurationHandler(String name) {
|
|
233 |
0
| ConfigurationHandler handler =
|
|
234 |
| (ConfigurationHandler) handlerMapping.get(name); |
|
235 |
0
| return handler;
|
|
236 |
| } |
|
237 |
| |
|
238 |
| |
|
239 |
| |
|
240 |
| |
|
241 |
| |
|
242 |
| |
|
243 |
1
| public String[] getConfigurationNames() {
|
|
244 |
1
| return (String[]) configurations.keySet().toArray(new String[configurations.size()]);
|
|
245 |
| } |
|
246 |
| |
|
247 |
| } |
|
248 |
| |
|
249 |
| class ConfigurationFileListener implements FileListener { |
|
250 |
| private String config = null; |
|
251 |
| |
|
252 |
32
| ConfigurationFileListener(String configurationName) {
|
|
253 |
32
| config = configurationName;
|
|
254 |
| } |
|
255 |
| |
|
256 |
| |
|
257 |
| |
|
258 |
0
| public void fileChanged(FileListenerEvent e) {
|
|
259 |
0
| try {
|
|
260 |
0
| ConfigurationManager.getInstance().reload(config);
|
|
261 |
| } catch (ConfigurationManagerException e1) { |
|
262 |
0
| System.out.println("Problem with reloading the file after file was saved.");
|
|
263 |
| } |
|
264 |
| } |
|
265 |
| |
|
266 |
| } |