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
(Generate patch)

Comparing projects/cms/source/server/uk/org/iscream/cms/server/core/ConfigurationManagerServant.java (file contents):
Revision 1.2 by tdb, Tue Nov 21 15:08:42 2000 UTC vs.
Revision 1.10 by ajm, Thu Mar 1 19:36:08 2001 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines