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.*; |
6 |
< |
import uk.ac.ukc.iscream.componentmanager.*; |
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 |
|
|
20 |
|
* |
21 |
|
* It also relies on the System.properties to set internal values. |
22 |
|
* |
21 |
– |
* ### |
22 |
– |
* A point to note is that this class does NOT yet manage |
23 |
– |
* created configurations which may cause memory problems! |
24 |
– |
* ### |
25 |
– |
* |
23 |
|
* @author $Author$ |
24 |
|
* @version $Id$ |
25 |
|
*/ |
42 |
|
*/ |
43 |
|
ConfigurationManagerServant() { |
44 |
|
// assign some local variables |
45 |
< |
_configPath = System.getProperty("uk.ac.ukc.iscream.ConfigurationLocation"); |
46 |
< |
_systemConfigFile = System.getProperty("uk.ac.ukc.iscream.SystemConfigurationFile"); |
45 |
> |
_configPath = System.getProperty("uk.org.iscream.cms.server.ConfigurationLocation"); |
46 |
> |
_systemConfigFile = System.getProperty("uk.org.iscream.cms.server.SystemConfigurationFile"); |
47 |
|
|
48 |
|
// load the system config |
49 |
|
loadSystemConfig(); |
87 |
|
} |
88 |
|
|
89 |
|
// search config for group membership |
90 |
< |
LinkedList groups = getGroupMembership(source); |
91 |
< |
|
90 |
> |
// and obain a list of groups by name |
91 |
> |
LinkedList nameGroups = getGroupMembership(source); |
92 |
|
// add the hosts individual config to the start of the list |
93 |
< |
groups.addFirst(source); |
93 |
> |
nameGroups.addFirst(source); |
94 |
> |
|
95 |
> |
// this list will be used to compile the groupings |
96 |
> |
LinkedList groups = new LinkedList(); |
97 |
> |
|
98 |
> |
// if we are dealing with a Host.<hostname> request, then we also |
99 |
> |
// want to look for ip address details, as configuration entries may relate to it |
100 |
> |
// if we can't resolve it, we don't look. |
101 |
> |
LinkedList ipGroups = null; |
102 |
> |
if (source.startsWith("Host.")) { |
103 |
> |
// hostname is after Host. |
104 |
> |
String hostname = source.substring(5); |
105 |
> |
try { |
106 |
> |
String ip = "Host." + InetAddress.getByName(hostname).getHostAddress(); |
107 |
> |
ipGroups = getGroupMembership(ip); |
108 |
> |
ipGroups.addFirst(ip); |
109 |
> |
// add to our list of groups |
110 |
> |
groups.addAll(ipGroups); |
111 |
> |
} catch (UnknownHostException e) { |
112 |
> |
_logger.write(toString(), Logger.ERROR, "could not resolve hostname - " + hostname); |
113 |
> |
} |
114 |
> |
} |
115 |
> |
|
116 |
> |
// add the rest of the groups to the end |
117 |
> |
groups.addAll(nameGroups); |
118 |
|
|
119 |
|
Iterator i = groups.iterator(); |
120 |
|
String fileList = ""; |
141 |
|
} |
142 |
|
} else { |
143 |
|
_logger.write(toString(), Logger.DEBUG, "no config entry for - " + groupName); |
144 |
+ |
} |
145 |
|
} |
146 |
|
// add the system config as the final check |
147 |
< |
fileList += _systemConfigFile + ";"; |
147 |
> |
fileList = _systemConfigFile + ";" + fileList; |
148 |
|
_logger.write(toString(), Logger.DEBUG, "config tree - " + fileList); |
149 |
|
|
150 |
|
// build the configuration |
184 |
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
185 |
|
* method to provide clean logging (every class should have this). |
186 |
|
* |
187 |
< |
* This uses the uk.ac.ukc.iscream.util.FormatName class |
187 |
> |
* This uses the uk.org.iscream.cms.server.util.FormatName class |
188 |
|
* to format the toString() |
189 |
|
* |
190 |
|
* @return the name of this class and its CVS revision |
302 |
|
* @return the list of groups that this source is a member of |
303 |
|
*/ |
304 |
|
private LinkedList getGroupMembership(String source) { |
305 |
+ |
_logger.write(toString(), Logger.DEBUG, "searching groups for - " + source); |
306 |
|
LinkedList groupMembership = new LinkedList(); |
307 |
|
Iterator i = new TreeSet(_systemConfigHolder.keySet()).iterator(); |
308 |
|
while(i.hasNext()) { |
309 |
|
String key = (String) i.next(); |
310 |
+ |
// look for a key that's a group entry |
311 |
|
if (key.startsWith("group.")) { |
312 |
+ |
// get the list of hosts in the group |
313 |
|
String group = _systemConfig.getProperty(key); |
314 |
< |
if (group.indexOf(source) != -1) { |
314 |
> |
if(groupMatch(source, group)) { |
315 |
|
groupMembership.add(key.substring(6)); |
316 |
+ |
_logger.write(toString(), Logger.DEBUG, "group match found for - " + source + " in group - " + key); |
317 |
|
} |
318 |
|
} |
319 |
|
} |
320 |
|
return groupMembership; |
321 |
< |
} |
321 |
> |
} |
322 |
> |
|
323 |
> |
/** |
324 |
> |
* Checks that a given source is matched within the |
325 |
> |
* given list of hosts. For example:<br> |
326 |
> |
* <br> |
327 |
> |
* Given "stue5de.ukc.ac.uk"<br> |
328 |
> |
* And "raptor.ukc.ac.uk;stue*.ukc.ac.uk<br> |
329 |
> |
* <br> |
330 |
> |
* This method would return true as there is a match. |
331 |
> |
* |
332 |
> |
* This method will also match if the source is exactly |
333 |
> |
* matched within the group of hosts (ie. no wildcard). |
334 |
> |
* |
335 |
> |
* @param source the string to look for |
336 |
> |
* @param group the group to search for a match |
337 |
> |
* |
338 |
> |
* @return if there is a match |
339 |
> |
*/ |
340 |
> |
public static boolean groupMatch(String source, String group) { |
341 |
> |
StringTokenizer st = new StringTokenizer(group, ";"); |
342 |
> |
// go through all the hosts in the group |
343 |
> |
while (st.hasMoreTokens()) { |
344 |
> |
String host = st.nextToken(); |
345 |
> |
if(StringUtils.wildcardMatch(source, host)) { |
346 |
> |
return true; |
347 |
> |
} |
348 |
> |
} |
349 |
> |
// not had a match |
350 |
> |
return false; |
351 |
> |
} |
352 |
|
|
353 |
|
/** |
354 |
|
* Build the properties as a Configuration to be |
370 |
|
|
371 |
|
// some holders for variables |
372 |
|
File currentFile; |
373 |
< |
long lastModified, newLastModified; |
374 |
< |
Properties properties, prevProperties; |
373 |
> |
long lastModified = 0, newLastModified = 0; |
374 |
> |
Properties properties = null, prevProperties = null; |
375 |
|
|
376 |
|
// the root of all configurations will be the system config |
377 |
|
// so we need to open the properties of that |
378 |
|
Properties defaultProperties = new Properties(); |
323 |
– |
currentFile = new File(_configPath, _systemConfigFile); |
324 |
– |
lastModified = currentFile.lastModified(); |
325 |
– |
defaultProperties.load(new FileInputStream(currentFile)); |
379 |
|
|
380 |
|
// This loop then iterates over the file list |
381 |
|
// creates the properties to be passed to the |
382 |
|
// Configuration constructor |
383 |
< |
do { |
383 |
> |
while (st.hasMoreTokens()) { |
384 |
|
properties = new Properties(defaultProperties); |
385 |
|
currentFile = new File(_configPath, st.nextToken()); |
386 |
|
newLastModified = currentFile.lastModified(); |
389 |
|
} |
390 |
|
properties.load(new FileInputStream(currentFile)); |
391 |
|
defaultProperties = properties; |
392 |
< |
} while (st.hasMoreTokens()); |
392 |
> |
} |
393 |
|
|
394 |
|
// this creates the configuration, all nice, ready to be returned |
395 |
|
ConfigurationServant ref = new ConfigurationServant(properties, fileList, lastModified); |
396 |
|
org.omg.CORBA.Object objRef = _refman.getRootPOA().servant_to_reference(ref); |
397 |
|
config = ConfigurationHelper.narrow(objRef); |
398 |
< |
|
398 |
> |
_logger.write(toString(), Logger.DEBUG, "returning built configuration"); |
399 |
|
} catch (Exception e) { |
400 |
|
// not sure what to do here |
401 |
|
// so we just log the error |