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.8
Committed: Thu Mar 1 19:18:38 2001 UTC (23 years, 3 months ago) by ajm
Branch: MAIN
Changes since 1.7: +124 -55 lines
Log Message:
Should now support grouping of configurations.
Compiled but untested.

File Contents

# User Rev Content
1 ajm 1.3 //---PACKAGE DECLARATION---
2 ajm 1.5 package uk.ac.ukc.iscream.core;
3 ajm 1.3
4     //---IMPORTS---
5 ajm 1.6 import uk.ac.ukc.iscream.util.*;
6 tdb 1.7 import uk.ac.ukc.iscream.componentmanager.*;
7 ajm 1.3 import java.util.*;
8     import java.io.*;
9    
10     /**
11     * This class is essentially a Configuration factory.
12     * This class implements the Configurator IDL and allows
13     * other classes in the system ot obtain their Configuration
14     *
15     * On construction it requires a reference to the RootPOA
16     * to allow it to create Configuration objects to be
17     * returned.
18     *
19     * It also relies on the System.properties to set internal values.
20     *
21 ajm 1.6 * ###
22     * A point to note is that this class does NOT yet manage
23     * created configurations which may cause memory problems!
24     * ###
25     *
26 ajm 1.8 * @author $Author: tdb1 $
27     * @version $Id: ConfigurationManagerServant.java,v 1.7 2001/01/18 23:10:44 tdb1 Exp $
28 ajm 1.3 */
29     class ConfigurationManagerServant extends ConfigurationManagerPOA {
30    
31     //---FINAL ATTRIBUTES---
32    
33     /**
34     * The current CVS revision of this class
35     */
36 ajm 1.8 public final String REVISION = "$Revision: 1.7 $";
37 ajm 1.3
38     //---STATIC METHODS---
39    
40     //---CONSTRUCTORS---
41    
42     /**
43     * Creates a new ConfiguratorServant
44     * This class uses the System.properties to set internal values
45     */
46 ajm 1.6 ConfigurationManagerServant() {
47 ajm 1.3 // assign some local variables
48     _configPath = System.getProperty("uk.ac.ukc.iscream.ConfigurationLocation");
49     _systemConfigFile = System.getProperty("uk.ac.ukc.iscream.SystemConfigurationFile");
50    
51     // load the system config
52     loadSystemConfig();
53    
54     // log our status
55 ajm 1.6 _logger.write(toString(), Logger.SYSINIT, "started");
56     _logger.write(toString(), Logger.SYSMSG, "configuration location - " + _configPath);
57     _logger.write(toString(), Logger.SYSMSG, "system configuration file - " + _systemConfigFile);
58 ajm 1.3 }
59    
60     //---PUBLIC METHODS---
61    
62     /**
63     * Returns a Configuration object which contains
64     * the configuration data requested by the calling
65     * object.
66     *
67     * This method will look in the systemConfig file
68     * for an entry for this "source", if there is no
69     * entry it returns a refernce to the system
70     * config. If there are any errors in reading the
71     * configuration, it returns null, the caller is
72     * expected to be able to handle this.
73     *
74     * This method also checks to see if the system.conf
75     * file has been updated and reloads its reference if
76     * needed.
77     *
78     * @param source the configuration required
79     * @return the Configuration
80     */
81     public Configuration getConfiguration(String source) {
82 ajm 1.6 _logger.write(toString(), Logger.SYSMSG, "got request for " + source);
83 ajm 1.8
84 ajm 1.3
85     // check to see if we need to reload the system config
86     // because it has changed
87     if (isModified(_systemConfig.getFileList(), _systemConfig.getLastModified())) {
88 ajm 1.6 _logger.write(toString(), Logger.SYSMSG, "system config changed");
89 ajm 1.3 loadSystemConfig();
90     }
91    
92 ajm 1.8 // search config for group membership
93     LinkedList groups = getGroupMembership(source);
94 ajm 1.3
95 ajm 1.8 // add the hosts individual config to the start of the list
96     groups.addFirst(source);
97 ajm 1.3
98 ajm 1.8 Iterator i = groups.iterator();
99     String fileList = "";
100     while (i.hasNext()) {
101     String groupName = (String) i.next();
102    
103     // we look for this entry in the systemConfig
104     String configFile = _systemConfig.getProperty("config." + groupName);
105     _logger.write(toString(), Logger.DEBUG, "looking for config tree in - " + configFile);
106 ajm 1.3
107 ajm 1.8 // get the file list of includes etc + the system config
108     try {
109     fileList += getIncludedFiles(configFile, "");
110 ajm 1.3 } catch (Exception e) {
111     // not sure what to do here
112 ajm 1.6 // so we just log the error
113     _logger.write(toString(), Logger.ERROR, "ERROR - " + e.getMessage());
114 ajm 1.3 }
115     }
116 ajm 1.8 // add the system config as the final check
117     fileList += _systemConfigFile + ";";
118     _logger.write(toString(), Logger.DEBUG, "config tree - " + fileList);
119    
120     // build the configuration
121     Configuration config = buildConfiguration(fileList);
122 ajm 1.3
123     // if this is null at this point, then there will have been an error
124     return config;
125     }
126    
127    
128     /**
129     * When passed a file list and a current value for the lastModified
130     * of the current configuration, this method compares the value
131     * to the actual value of the configuration files to determine
132     * whether or not the configuration has been modified.
133     *
134     * @param fileList a list of files that the caller uses for configuration
135     * @param lastModified the last modified date of the callers configuration
136     *
137     * @return whether or not the configuration has been modified
138     */
139     public boolean isModified(String fileList, long lastModified) {
140     StringTokenizer st = new StringTokenizer(fileList, ";");
141     long newLastModified;
142     File currentFile;
143     while (st.hasMoreTokens()) {
144     currentFile = new File(_configPath, st.nextToken());
145     newLastModified = currentFile.lastModified();
146     if (newLastModified > lastModified) {
147     return true;
148     }
149     }
150     return false;
151     }
152 ajm 1.6
153 ajm 1.3 /**
154     * Overrides the {@link java.lang.Object#toString() Object.toString()}
155     * method to provide clean logging (every class should have this).
156     *
157 ajm 1.6 * This uses the uk.ac.ukc.iscream.util.FormatName class
158     * to format the toString()
159     *
160 ajm 1.3 * @return the name of this class and its CVS revision
161     */
162     public String toString() {
163 ajm 1.6 return FormatName.getName(
164     _name,
165     getClass().getName(),
166     REVISION);
167 ajm 1.3 }
168    
169     //---PRIVATE METHODS---
170    
171     /**
172     * This is a recursive function private to this class.
173     * It constructs a hierarchy of files as a ";" serperated
174     * string which can be used to read in the configuration.
175     * This function calls itself.
176     *
177     * @param currentFile the current file to be processed
178 ajm 1.4 * @param readFiles used for recursion purposes only, these are the files it has read so far
179     *
180 ajm 1.3 * @return the current list that has been constructed
181     *
182     * @throws IOException if there is trouble reading the file
183     * @throws FileNotFoundException is there is trouble finding the file
184 ajm 1.4 * @throws CircularIncludeException this is if a circular include is detected
185 ajm 1.3 */
186 ajm 1.4 private String getIncludedFiles(String currentFile, String readFiles) throws IOException, FileNotFoundException, Exception {
187    
188     // check for circular include here
189     if (hasDuplicate(currentFile, readFiles) || currentFile.equals(_systemConfigFile)) {
190     throw new CircularIncludeException(currentFile + " is included more than once");
191     }
192    
193     // if there wasn't, we're gonna use this file, so make a note of it as read
194     // (note the use of the ";", this is for the hasDuplicate, function)
195     readFiles = readFiles + currentFile + ";";
196    
197 ajm 1.3 Properties properties = new Properties();
198     properties.load(new FileInputStream(new File(_configPath, currentFile)));
199 ajm 1.4
200     // get the include property
201 ajm 1.3 String includes = properties.getProperty("include");
202 ajm 1.4
203     // if we're the last file with no includes, return our name
204 ajm 1.3 if (includes == null) {
205     return currentFile;
206 ajm 1.4
207     // otherwise, recurse over our includes
208 ajm 1.3 } else {
209     StringTokenizer st = new StringTokenizer(includes, ";");
210     String returnList= "";
211     while (st.hasMoreTokens()) {
212 ajm 1.4 returnList = getIncludedFiles(st.nextToken(), readFiles) + ";" + returnList;
213 ajm 1.3 }
214 ajm 1.4
215 ajm 1.3 return returnList + currentFile;
216     }
217     }
218    
219     /**
220 ajm 1.4 * This simple method checks to see if a given
221     * file exists in the given list.
222     *
223     * @param file the file to check the list for
224     * @param fileList the list to check
225     *
226     * @return if the given file appeard in the list
227     */
228     private boolean hasDuplicate(String file, String fileList) {
229     StringTokenizer st = new StringTokenizer(fileList, ";");
230     while (st.hasMoreTokens()) {
231     if (file.equals(st.nextToken())) {
232     return true;
233     }
234     }
235     return false;
236     }
237    
238     /**
239 ajm 1.3 * Opens and loads the system configuration into the
240     * local reference _systemConfig
241     */
242     private void loadSystemConfig() {
243 ajm 1.6 _logger.write(this.toString(), Logger.SYSMSG, "reloading " + _systemConfigFile);
244 ajm 1.3 // get a reference to the system config and store it
245     try {
246     // create the properties for the configuration
247     File systemConfigFile = new File(_configPath, _systemConfigFile);
248 ajm 1.8 _systemConfigHolder = new Properties();
249     _systemConfigHolder.load(new FileInputStream(systemConfigFile));
250    
251 ajm 1.3 // create the servant
252 ajm 1.8 ConfigurationServant ref = new ConfigurationServant(_systemConfigHolder, _systemConfigFile, systemConfigFile.lastModified());
253 ajm 1.6 org.omg.CORBA.Object objRef = _refman.getRootPOA().servant_to_reference(ref);
254 ajm 1.3
255     // narrow it to a Configuration
256     _systemConfig = ConfigurationHelper.narrow(objRef);
257    
258     } catch (Exception e) {
259 ajm 1.6 _logger.write(toString(), Logger.FATAL, "ERROR: " + e.getMessage());
260 ajm 1.3 }
261     }
262    
263 ajm 1.8 /**
264     * Parses the system configuration file
265     * for group membership entries.
266     *
267     * It looks for all entries of group.<name>
268     * which contain the given source name
269     *
270     * @param source the source to find membership for
271     *
272     * @return the list of groups that this source is a member of
273     */
274     private LinkedList getGroupMembership(String source) {
275     LinkedList groupMembership = new LinkedList();
276     Iterator i = new TreeSet(_systemConfigHolder.keySet()).iterator();
277     while(i.hasNext()) {
278     String key = (String) i.next();
279     if (key.startsWith("group.")) {
280     String group = _systemConfig.getProperty(key);
281     if (group.indexOf(source) != -1) {
282     groupMembership.add(key.substring(6));
283     }
284     }
285     }
286     return groupMembership;
287     }
288    
289     /**
290     * Build the properties as a Configuration to be
291     * returned to the caller
292     *
293     * @param fileList the list of files to build the configuration from
294     *
295     * @return the built Configuration
296     */
297     private Configuration buildConfiguration(String fileList) {
298     Configuration config = null;
299    
300     // if there is an entry
301     if (fileList != null) {
302     try {
303    
304     // build the properites here from the filelist....
305     StringTokenizer st = new StringTokenizer(fileList, ";");
306    
307     // some holders for variables
308     File currentFile;
309     long lastModified, newLastModified;
310     Properties properties, prevProperties;
311    
312     // the root of all configurations will be the system config
313     // so we need to open the properties of that
314     Properties defaultProperties = new Properties();
315     currentFile = new File(_configPath, _systemConfigFile);
316     lastModified = currentFile.lastModified();
317     defaultProperties.load(new FileInputStream(currentFile));
318    
319     // This loop then iterates over the file list
320     // creates the properties to be passed to the
321     // Configuration constructor
322     do {
323     properties = new Properties(defaultProperties);
324     currentFile = new File(_configPath, st.nextToken());
325     newLastModified = currentFile.lastModified();
326     if (newLastModified > lastModified) {
327     lastModified = newLastModified;
328     }
329     properties.load(new FileInputStream(currentFile));
330     defaultProperties = properties;
331     } while (st.hasMoreTokens());
332    
333     // this creates the configuration, all nice, ready to be returned
334     ConfigurationServant ref = new ConfigurationServant(properties, fileList, lastModified);
335     org.omg.CORBA.Object objRef = _refman.getRootPOA().servant_to_reference(ref);
336     config = ConfigurationHelper.narrow(objRef);
337    
338     } catch (Exception e) {
339     // not sure what to do here
340     // so we just log the error
341     _logger.write(toString(), Logger.ERROR, "ERROR - " + e.getMessage());
342     }
343    
344     // if there isn't an entry for the requested config
345     } else {
346     _logger.write(toString(), Logger.DEBUG, "no configured config, returning " + _systemConfigFile);
347     config = _systemConfig;
348     }
349     return config;
350     }
351    
352 ajm 1.3 //---ACCESSOR/MUTATOR METHODS---
353    
354     //---ATTRIBUTES---
355    
356     /**
357 ajm 1.6 * This is the friendly identifier of the
358     * component this class is running in.
359     * eg, a Filter may be called "filter1",
360     * If this class does not have an owning
361     * component, a name from the configuration
362     * can be placed here. This name could also
363     * be changed to null for utility classes.
364     */
365     private String _name = Core.NAME;
366    
367     /**
368     * This holds a reference to the
369     * system logger that is being used.
370 ajm 1.3 */
371 ajm 1.6 private Logger _logger = ReferenceManager.getInstance().getLogger();
372 ajm 1.3
373     /**
374 ajm 1.6 * A reference to the reference manager in use
375 ajm 1.3 */
376 ajm 1.6 private ReferenceManager _refman = ReferenceManager.getInstance();
377 ajm 1.3
378     /**
379     * The root path to all configurations
380     */
381     private String _configPath;
382    
383     /**
384     * The name of the file that contains the system configuration
385     */
386     private String _systemConfigFile;
387    
388     /**
389     * An instance of the system config
390     */
391     private Configuration _systemConfig;
392 ajm 1.8
393     /**
394     * The system config file represented by a
395     * properties object.
396     */
397     private Properties _systemConfigHolder;
398 ajm 1.3
399     //---STATIC ATTRIBUTES---
400    
401     }