1 |
|
//---PACKAGE DECLARATION--- |
2 |
< |
package uk.ac.ukc.iscream.core; |
2 |
> |
package uk.org.iscream.cms.server.core; |
3 |
|
|
4 |
|
//---IMPORTS--- |
5 |
< |
import uk.ac.ukc.iscream.util.*; |
5 |
> |
import uk.org.iscream.cms.server.util.*; |
6 |
> |
import uk.org.iscream.cms.server.componentmanager.*; |
7 |
> |
import java.net.InetAddress; |
8 |
> |
import java.net.UnknownHostException; |
9 |
|
import java.util.*; |
10 |
|
import java.io.*; |
11 |
|
|
47 |
|
*/ |
48 |
|
ConfigurationManagerServant() { |
49 |
|
// assign some local variables |
50 |
< |
_configPath = System.getProperty("uk.ac.ukc.iscream.ConfigurationLocation"); |
51 |
< |
_systemConfigFile = System.getProperty("uk.ac.ukc.iscream.SystemConfigurationFile"); |
50 |
> |
_configPath = System.getProperty("uk.org.iscream.cms.server.ConfigurationLocation"); |
51 |
> |
_systemConfigFile = System.getProperty("uk.org.iscream.cms.server.SystemConfigurationFile"); |
52 |
|
|
53 |
|
// load the system config |
54 |
|
loadSystemConfig(); |
82 |
|
*/ |
83 |
|
public Configuration getConfiguration(String source) { |
84 |
|
_logger.write(toString(), Logger.SYSMSG, "got request for " + source); |
85 |
< |
Configuration config = null; |
85 |
> |
|
86 |
|
|
87 |
|
// check to see if we need to reload the system config |
88 |
|
// because it has changed |
91 |
|
loadSystemConfig(); |
92 |
|
} |
93 |
|
|
94 |
< |
// we look for this entry in the systemConfig |
95 |
< |
String configFile = _systemConfig.getProperty("config." + source); |
96 |
< |
_logger.write(toString(), Logger.DEBUG, "looking for config tree in - " + configFile); |
97 |
< |
|
98 |
< |
// if there is an entry |
99 |
< |
if (configFile != null) { |
94 |
> |
// search config for group membership |
95 |
> |
// and obain a list of groups by name |
96 |
> |
LinkedList nameGroups = getGroupMembership(source); |
97 |
> |
// add the hosts individual config to the start of the list |
98 |
> |
nameGroups.addFirst(source); |
99 |
> |
|
100 |
> |
// this list will be used to compile the groupings |
101 |
> |
LinkedList groups = new LinkedList(); |
102 |
> |
|
103 |
> |
// if we are dealing with a Host.<hostname> request, then we also |
104 |
> |
// want to look for ip address details, as configuration entries may relate to it |
105 |
> |
// if we can't resolve it, we don't look. |
106 |
> |
LinkedList ipGroups = null; |
107 |
> |
if (source.startsWith("Host.")) { |
108 |
> |
// hostname is after Host. |
109 |
> |
String hostname = source.substring(5); |
110 |
|
try { |
111 |
< |
// get the file list of includes etc + the system config |
112 |
< |
String fileList = _systemConfigFile + ";" + getIncludedFiles(configFile, ""); |
113 |
< |
_logger.write(toString(), Logger.DEBUG, "config tree - " + fileList); |
114 |
< |
|
115 |
< |
// build the properites here from the filelist.... |
116 |
< |
StringTokenizer st = new StringTokenizer(fileList, ";"); |
117 |
< |
|
118 |
< |
// some holders for variables |
119 |
< |
File currentFile; |
120 |
< |
long lastModified, newLastModified; |
121 |
< |
Properties properties, prevProperties; |
122 |
< |
|
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()); |
111 |
> |
String ip = "Host." + InetAddress.getByName(hostname).getHostAddress(); |
112 |
> |
ipGroups = getGroupMembership(ip); |
113 |
> |
ipGroups.addFirst(ip); |
114 |
> |
// add to our list of groups |
115 |
> |
groups.addAll(ipGroups); |
116 |
> |
} catch (UnknownHostException e) { |
117 |
> |
_logger.write(toString(), Logger.ERROR, "could not resolve hostname - " + hostname); |
118 |
> |
} |
119 |
> |
} |
120 |
> |
|
121 |
> |
// add the rest of the groups to the end |
122 |
> |
groups.addAll(nameGroups); |
123 |
|
|
124 |
< |
// this creates the configuration, all nice, ready to be returned |
125 |
< |
ConfigurationServant ref = new ConfigurationServant(properties, fileList, lastModified); |
126 |
< |
org.omg.CORBA.Object objRef = _refman.getRootPOA().servant_to_reference(ref); |
127 |
< |
config = ConfigurationHelper.narrow(objRef); |
128 |
< |
|
129 |
< |
} catch (Exception e) { |
130 |
< |
// not sure what to do here |
131 |
< |
// so we just log the error |
132 |
< |
_logger.write(toString(), Logger.ERROR, "ERROR - " + e.getMessage()); |
124 |
> |
Iterator i = groups.iterator(); |
125 |
> |
String fileList = ""; |
126 |
> |
while (i.hasNext()) { |
127 |
> |
String groupName = (String) i.next(); |
128 |
> |
_logger.write(toString(), Logger.DEBUG, "looking for config entry for - " + groupName); |
129 |
> |
// we look for this entry in the systemConfig |
130 |
> |
String configFile = _systemConfig.getProperty("config." + groupName); |
131 |
> |
// if there is a config entry then |
132 |
> |
if (configFile != null) { |
133 |
> |
_logger.write(toString(), Logger.DEBUG, "looking for config tree in - " + configFile); |
134 |
> |
|
135 |
> |
// get the file list of includes etc + the system config |
136 |
> |
String groupFileList = null; |
137 |
> |
try { |
138 |
> |
groupFileList = getIncludedFiles(configFile, "") + ";"; |
139 |
> |
} catch (Exception e) { |
140 |
> |
// not sure what to do here |
141 |
> |
// so we just log the error |
142 |
> |
_logger.write(toString(), Logger.ERROR, "ERROR - " + e); |
143 |
> |
} |
144 |
> |
if (groupFileList != null) { |
145 |
> |
fileList += groupFileList; |
146 |
> |
} |
147 |
> |
} else { |
148 |
> |
_logger.write(toString(), Logger.DEBUG, "no config entry for - " + groupName); |
149 |
|
} |
141 |
– |
|
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; |
150 |
|
} |
151 |
+ |
// add the system config as the final check |
152 |
+ |
fileList = _systemConfigFile + ";" + fileList; |
153 |
+ |
_logger.write(toString(), Logger.DEBUG, "config tree - " + fileList); |
154 |
|
|
155 |
+ |
// build the configuration |
156 |
+ |
Configuration config = buildConfiguration(fileList); |
157 |
+ |
|
158 |
|
// if this is null at this point, then there will have been an error |
159 |
|
return config; |
160 |
|
} |
189 |
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
190 |
|
* method to provide clean logging (every class should have this). |
191 |
|
* |
192 |
< |
* This uses the uk.ac.ukc.iscream.util.FormatName class |
192 |
> |
* This uses the uk.org.iscream.cms.server.util.FormatName class |
193 |
|
* to format the toString() |
194 |
|
* |
195 |
|
* @return the name of this class and its CVS revision |
280 |
|
try { |
281 |
|
// create the properties for the configuration |
282 |
|
File systemConfigFile = new File(_configPath, _systemConfigFile); |
283 |
< |
Properties systemConfigHolder = new Properties(); |
284 |
< |
systemConfigHolder.load(new FileInputStream(systemConfigFile)); |
285 |
< |
|
283 |
> |
_systemConfigHolder = new Properties(); |
284 |
> |
_systemConfigHolder.load(new FileInputStream(systemConfigFile)); |
285 |
> |
|
286 |
|
// create the servant |
287 |
< |
ConfigurationServant ref = new ConfigurationServant(systemConfigHolder, _systemConfigFile, systemConfigFile.lastModified()); |
287 |
> |
ConfigurationServant ref = new ConfigurationServant(_systemConfigHolder, _systemConfigFile, systemConfigFile.lastModified()); |
288 |
|
org.omg.CORBA.Object objRef = _refman.getRootPOA().servant_to_reference(ref); |
289 |
|
|
290 |
|
// narrow it to a Configuration |
295 |
|
} |
296 |
|
} |
297 |
|
|
298 |
+ |
/** |
299 |
+ |
* Parses the system configuration file |
300 |
+ |
* for group membership entries. |
301 |
+ |
* |
302 |
+ |
* It looks for all entries of group.<name> |
303 |
+ |
* which contain the given source name |
304 |
+ |
* |
305 |
+ |
* @param source the source to find membership for |
306 |
+ |
* |
307 |
+ |
* @return the list of groups that this source is a member of |
308 |
+ |
*/ |
309 |
+ |
private LinkedList getGroupMembership(String source) { |
310 |
+ |
_logger.write(toString(), Logger.DEBUG, "searching groups for - " + source); |
311 |
+ |
LinkedList groupMembership = new LinkedList(); |
312 |
+ |
Iterator i = new TreeSet(_systemConfigHolder.keySet()).iterator(); |
313 |
+ |
while(i.hasNext()) { |
314 |
+ |
String key = (String) i.next(); |
315 |
+ |
if (key.startsWith("group.")) { |
316 |
+ |
String group = _systemConfig.getProperty(key); |
317 |
+ |
// if it is in the group |
318 |
+ |
if (group.indexOf(source) != -1) { |
319 |
+ |
groupMembership.add(key.substring(6)); |
320 |
+ |
_logger.write(toString(), Logger.DEBUG, "group match found for - " + source + " in - " + key); |
321 |
+ |
|
322 |
+ |
// if there are wildcards in the group |
323 |
+ |
} else if (group.indexOf("*") != -1) { |
324 |
+ |
// check the wildcards apply to this srce |
325 |
+ |
if(StringUtils.wildcardCheck(source, group)) { |
326 |
+ |
groupMembership.add(key.substring(6)); |
327 |
+ |
_logger.write(toString(), Logger.DEBUG, "wildcard group match found for - " + source + " in - " + key); |
328 |
+ |
} |
329 |
+ |
} |
330 |
+ |
|
331 |
+ |
} |
332 |
+ |
} |
333 |
+ |
return groupMembership; |
334 |
+ |
} |
335 |
+ |
|
336 |
+ |
/** |
337 |
+ |
* Build the properties as a Configuration to be |
338 |
+ |
* returned to the caller |
339 |
+ |
* |
340 |
+ |
* @param fileList the list of files to build the configuration from |
341 |
+ |
* |
342 |
+ |
* @return the built Configuration |
343 |
+ |
*/ |
344 |
+ |
private Configuration buildConfiguration(String fileList) { |
345 |
+ |
Configuration config = null; |
346 |
+ |
|
347 |
+ |
// if there is an entry |
348 |
+ |
if (!fileList.equals("")) { |
349 |
+ |
try { |
350 |
+ |
|
351 |
+ |
// build the properites here from the filelist.... |
352 |
+ |
StringTokenizer st = new StringTokenizer(fileList, ";"); |
353 |
+ |
|
354 |
+ |
// some holders for variables |
355 |
+ |
File currentFile; |
356 |
+ |
long lastModified = 0, newLastModified = 0; |
357 |
+ |
Properties properties = null, prevProperties = null; |
358 |
+ |
|
359 |
+ |
// the root of all configurations will be the system config |
360 |
+ |
// so we need to open the properties of that |
361 |
+ |
Properties defaultProperties = new Properties(); |
362 |
+ |
|
363 |
+ |
// This loop then iterates over the file list |
364 |
+ |
// creates the properties to be passed to the |
365 |
+ |
// Configuration constructor |
366 |
+ |
while (st.hasMoreTokens()) { |
367 |
+ |
properties = new Properties(defaultProperties); |
368 |
+ |
currentFile = new File(_configPath, st.nextToken()); |
369 |
+ |
newLastModified = currentFile.lastModified(); |
370 |
+ |
if (newLastModified > lastModified) { |
371 |
+ |
lastModified = newLastModified; |
372 |
+ |
} |
373 |
+ |
properties.load(new FileInputStream(currentFile)); |
374 |
+ |
defaultProperties = properties; |
375 |
+ |
} |
376 |
+ |
|
377 |
+ |
// this creates the configuration, all nice, ready to be returned |
378 |
+ |
ConfigurationServant ref = new ConfigurationServant(properties, fileList, lastModified); |
379 |
+ |
org.omg.CORBA.Object objRef = _refman.getRootPOA().servant_to_reference(ref); |
380 |
+ |
config = ConfigurationHelper.narrow(objRef); |
381 |
+ |
_logger.write(toString(), Logger.DEBUG, "returning built configuration"); |
382 |
+ |
} catch (Exception e) { |
383 |
+ |
// not sure what to do here |
384 |
+ |
// so we just log the error |
385 |
+ |
_logger.write(toString(), Logger.ERROR, "ERROR - " + e); |
386 |
+ |
} |
387 |
+ |
|
388 |
+ |
// if there isn't an entry for the requested config |
389 |
+ |
} else { |
390 |
+ |
_logger.write(toString(), Logger.DEBUG, "no configured config, returning " + _systemConfigFile); |
391 |
+ |
config = _systemConfig; |
392 |
+ |
} |
393 |
+ |
return config; |
394 |
+ |
} |
395 |
+ |
|
396 |
|
//---ACCESSOR/MUTATOR METHODS--- |
397 |
|
|
398 |
|
//---ATTRIBUTES--- |
433 |
|
* An instance of the system config |
434 |
|
*/ |
435 |
|
private Configuration _systemConfig; |
436 |
+ |
|
437 |
+ |
/** |
438 |
+ |
* The system config file represented by a |
439 |
+ |
* properties object. |
440 |
+ |
*/ |
441 |
+ |
private Properties _systemConfigHolder; |
442 |
|
|
443 |
|
//---STATIC ATTRIBUTES--- |
444 |
|
|