ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/server/uk/org/iscream/cms/server/core/ConfigurationManagerServant.java
Revision: 1.5
Committed: Wed Nov 29 21:27:08 2000 UTC (23 years, 6 months ago) by ajm
Branch: MAIN
Branch point for: SERVER_PACKAGEBUILD
Changes since 1.4: +3 -2 lines
Log Message:
Update for package move.

File Contents

# Content
1 //---PACKAGE DECLARATION---
2 package uk.ac.ukc.iscream.core;
3
4 //---IMPORTS---
5 import uk.ac.ukc.iscream.core.*;
6 import java.util.*;
7 import java.io.*;
8 import org.omg.CORBA.*;
9 import org.omg.PortableServer.*;
10
11 /**
12 * This class is essentially a Configuration factory.
13 * This class implements the Configurator IDL and allows
14 * other classes in the system ot obtain their Configuration
15 *
16 * On construction it requires a reference to the RootPOA
17 * to allow it to create Configuration objects to be
18 * returned.
19 *
20 * It also relies on the System.properties to set internal values.
21 *
22 * @author $Author: ajm4 $
23 * @version $Id: ConfigurationManagerServant.java,v 1.4 2000/11/21 23:48:55 ajm4 Exp $
24 */
25 class ConfigurationManagerServant extends ConfigurationManagerPOA {
26
27 //---FINAL ATTRIBUTES---
28
29 /**
30 * The current CVS revision of this class
31 */
32 public final String REVISION = "$Revision: 1.4 $";
33
34 //---STATIC METHODS---
35
36 //---CONSTRUCTORS---
37
38 /**
39 * Creates a new ConfiguratorServant
40 * This class uses the System.properties to set internal values
41 *
42 * @param rootPOARef a reference to the RootPOA
43 * @param logRef a reference to the Logger
44 */
45 ConfigurationManagerServant(POA rootPOARef, Logger logRef) {
46 // assign some local variables
47 _rootPOARef = rootPOARef;
48 _logRef = logRef;
49 _configPath = System.getProperty("uk.ac.ukc.iscream.ConfigurationLocation");
50 _systemConfigFile = System.getProperty("uk.ac.ukc.iscream.SystemConfigurationFile");
51
52 // load the system config
53 loadSystemConfig();
54
55 // log our status
56 _logRef.write(this.toString(), Logger.SYSINIT, "started");
57 _logRef.write(this.toString(), Logger.SYSMSG, "configuration location - " + _configPath);
58 _logRef.write(this.toString(), Logger.SYSMSG, "system configuration file - " + _systemConfigFile);
59 }
60
61 //---PUBLIC METHODS---
62
63 /**
64 * Returns a Configuration object which contains
65 * the configuration data requested by the calling
66 * object.
67 *
68 * This method will look in the systemConfig file
69 * for an entry for this "source", if there is no
70 * entry it returns a refernce to the system
71 * config. If there are any errors in reading the
72 * configuration, it returns null, the caller is
73 * expected to be able to handle this.
74 *
75 * This method also checks to see if the system.conf
76 * file has been updated and reloads its reference if
77 * needed.
78 *
79 * @param source the configuration required
80 * @return the Configuration
81 */
82 public Configuration getConfiguration(String source) {
83 _logRef.write(this.toString(), Logger.SYSMSG, "got request for " + source);
84 Configuration config = null;
85
86 // check to see if we need to reload the system config
87 // because it has changed
88 if (isModified(_systemConfig.getFileList(), _systemConfig.getLastModified())) {
89 _logRef.write(this.toString(), Logger.SYSMSG, "system config changed");
90 loadSystemConfig();
91 }
92
93 // we look for this entry in the systemConfig
94 String configFile = _systemConfig.getProperty("config." + source);
95 _logRef.write(this.toString(), Logger.DEBUG, "looking for config tree in - " + configFile);
96
97 // if there is an entry
98 if (configFile != null) {
99 try {
100 // get the file list of includes etc + the system config
101 String fileList = _systemConfigFile + ";" + getIncludedFiles(configFile, "");
102 _logRef.write(this.toString(), Logger.DEBUG, "config tree - " + fileList);
103
104 // build the properites here from the filelist....
105 StringTokenizer st = new StringTokenizer(fileList, ";");
106
107 // some holders for variables
108 File currentFile;
109 long lastModified, newLastModified;
110 Properties properties, prevProperties;
111
112 // the root of all configurations will be the system config
113 // so we need to open the properties of that
114 Properties defaultProperties = new Properties();
115 currentFile = new File(_configPath, _systemConfigFile);
116 lastModified = currentFile.lastModified();
117 defaultProperties.load(new FileInputStream(currentFile));
118
119 // This loop then iterates over the file list
120 // creates the properties to be passed to the
121 // Configuration constructor
122 do {
123 properties = new Properties(defaultProperties);
124 currentFile = new File(_configPath, st.nextToken());
125 newLastModified = currentFile.lastModified();
126 if (newLastModified > lastModified) {
127 lastModified = newLastModified;
128 }
129 properties.load(new FileInputStream(currentFile));
130 defaultProperties = properties;
131 } while (st.hasMoreTokens());
132
133 // this creates the configuration, all nice, ready to be returned
134 ConfigurationServant ref = new ConfigurationServant(properties, fileList, lastModified, _logRef);
135 org.omg.CORBA.Object objRef = _rootPOARef.servant_to_reference(ref);
136 config = ConfigurationHelper.narrow(objRef);
137
138 } catch (Exception e) {
139 // not sure what to do here
140 System.err.println("CONFIGURATION MANAGER ERROR: " + e);
141 e.printStackTrace(System.out);
142 }
143
144 // if there isn't an entry for the requested config
145 } else {
146 _logRef.write(this.toString(), Logger.DEBUG, "no configured config, returning " + _systemConfigFile);
147 config = _systemConfig;
148 }
149
150 // if this is null at this point, then there will have been an error
151 return config;
152 }
153
154
155 /**
156 * When passed a file list and a current value for the lastModified
157 * of the current configuration, this method compares the value
158 * to the actual value of the configuration files to determine
159 * whether or not the configuration has been modified.
160 *
161 * @param fileList a list of files that the caller uses for configuration
162 * @param lastModified the last modified date of the callers configuration
163 *
164 * @return whether or not the configuration has been modified
165 */
166 public boolean isModified(String fileList, long lastModified) {
167 StringTokenizer st = new StringTokenizer(fileList, ";");
168 long newLastModified;
169 File currentFile;
170 while (st.hasMoreTokens()) {
171 currentFile = new File(_configPath, st.nextToken());
172 newLastModified = currentFile.lastModified();
173 if (newLastModified > lastModified) {
174 return true;
175 }
176 }
177 return false;
178 }
179
180 /**
181 * Overrides the {@link java.lang.Object#toString() Object.toString()}
182 * method to provide clean logging (every class should have this).
183 *
184 * @return the name of this class and its CVS revision
185 */
186 public String toString() {
187 return this.getClass().getName() + "(" + REVISION.substring(11, REVISION.length() - 2) + ")";
188 }
189
190 //---PRIVATE METHODS---
191
192 /**
193 * This is a recursive function private to this class.
194 * It constructs a hierarchy of files as a ";" serperated
195 * string which can be used to read in the configuration.
196 * This function calls itself.
197 *
198 * @param currentFile the current file to be processed
199 * @param readFiles used for recursion purposes only, these are the files it has read so far
200 *
201 * @return the current list that has been constructed
202 *
203 * @throws IOException if there is trouble reading the file
204 * @throws FileNotFoundException is there is trouble finding the file
205 * @throws CircularIncludeException this is if a circular include is detected
206 */
207 private String getIncludedFiles(String currentFile, String readFiles) throws IOException, FileNotFoundException, Exception {
208
209 // check for circular include here
210 if (hasDuplicate(currentFile, readFiles) || currentFile.equals(_systemConfigFile)) {
211 throw new CircularIncludeException(currentFile + " is included more than once");
212 }
213
214 // if there wasn't, we're gonna use this file, so make a note of it as read
215 // (note the use of the ";", this is for the hasDuplicate, function)
216 readFiles = readFiles + currentFile + ";";
217
218 Properties properties = new Properties();
219 properties.load(new FileInputStream(new File(_configPath, currentFile)));
220
221 // get the include property
222 String includes = properties.getProperty("include");
223
224 // if we're the last file with no includes, return our name
225 if (includes == null) {
226 return currentFile;
227
228 // otherwise, recurse over our includes
229 } else {
230 StringTokenizer st = new StringTokenizer(includes, ";");
231 String returnList= "";
232 while (st.hasMoreTokens()) {
233 returnList = getIncludedFiles(st.nextToken(), readFiles) + ";" + returnList;
234 }
235
236 return returnList + currentFile;
237 }
238 }
239
240 /**
241 * This simple method checks to see if a given
242 * file exists in the given list.
243 *
244 * @param file the file to check the list for
245 * @param fileList the list to check
246 *
247 * @return if the given file appeard in the list
248 */
249 private boolean hasDuplicate(String file, String fileList) {
250 StringTokenizer st = new StringTokenizer(fileList, ";");
251 while (st.hasMoreTokens()) {
252 if (file.equals(st.nextToken())) {
253 return true;
254 }
255 }
256 return false;
257 }
258
259 /**
260 * Opens and loads the system configuration into the
261 * local reference _systemConfig
262 */
263 private void loadSystemConfig() {
264 _logRef.write(this.toString(), Logger.SYSMSG, "reloading " + _systemConfigFile);
265 // get a reference to the system config and store it
266 try {
267 // create the properties for the configuration
268 File systemConfigFile = new File(_configPath, _systemConfigFile);
269 Properties systemConfigHolder = new Properties();
270 systemConfigHolder.load(new FileInputStream(systemConfigFile));
271
272 // create the servant
273 ConfigurationServant ref = new ConfigurationServant(systemConfigHolder, _systemConfigFile, systemConfigFile.lastModified(), _logRef);
274 org.omg.CORBA.Object objRef = _rootPOARef.servant_to_reference(ref);
275
276 // narrow it to a Configuration
277 _systemConfig = ConfigurationHelper.narrow(objRef);
278
279 } catch (Exception e) {
280 _logRef.write(this.toString(), Logger.FATAL, "ERROR: " + e.getMessage());
281 }
282 }
283
284 //---ACCESSOR/MUTATOR METHODS---
285
286 //---ATTRIBUTES---
287
288 /**
289 * Local storage of the RootPOA
290 */
291 private POA _rootPOARef;
292
293 /**
294 * Local storage of the Logger
295 */
296 private Logger _logRef;
297
298 /**
299 * The root path to all configurations
300 */
301 private String _configPath;
302
303 /**
304 * The name of the file that contains the system configuration
305 */
306 private String _systemConfigFile;
307
308 /**
309 * An instance of the system config
310 */
311 private Configuration _systemConfig;
312
313 //---STATIC ATTRIBUTES---
314
315 }