1 |
+ |
/* |
2 |
+ |
* i-scream central monitoring system |
3 |
+ |
* http://www.i-scream.org.uk |
4 |
+ |
* Copyright (C) 2000-2002 i-scream |
5 |
+ |
* |
6 |
+ |
* This program is free software; you can redistribute it and/or |
7 |
+ |
* modify it under the terms of the GNU General Public License |
8 |
+ |
* as published by the Free Software Foundation; either version 2 |
9 |
+ |
* of the License, or (at your option) any later version. |
10 |
+ |
* |
11 |
+ |
* This program is distributed in the hope that it will be useful, |
12 |
+ |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
+ |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
+ |
* GNU General Public License for more details. |
15 |
+ |
* |
16 |
+ |
* You should have received a copy of the GNU General Public License |
17 |
+ |
* along with this program; if not, write to the Free Software |
18 |
+ |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
19 |
+ |
*/ |
20 |
+ |
|
21 |
|
//---PACKAGE DECLARATION--- |
22 |
< |
package uk.ac.ukc.iscream.core; |
22 |
> |
package uk.org.iscream.cms.server.core; |
23 |
|
|
24 |
|
//---IMPORTS--- |
25 |
< |
import uk.ac.ukc.iscream.util.*; |
26 |
< |
import uk.ac.ukc.iscream.componentmanager.*; |
25 |
> |
import uk.org.iscream.cms.util.*; |
26 |
> |
import uk.org.iscream.cms.server.componentmanager.*; |
27 |
|
import java.net.InetAddress; |
28 |
|
import java.net.UnknownHostException; |
29 |
|
import java.util.*; |
40 |
|
* |
41 |
|
* It also relies on the System.properties to set internal values. |
42 |
|
* |
23 |
– |
* ### |
24 |
– |
* A point to note is that this class does NOT yet manage |
25 |
– |
* created configurations which may cause memory problems! |
26 |
– |
* ### |
27 |
– |
* |
43 |
|
* @author $Author$ |
44 |
|
* @version $Id$ |
45 |
|
*/ |
62 |
|
*/ |
63 |
|
ConfigurationManagerServant() { |
64 |
|
// assign some local variables |
65 |
< |
_configPath = System.getProperty("uk.ac.ukc.iscream.ConfigurationLocation"); |
66 |
< |
_systemConfigFile = System.getProperty("uk.ac.ukc.iscream.SystemConfigurationFile"); |
65 |
> |
_configPath = System.getProperty("uk.org.iscream.cms.server.ConfigurationLocation"); |
66 |
> |
_systemConfigFile = System.getProperty("uk.org.iscream.cms.server.SystemConfigurationFile"); |
67 |
|
|
68 |
|
// load the system config |
69 |
|
loadSystemConfig(); |
107 |
|
} |
108 |
|
|
109 |
|
// search config for group membership |
110 |
< |
LinkedList groups = getGroupMembership(source); |
110 |
> |
// and obain a list of groups by name |
111 |
> |
LinkedList nameGroups = getGroupMembership(source); |
112 |
> |
// add the hosts individual config to the start of the list |
113 |
> |
nameGroups.addFirst(source); |
114 |
|
|
115 |
+ |
// this list will be used to compile the groupings |
116 |
+ |
LinkedList groups = new LinkedList(); |
117 |
+ |
|
118 |
|
// if we are dealing with a Host.<hostname> request, then we also |
119 |
|
// want to look for ip address details, as configuration entries may relate to it |
120 |
|
// if we can't resolve it, we don't look. |
121 |
+ |
LinkedList ipGroups = null; |
122 |
|
if (source.startsWith("Host.")) { |
123 |
|
// hostname is after Host. |
124 |
|
String hostname = source.substring(5); |
125 |
|
try { |
126 |
|
String ip = "Host." + InetAddress.getByName(hostname).getHostAddress(); |
127 |
< |
LinkedList ipGroups = getGroupMembership(ip); |
128 |
< |
groups.addFirst(ip); |
127 |
> |
ipGroups = getGroupMembership(ip); |
128 |
> |
ipGroups.addFirst(ip); |
129 |
> |
// add to our list of groups |
130 |
|
groups.addAll(ipGroups); |
131 |
|
} catch (UnknownHostException e) { |
132 |
|
_logger.write(toString(), Logger.ERROR, "could not resolve hostname - " + hostname); |
133 |
|
} |
134 |
|
} |
135 |
+ |
|
136 |
+ |
// add the rest of the groups to the end |
137 |
+ |
groups.addAll(nameGroups); |
138 |
|
|
113 |
– |
// add the hosts individual config to the start of the list |
114 |
– |
groups.addFirst(source); |
115 |
– |
|
139 |
|
Iterator i = groups.iterator(); |
140 |
|
String fileList = ""; |
141 |
|
while (i.hasNext()) { |
204 |
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
205 |
|
* method to provide clean logging (every class should have this). |
206 |
|
* |
207 |
< |
* This uses the uk.ac.ukc.iscream.util.FormatName class |
207 |
> |
* This uses the uk.org.iscream.cms.util.FormatName class |
208 |
|
* to format the toString() |
209 |
|
* |
210 |
|
* @return the name of this class and its CVS revision |
322 |
|
* @return the list of groups that this source is a member of |
323 |
|
*/ |
324 |
|
private LinkedList getGroupMembership(String source) { |
325 |
< |
_logger.write(toString(), Logger.DEBUG, "searching group members for - " + source); |
325 |
> |
_logger.write(toString(), Logger.DEBUG, "searching groups for - " + source); |
326 |
|
LinkedList groupMembership = new LinkedList(); |
327 |
|
Iterator i = new TreeSet(_systemConfigHolder.keySet()).iterator(); |
328 |
|
while(i.hasNext()) { |
329 |
|
String key = (String) i.next(); |
330 |
+ |
// look for a key that's a group entry |
331 |
|
if (key.startsWith("group.")) { |
332 |
+ |
// get the list of hosts in the group |
333 |
|
String group = _systemConfig.getProperty(key); |
334 |
< |
// if it is in the group |
310 |
< |
if (group.indexOf(source) != -1) { |
334 |
> |
if(groupMatch(source, group)) { |
335 |
|
groupMembership.add(key.substring(6)); |
336 |
< |
|
313 |
< |
// if there are wildcards in the group |
314 |
< |
} else if (group.indexOf("*") != -1) { |
315 |
< |
// check the wildcards apply to this srce |
316 |
< |
if( wildcardCheck(source, group)) { |
317 |
< |
groupMembership.add(key.substring(6)); |
318 |
< |
} |
336 |
> |
_logger.write(toString(), Logger.DEBUG, "group match found for - " + source + " in group - " + key); |
337 |
|
} |
320 |
– |
|
338 |
|
} |
339 |
|
} |
340 |
|
return groupMembership; |
341 |
< |
} |
342 |
< |
|
341 |
> |
} |
342 |
> |
|
343 |
|
/** |
344 |
< |
* Checks that a given string is not matched within the |
345 |
< |
* given list of strings. eg:<br> |
344 |
> |
* Checks that a given source is matched within the |
345 |
> |
* given list of hosts. For example:<br> |
346 |
|
* <br> |
347 |
|
* Given "stue5de.ukc.ac.uk"<br> |
348 |
|
* And "raptor.ukc.ac.uk;stue*.ukc.ac.uk<br> |
349 |
|
* <br> |
350 |
< |
* This method would return true as there is a match |
351 |
< |
* |
350 |
> |
* This method would return true as there is a match. |
351 |
> |
* |
352 |
> |
* This method will also match if the source is exactly |
353 |
> |
* matched within the group of hosts (ie. no wildcard). |
354 |
> |
* |
355 |
|
* @param source the string to look for |
356 |
< |
* @param group the group to search for a wilcard entry |
356 |
> |
* @param group the group to search for a match |
357 |
|
* |
358 |
|
* @return if there is a match |
359 |
|
*/ |
360 |
< |
private boolean wildcardCheck(String source, String group) { |
341 |
< |
boolean foundMatch = false; |
360 |
> |
public static boolean groupMatch(String source, String group) { |
361 |
|
StringTokenizer st = new StringTokenizer(group, ";"); |
362 |
|
// go through all the hosts in the group |
363 |
|
while (st.hasMoreTokens()) { |
364 |
|
String host = st.nextToken(); |
365 |
< |
// this host has wildcards |
366 |
< |
if(host.indexOf("*") != -1) { |
348 |
< |
StringTokenizer hostst = new StringTokenizer(host, "*"); |
349 |
< |
String part = ""; |
350 |
< |
int index = 0; |
351 |
< |
// the first token will be everything at the start |
352 |
< |
// unless it starts with a wildcard |
353 |
< |
if(!host.startsWith("*")) { |
354 |
< |
part = hostst.nextToken(); |
355 |
< |
if (source.startsWith(part)) { |
356 |
< |
foundMatch = true; |
357 |
< |
index = part.length(); |
358 |
< |
} |
359 |
< |
// all of the start of the string is matched |
360 |
< |
} else { |
361 |
< |
foundMatch = true; |
362 |
< |
} |
363 |
< |
// if the start matched, we want to check the rest... |
364 |
< |
if (foundMatch) { |
365 |
< |
while (hostst.hasMoreTokens()) { |
366 |
< |
part = hostst.nextToken(); |
367 |
< |
// if the next section can't be matched |
368 |
< |
// then this isn't in the source |
369 |
< |
if(source.substring(index).indexOf(part) == -1) { |
370 |
< |
foundMatch = false; |
371 |
< |
// we don't want to look through any more of it |
372 |
< |
break; |
373 |
< |
} else { |
374 |
< |
foundMatch = true; |
375 |
< |
index += source.substring(index).indexOf(part) + part.length(); |
376 |
< |
} |
377 |
< |
} |
378 |
< |
// if we reach here and we've found a match |
379 |
< |
// we want to check that the last part |
380 |
< |
// of the wildcard string is the last part |
381 |
< |
// of the source, if it is, we break out |
382 |
< |
// and finish as we've found a match. |
383 |
< |
// if the end of the wildcard is a *, then |
384 |
< |
// we don't care |
385 |
< |
if (!host.endsWith("*") && foundMatch) { |
386 |
< |
if ((source.endsWith(part))) { |
387 |
< |
_logger.write(toString(), Logger.DEBUG, "wildcard match found for - " + source + " in - " + host); |
388 |
< |
break; |
389 |
< |
// if there is no match, say so so we go round again |
390 |
< |
} else { |
391 |
< |
foundMatch = false; |
392 |
< |
} |
393 |
< |
} else if (foundMatch) { |
394 |
< |
_logger.write(toString(), Logger.DEBUG, "wildcard match found for - " + source + " in - " + host); |
395 |
< |
break; |
396 |
< |
} |
397 |
< |
} |
365 |
> |
if(StringUtils.wildcardMatch(source, host)) { |
366 |
> |
return true; |
367 |
|
} |
368 |
|
} |
369 |
< |
return foundMatch; |
369 |
> |
// not had a match |
370 |
> |
return false; |
371 |
|
} |
402 |
– |
|
372 |
|
|
373 |
|
/** |
374 |
|
* Build the properties as a Configuration to be |