1 |
/* |
2 |
* i-scream central monitoring system |
3 |
* http://www.i-scream.org.uk |
4 |
* Copyright (C) 2000-2002 i-scream |
5 |
* |
6 |
* This program is free software; you can redistribute it and/or |
7 |
* modify it under the terms of the GNU General Public License |
8 |
* as published by the Free Software Foundation; either version 2 |
9 |
* of the License, or (at your option) any later version. |
10 |
* |
11 |
* This program is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
17 |
* along with this program; if not, write to the Free Software |
18 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
19 |
*/ |
20 |
|
21 |
//---PACKAGE DECLARATION--- |
22 |
package uk.org.iscream.cms.server.componentmanager; |
23 |
|
24 |
//---IMPORTS--- |
25 |
import java.util.*; |
26 |
import uk.org.iscream.cms.server.core.*; |
27 |
import uk.org.iscream.cms.server.util.*; |
28 |
|
29 |
/** |
30 |
* A Configuration Proxy hold configurations caches and |
31 |
* maintains configurations obtained from the Configuration |
32 |
* Manager. |
33 |
* |
34 |
* It also has support for checking for updates in configurations, |
35 |
* if a change is detected, it reloads all the changed configurations. |
36 |
* This is done by a thread which runs every ConfigurationProxy.updateTime |
37 |
* seconds. |
38 |
* |
39 |
* This is a singleton class, and should be used by all classes |
40 |
* that wish to gain a configuration from the server, as it then |
41 |
* allows them to be dynamically reconfigured. |
42 |
* |
43 |
* @author $Author: tdb $ |
44 |
* @version $Id: ConfigurationProxy.java,v 1.16 2002/05/18 18:16:01 tdb Exp $ |
45 |
*/ |
46 |
public class ConfigurationProxy extends Thread { |
47 |
|
48 |
//---FINAL ATTRIBUTES--- |
49 |
|
50 |
/** |
51 |
* The current CVS revision of this class |
52 |
*/ |
53 |
public static final String REVISION = "$Revision: 1.16 $"; |
54 |
|
55 |
/** |
56 |
* The default time to refresh the |
57 |
* configurations - 60 seconds. |
58 |
*/ |
59 |
public final int DEFAULT_REFRESH_TIME = 60; |
60 |
|
61 |
//---STATIC METHODS--- |
62 |
|
63 |
/** |
64 |
* Return a reference to the single class. |
65 |
* Construct it if it does not already exist, otherwise just return the reference. |
66 |
*/ |
67 |
public synchronized static ConfigurationProxy getInstance() { |
68 |
if (_instance == null){ |
69 |
_instance = new ConfigurationProxy(); |
70 |
} |
71 |
return _instance; |
72 |
} |
73 |
|
74 |
//---CONSTRUCTORS--- |
75 |
|
76 |
/** |
77 |
* Construct a ConfigurationProxy |
78 |
* Then starts the auto-updating part (the thread) running. |
79 |
*/ |
80 |
private ConfigurationProxy() { |
81 |
// set the Thread name |
82 |
setName("componentmanager.ConfigurationProxy"); |
83 |
start(); |
84 |
} |
85 |
|
86 |
//---PUBLIC METHODS--- |
87 |
|
88 |
/** |
89 |
* This method runs and waits for a period defined by |
90 |
* ConfigurationProxy.updateTime. Each time it checks all the |
91 |
* configuratons it has loaded to see if their configurations have |
92 |
* changed. If they have, it refreshes the cache with the new configuration |
93 |
* and then instructs the old one to be disconnected from the ORB so that it |
94 |
* can be garbage collected. |
95 |
*/ |
96 |
public void run() { |
97 |
// periodic checks for config changes here |
98 |
|
99 |
while(_running) { |
100 |
int refreshTime = 0; |
101 |
try { |
102 |
refreshTime = Integer.parseInt(getProperty("ConfigurationProxy", "ConfigurationProxy.updateTime")); |
103 |
} catch (NumberFormatException e) { |
104 |
refreshTime = DEFAULT_REFRESH_TIME; |
105 |
_logger.write(toString(), Logger.WARNING, "Erronous ConfigurationProxy.updateTime value in configuration using default of " + refreshTime + " seconds"); |
106 |
} catch (PropertyNotFoundException e) { |
107 |
refreshTime = DEFAULT_REFRESH_TIME; |
108 |
_logger.write(toString(), Logger.WARNING, "ConfigurationProxy.updateTime value unavailable using default of " + refreshTime + " seconds"); |
109 |
} |
110 |
|
111 |
try { |
112 |
Thread.sleep(refreshTime * 1000); |
113 |
} catch (InterruptedException e) { |
114 |
} |
115 |
// ensures we update the list independently of the getProperty method |
116 |
synchronized (this) { |
117 |
Iterator i = _configCache.keySet().iterator(); |
118 |
while(i.hasNext()) { |
119 |
String configName = (String) i.next(); |
120 |
ConfigurationCache cc = (ConfigurationCache) _configCache.get(configName); |
121 |
if (_confman.isModified(cc.getConfig().getFileList(), cc.getConfig().getLastModified())) { |
122 |
// disconnect the old Configuration object from the ORB |
123 |
cc.getConfig().disconnect(); |
124 |
// get the updated Configuration from the manager |
125 |
cc.setConfig(_confman.getConfiguration(configName)); |
126 |
cc.setPropertyCache(new HashMap()); |
127 |
} |
128 |
} |
129 |
} |
130 |
} |
131 |
} |
132 |
|
133 |
/** |
134 |
* This method obtains the configuration from the configuration |
135 |
* cache, if the configuration isn't in the cache, it adds it. |
136 |
* It then asks the configuration for the requested property |
137 |
* name, and then returns that to the caller. |
138 |
* |
139 |
* @param configName the name of the configuration |
140 |
* @param propertyName the name of the property to obtain |
141 |
* |
142 |
* @return the value of the requested property |
143 |
*/ |
144 |
public String getProperty(String configName, String propertyName) throws PropertyNotFoundException { |
145 |
// ensures the run method can update the cache if needed. |
146 |
synchronized (this) { |
147 |
if (!_configCache.containsKey(configName)) { |
148 |
_configCache.put(configName, new ConfigurationCache(_confman.getConfiguration(configName), new HashMap())); |
149 |
} |
150 |
ConfigurationCache cc = (ConfigurationCache) _configCache.get(configName); |
151 |
HashMap propertyCache = cc.getPropertyCache(); |
152 |
Configuration config = cc.getConfig(); |
153 |
if(!propertyCache.containsKey(propertyName)) { |
154 |
// try and get the config |
155 |
// if we can't throw an exception |
156 |
try { |
157 |
propertyCache.put(propertyName, config.getProperty(propertyName)); |
158 |
} catch (org.omg.CORBA.MARSHAL e) { |
159 |
// IMPORTANT - make sure we cache the fact something doesn't exist |
160 |
propertyCache.put(propertyName, null); |
161 |
} |
162 |
} |
163 |
String property = (String) propertyCache.get(propertyName); |
164 |
if(property == null) { |
165 |
// the property cannot be found, so we throw back |
166 |
// a PropertyNotFoundException and give up. |
167 |
throw new PropertyNotFoundException("Could not locate the property \""+propertyName+"\" in the configuration \""+configName+"\""); |
168 |
} |
169 |
return property; |
170 |
} |
171 |
} |
172 |
|
173 |
/** |
174 |
* This method obtains the configuration from the configuration |
175 |
* cache, if the configuration isn't in the cache, it adds it. |
176 |
* It then returns the file list used to build that configuration. |
177 |
* |
178 |
* @param configName the name of the configuration |
179 |
* |
180 |
* @return the semi-coma separated list of filenames |
181 |
*/ |
182 |
public String getFileList(String configName) { |
183 |
// ensures the run method can update the cache if needed. |
184 |
synchronized (this) { |
185 |
if (!_configCache.containsKey(configName)) { |
186 |
_configCache.put(configName, new ConfigurationCache(_confman.getConfiguration(configName), new HashMap())); |
187 |
} |
188 |
ConfigurationCache cc = (ConfigurationCache) _configCache.get(configName); |
189 |
return cc.getConfig().getFileList(); |
190 |
} |
191 |
} |
192 |
|
193 |
/** |
194 |
* This method obtains the configuration from the configuration |
195 |
* cache, if the configuration isn't in the cache, it adds it. |
196 |
* It then returns the time in milliseconds of the most last |
197 |
* modified date of the most recently modified file in the configurations |
198 |
* file list. |
199 |
* |
200 |
* @param configName the name of the configuration |
201 |
* |
202 |
* @return the time in millis |
203 |
*/ |
204 |
public long getLastModified(String configName) { |
205 |
// ensures the run method can update the cache if needed. |
206 |
synchronized (this) { |
207 |
if (!_configCache.containsKey(configName)) { |
208 |
_configCache.put(configName, new ConfigurationCache(_confman.getConfiguration(configName), new HashMap())); |
209 |
} |
210 |
ConfigurationCache cc = (ConfigurationCache) _configCache.get(configName); |
211 |
return cc.getConfig().getLastModified(); |
212 |
} |
213 |
} |
214 |
|
215 |
/** |
216 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
217 |
* method to provide clean logging (every class should have this). |
218 |
* |
219 |
* This uses the uk.org.iscream.cms.server.util.FormatName class |
220 |
* to format the toString() |
221 |
* |
222 |
* @return the name of this class and its CVS revision |
223 |
*/ |
224 |
public String toString() { |
225 |
return FormatName.getName( |
226 |
_name, |
227 |
getClass().getName(), |
228 |
REVISION); |
229 |
} |
230 |
|
231 |
//---PRIVATE METHODS--- |
232 |
|
233 |
//---ACCESSOR/MUTATOR METHODS--- |
234 |
|
235 |
//---ATTRIBUTES--- |
236 |
|
237 |
/** |
238 |
* This is the friendly identifier of the |
239 |
* component this class is running in. |
240 |
* eg, a Filter may be called "filter1", |
241 |
* If this class does not have an owning |
242 |
* component, a name from the configuration |
243 |
* can be placed here. This name could also |
244 |
* be changed to null for utility classes. |
245 |
*/ |
246 |
private String _name = null; |
247 |
|
248 |
/** |
249 |
* This holds a reference to the |
250 |
* system logger that is being used. |
251 |
*/ |
252 |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
253 |
|
254 |
/** |
255 |
* A reference to the reference manager in use |
256 |
*/ |
257 |
private ReferenceManager _refman = ReferenceManager.getInstance(); |
258 |
|
259 |
/** |
260 |
* This holds a reference to the configuration |
261 |
* manager |
262 |
*/ |
263 |
private ConfigurationManager _confman = ReferenceManager.getInstance().getCM(); |
264 |
|
265 |
/** |
266 |
* Holds a list of configuration cache's for every configuration that |
267 |
* has been requested of this proxy. |
268 |
*/ |
269 |
private HashMap _configCache = new HashMap(); |
270 |
|
271 |
/** |
272 |
* Holds the current state of the configuration proxy's thread |
273 |
*/ |
274 |
private boolean _running = true; |
275 |
|
276 |
//---STATIC ATTRIBUTES--- |
277 |
|
278 |
/** |
279 |
* A reference to the single instance of this class |
280 |
*/ |
281 |
private static ConfigurationProxy _instance; |
282 |
|
283 |
} |