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.5 by ajm, Wed Nov 29 21:27:08 2000 UTC vs.
Revision 1.10 by ajm, Thu Mar 1 19:36:08 2001 UTC

# Line 2 | Line 2
2   package uk.ac.ukc.iscream.core;
3  
4   //---IMPORTS---
5 < import uk.ac.ukc.iscream.core.*;
5 > import uk.ac.ukc.iscream.util.*;
6 > import uk.ac.ukc.iscream.componentmanager.*;
7   import java.util.*;
8   import java.io.*;
8 import org.omg.CORBA.*;
9 import org.omg.PortableServer.*;
9  
10   /**
11   * This class is essentially a Configuration factory.
# Line 19 | Line 18 | import org.omg.PortableServer.*;
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   */
# Line 38 | Line 42 | class ConfigurationManagerServant extends Configuratio
42      /**
43       * Creates a new ConfiguratorServant
44       * 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
45       */
46 <    ConfigurationManagerServant(POA rootPOARef, Logger logRef) {
46 >    ConfigurationManagerServant() {
47          // assign some local variables
47        _rootPOARef = rootPOARef;
48        _logRef = logRef;
48          _configPath = System.getProperty("uk.ac.ukc.iscream.ConfigurationLocation");
49          _systemConfigFile = System.getProperty("uk.ac.ukc.iscream.SystemConfigurationFile");
50  
# Line 53 | Line 52 | class ConfigurationManagerServant extends Configuratio
52          loadSystemConfig();
53  
54          // log our status
55 <        _logRef.write(this.toString(), Logger.SYSINIT, "started");
56 <        _logRef.write(this.toString(), Logger.SYSMSG, "configuration location - " + _configPath);
57 <        _logRef.write(this.toString(), Logger.SYSMSG, "system configuration file - " + _systemConfigFile);
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---
# Line 80 | Line 79 | class ConfigurationManagerServant extends Configuratio
79       * @return the Configuration
80       */
81      public Configuration getConfiguration(String source) {
82 <        _logRef.write(this.toString(), Logger.SYSMSG, "got request for " + source);
83 <        Configuration config = null;
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 <            _logRef.write(this.toString(), Logger.SYSMSG, "system config changed");
88 >            _logger.write(toString(), Logger.SYSMSG, "system config changed");
89              loadSystemConfig();
90          }
91  
92 <        // we look for this entry in the systemConfig
93 <        String configFile = _systemConfig.getProperty("config." + source);
95 <        _logRef.write(this.toString(), Logger.DEBUG, "looking for config tree in - " + configFile);
92 >        // search config for group membership
93 >        LinkedList groups = getGroupMembership(source);
94  
95 <        // if there is an entry
96 <        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());
95 >        // add the hosts individual config to the start of the list
96 >        groups.addFirst(source);
97  
98 <                // this creates the configuration, all nice, ready to be returned
99 <                ConfigurationServant ref = new ConfigurationServant(properties, fileList, lastModified, _logRef);
100 <                org.omg.CORBA.Object objRef = _rootPOARef.servant_to_reference(ref);
101 <                config = ConfigurationHelper.narrow(objRef);
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 <                System.err.println("CONFIGURATION MANAGER ERROR: " + e);
114 <                e.printStackTrace(System.out);
113 >                // so we just log the error
114 >                _logger.write(toString(), Logger.ERROR, "ERROR - " + e);
115              }
116 <            
117 <        // if there isn't an entry for the requested config
118 <        } else {
146 <            _logRef.write(this.toString(), Logger.DEBUG, "no configured config, returning " + _systemConfigFile);
147 <            config = _systemConfig;
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      }
# Line 176 | Line 153 | class ConfigurationManagerServant extends Configuratio
153          }
154          return false;
155      }
156 <    
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 this.getClass().getName() + "(" + REVISION.substring(11, REVISION.length() - 2) + ")";
167 >        return FormatName.getName(
168 >            _name,
169 >            getClass().getName(),
170 >            REVISION);
171      }
172  
173   //---PRIVATE METHODS---
# Line 261 | Line 244 | class ConfigurationManagerServant extends Configuratio
244       * local reference _systemConfig
245       */
246      private void loadSystemConfig() {
247 <        _logRef.write(this.toString(), Logger.SYSMSG, "reloading " + _systemConfigFile);
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 <            Properties systemConfigHolder = new Properties();
253 <            systemConfigHolder.load(new FileInputStream(systemConfigFile));
254 <                        
252 >            _systemConfigHolder = new Properties();
253 >            _systemConfigHolder.load(new FileInputStream(systemConfigFile));
254 >            
255              // create the servant
256 <            ConfigurationServant ref = new ConfigurationServant(systemConfigHolder, _systemConfigFile, systemConfigFile.lastModified(), _logRef);
257 <            org.omg.CORBA.Object objRef = _rootPOARef.servant_to_reference(ref);
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 <            _logRef.write(this.toString(), Logger.FATAL, "ERROR: " + e.getMessage());
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 <     * Local storage of the RootPOA
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 POA _rootPOARef;
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 <     * Local storage of the Logger
378 >     * A reference to the reference manager in use
379       */
380 <    private Logger _logRef;
380 >    private ReferenceManager _refman = ReferenceManager.getInstance();
381      
382      /**
383       * The root path to all configurations
# Line 309 | Line 393 | class ConfigurationManagerServant extends Configuratio
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      

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines