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. |
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 |
|
*/ |
41 |
|
/** |
42 |
|
* Creates a new ConfiguratorServant |
43 |
|
* This class uses the System.properties to set internal values |
40 |
– |
* |
41 |
– |
* @param rootPOARef a reference to the RootPOA |
42 |
– |
* @param logRef a reference to the Logger |
44 |
|
*/ |
45 |
< |
ConfigurationManagerServant(POA rootPOARef, Logger logRef) { |
45 |
> |
ConfigurationManagerServant() { |
46 |
|
// assign some local variables |
46 |
– |
_rootPOARef = rootPOARef; |
47 |
– |
_logRef = logRef; |
47 |
|
_configPath = System.getProperty("uk.ac.ukc.iscream.ConfigurationLocation"); |
48 |
|
_systemConfigFile = System.getProperty("uk.ac.ukc.iscream.SystemConfigurationFile"); |
49 |
|
|
51 |
|
loadSystemConfig(); |
52 |
|
|
53 |
|
// log our status |
54 |
< |
_logRef.write(this.toString(), Logger.SYSINIT, "started"); |
55 |
< |
_logRef.write(this.toString(), Logger.SYSMSG, "configuration location - " + _configPath); |
56 |
< |
_logRef.write(this.toString(), Logger.SYSMSG, "system configuration file - " + _systemConfigFile); |
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--- |
78 |
|
* @return the Configuration |
79 |
|
*/ |
80 |
|
public Configuration getConfiguration(String source) { |
81 |
< |
_logRef.write(this.toString(), Logger.SYSMSG, "got request for " + source); |
81 |
> |
_logger.write(toString(), Logger.SYSMSG, "got request for " + source); |
82 |
|
Configuration config = null; |
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 |
< |
_logRef.write(this.toString(), Logger.SYSMSG, "system config changed"); |
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 |
< |
_logRef.write(this.toString(), Logger.DEBUG, "looking for config tree in - " + configFile); |
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 |
< |
// get the file list of includes etc |
99 |
< |
String fileList = getIncludedFiles(configFile); |
100 |
< |
_logRef.write(this.toString(), Logger.DEBUG, "config tree - " + 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, ";"); |
129 |
|
} while (st.hasMoreTokens()); |
130 |
|
|
131 |
|
// this creates the configuration, all nice, ready to be returned |
132 |
< |
ConfigurationServant ref = new ConfigurationServant(properties, fileList, lastModified, _logRef); |
133 |
< |
org.omg.CORBA.Object objRef = _rootPOARef.servant_to_reference(ref); |
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 |
< |
// if there isn't an entry for he requested config |
142 |
> |
// if there isn't an entry for the requested config |
143 |
|
} else { |
144 |
< |
_logRef.write(this.toString(), Logger.DEBUG, "no configured config, returning " + _systemConfigFile); |
144 |
> |
_logger.write(toString(), Logger.DEBUG, "no configured config, returning " + _systemConfigFile); |
145 |
|
config = _systemConfig; |
146 |
|
} |
147 |
|
|
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--- |
200 |
|
* This function calls itself. |
201 |
|
* |
202 |
|
* @param currentFile the current file to be processed |
203 |
< |
* |
203 |
> |
* @param readFiles used for recursion purposes only, these are the files it has read so far |
204 |
> |
* |
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 getIncludedFiles(String currentFile) throws IOException, FileNotFoundException { |
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 |
< |
String nextFile = st.nextToken(); |
215 |
< |
returnList = getIncludedFiles(nextFile) + ";" + returnList; |
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 |
< |
_logRef.write(this.toString(), Logger.SYSMSG, "reloading" + _systemConfigFile); |
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 |
274 |
|
systemConfigHolder.load(new FileInputStream(systemConfigFile)); |
275 |
|
|
276 |
|
// create the servant |
277 |
< |
ConfigurationServant ref = new ConfigurationServant(systemConfigHolder, _systemConfigFile, systemConfigFile.lastModified(), _logRef); |
278 |
< |
org.omg.CORBA.Object objRef = _rootPOARef.servant_to_reference(ref); |
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 |
< |
_logRef.write(this.toString(), Logger.FATAL, "ERROR: " + e.getMessage()); |
284 |
> |
_logger.write(toString(), Logger.FATAL, "ERROR: " + e.getMessage()); |
285 |
|
} |
286 |
|
} |
287 |
|
|
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 |