ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/server/uk/org/iscream/cms/server/componentmanager/ConfigurationProxy.java
Revision: 1.14
Committed: Mon Mar 26 12:10:42 2001 UTC (23 years, 1 month ago) by tdb
Branch: MAIN
CVS Tags: PROJECT_COMPLETION
Changes since 1.13: +4 -3 lines
Log Message:
Closing the javadoc tag helps...

File Contents

# Content
1 //---PACKAGE DECLARATION---
2 package uk.org.iscream.componentmanager;
3
4 //---IMPORTS---
5 import java.util.*;
6 import uk.org.iscream.core.*;
7 import uk.org.iscream.util.*;
8
9 /**
10 * A Configuration Proxy hold configurations caches and
11 * maintains configurations obtained from the Configuration
12 * Manager.
13 *
14 * 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 * @author $Author: ajm4 $
24 * @version $Id: ConfigurationProxy.java,v 1.13 2001/03/26 01:39:41 ajm4 Exp $
25 */
26 public class ConfigurationProxy extends Thread {
27
28 //---FINAL ATTRIBUTES---
29
30 /**
31 * The current CVS revision of this class
32 */
33 public static final String REVISION = "$Revision: 1.13 $";
34
35 /**
36 * The default time to refresh the
37 * configurations - 60 seconds.
38 */
39 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 public synchronized static ConfigurationProxy getInstance() {
48 if (_instance == null){
49 _instance = new ConfigurationProxy();
50 }
51 return _instance;
52 }
53
54 //---CONSTRUCTORS---
55
56 /**
57 * Construct a ConfigurationProxy
58 * Then starts the auto-updating part (the thread) running.
59 */
60 private ConfigurationProxy() {
61 // set the Thread name
62 setName("componentmanager.ConfigurationProxy");
63 start();
64 }
65
66 //---PUBLIC METHODS---
67
68 /**
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 public void run() {
77 // periodic checks for config changes here
78
79 while(_running) {
80 int refreshTime = 0;
81 try {
82 refreshTime = Integer.parseInt(getProperty("ConfigurationProxy", "ConfigurationProxy.updateTime"));
83 } catch (NumberFormatException e) {
84 refreshTime = DEFAULT_REFRESH_TIME;
85 _logger.write(toString(), Logger.WARNING, "Erronous ConfigurationProxy.updateTime value in configuration using default of " + refreshTime + " seconds");
86 } catch (PropertyNotFoundException e) {
87 refreshTime = DEFAULT_REFRESH_TIME;
88 _logger.write(toString(), Logger.WARNING, "ConfigurationProxy.updateTime value unavailable using default of " + refreshTime + " seconds");
89 }
90
91 try {
92 Thread.sleep(refreshTime * 1000);
93 } catch (InterruptedException e) {
94 }
95 // 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 // disconnect the old Configuration object from the ORB
103 cc.getConfig().disconnect();
104 // get the updated Configuration from the manager
105 cc.setConfig(_confman.getConfiguration(configName));
106 cc.setPropertyCache(new HashMap());
107 }
108 }
109 }
110 }
111 }
112
113 /**
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 public String getProperty(String configName, String propertyName) throws PropertyNotFoundException {
125 // 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 // IMPORTANT - make sure we cache the fact something doesn't exist
140 propertyCache.put(propertyName, null);
141 }
142 }
143 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 }
151 }
152
153 /**
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 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 /**
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 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 /**
196 * Overrides the {@link java.lang.Object#toString() Object.toString()}
197 * method to provide clean logging (every class should have this).
198 *
199 * This uses the uk.org.iscream.util.FormatName class
200 * 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 /**
246 * Holds a list of configuration cache's for every configuration that
247 * has been requested of this proxy.
248 */
249 private HashMap _configCache = new HashMap();
250
251 /**
252 * Holds the current state of the configuration proxy's thread
253 */
254 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 }