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.2 by tdb, Tue Nov 21 15:08:42 2000 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 <        try {
43 <            _configPath = System.getProperty("uk.ac.ukc.iscream.ConfigurationLocation");
44 <            _systemConfigFile = System.getProperty("uk.ac.ukc.iscream.SystemConfigurationFile");
45 <            Properties systemConfigHolder = new Properties();
46 <            File systemConfigFile = new File(_configPath, _systemConfigFile);
47 <            systemConfigHolder.load(new FileInputStream(systemConfigFile));
48 <            ConfigurationServant ref = new ConfigurationServant(systemConfigHolder,systemConfigFile.lastModified(), _logRef);
49 <            org.omg.CORBA.Object objRef = _rootPOARef.servant_to_reference(ref);
50 <            _systemConfig = ConfigurationHelper.narrow(objRef);
51 <            _rootPOARef = rootPOARef;
52 <            _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 <        }
62 <    }
63 <
64 < //---PUBLIC METHODS---
65 <    
66 <    /**
67 <     * Returns a Configuration object which contains
68 <     * the configuration data requested by the calling
69 <     * object.
70 <     *
71 <     * If this method returns a null, that is an indication
72 <     * that no configuration currently exists for the requested
73 <     * source.  The caller should handle appropriately.
74 <     *
75 <     * @param source the configuration required
76 <     * @return the Configuration
77 <     */
78 <    public Configuration getConfiguration(String source) {
79 <        _logRef.write(this.toString(), Logger.SYSMSG, "got request for " + source);
80 <        
81 <        Configuration config = null;
82 <        String configFile = _systemConfig.getProperty(source);
83 <        if (configFile != null) {
84 <            try {
85 <                String fileList = getIncludedFiles(configFile, configFile);            
86 <              
87 <            // build the properites here from the filelist....
88 <            StringTokenizer st = new StringTokenizer(fileList, ",");
89 <            } catch (Exception e) {
90 <                // not sure what to do here
91 <                System.err.println("CONFIGURATION MANAGER ERROR: " + e);
92 <                e.printStackTrace(System.out);
93 <            }
94 <            
95 <            // on error...config remains null
96 <        } else {
97 <            config = _systemConfig;
98 <        }
99 <        return config;
100 <    }
101 <    
102 <    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 <   */
149 <    /**
150 <     * When passed a source and a current value for the lastModified
151 <     * of the current configuration, this method compares the value
152 <     * to the actual value of the configuration file to determine
153 <     * whether or not the configuration has been modified.
154 <     *
155 <     * @return whether or not the configuration has been modified
156 <     */
157 <    public boolean isModified(String source, long currentLastModified) {
158 <        return new File(getFileName(source)).lastModified() > currentLastModified;
159 <    }
160 <    
161 <    /**
162 <     * Overrides the {@link java.lang.Object#toString() Object.toString()}
163 <     * method to provide clean logging (every class should have this).
164 <     *
165 <     * @return the name of this class and its CVS revision
166 <     */
167 <    public String toString() {
168 <        return this.getClass().getName() + "(" + REVISION.substring(11, REVISION.length() - 2) + ")";
169 <    }
170 < //---PRIVATE METHODS---
171 <
172 <    /**
173 <     * Constructs the name of a configuration file from a
174 <     * configuration source that is passed to it.
175 <     *
176 <     * @param source the source name
177 <     * @return the filename for the sources configuration
178 <     */
179 <    private String getFileName(String source) {
180 <        return source + ".properties";
181 <    }
182 <
183 < //---ACCESSOR/MUTATOR METHODS---
184 <
185 < //---ATTRIBUTES---
186 <
187 <    /**
188 <     * Local storage of the RootPOA
189 <     */
190 <    private POA _rootPOARef;
191 <    
192 <    /**
193 <     * Local storage of the Logger
194 <     */
195 <    private Logger _logRef;
196 <    
197 <    /**
198 <     * The root path to all configurations
199 <     */
200 <    private String _configPath;
201 <    
202 <    private String _systemConfigFile;
203 <    
204 <    private Configuration _systemConfig;
205 <    
206 < //---STATIC ATTRIBUTES---
207 <    
208 < }
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 > }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines