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.1 by ajm, Mon Nov 20 17:11:44 2000 UTC vs.
Revision 1.6 by ajm, Tue Dec 12 18:26:52 2000 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines