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