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.6 by ajm, Tue Dec 12 18:26:52 2000 UTC vs.
Revision 1.18 by tdb, Tue May 29 17:02:34 2001 UTC

# Line 1 | Line 1
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  
# Line 44 | Line 47 | class ConfigurationManagerServant extends Configuratio
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();
# Line 79 | Line 82 | class ConfigurationManagerServant extends Configuratio
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
# Line 88 | Line 91 | class ConfigurationManagerServant extends Configuratio
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      }
# Line 179 | Line 189 | class ConfigurationManagerServant extends Configuratio
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
# Line 270 | Line 280 | class ConfigurationManagerServant extends Configuratio
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
# Line 285 | Line 295 | class ConfigurationManagerServant extends Configuratio
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 group members 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 +                
321 +                // if there are wildcards in the group
322 +                } else if (group.indexOf("*") != -1) {
323 +                    // check the wildcards apply to this srce
324 +                    if( wildcardCheck(source, group)) {
325 +                        groupMembership.add(key.substring(6));
326 +                    }
327 +                }
328 +                
329 +            }  
330 +        }
331 +        return groupMembership;
332 +    }    
333 +
334 +    /**
335 +     * Checks that a given string is not matched within the
336 +     * given list of strings. eg:<br>
337 +     * <br>
338 +     * Given "stue5de.ukc.ac.uk"<br>
339 +     * And   "raptor.ukc.ac.uk;stue*.ukc.ac.uk<br>
340 +     * <br>
341 +     * This method would return true as there is a match
342 +     *
343 +     * @param source the string to look for
344 +     * @param group the group to search for a wilcard entry
345 +     *
346 +     * @return if there is a match
347 +     */
348 +    private boolean wildcardCheck(String source, String group) {
349 +        boolean foundMatch = false;
350 +        StringTokenizer st = new StringTokenizer(group, ";");
351 +        // go through all the hosts in the group
352 +        while (st.hasMoreTokens()) {
353 +            String host = st.nextToken();
354 +            // this host has wildcards
355 +            if(host.indexOf("*") != -1) {
356 +                StringTokenizer hostst = new StringTokenizer(host, "*");
357 +                String part = "";
358 +                int index = 0;
359 +                // the first token will be everything at the start
360 +                // unless it starts with a wildcard
361 +                if(!host.startsWith("*")) {
362 +                    part = hostst.nextToken();
363 +                    if (source.startsWith(part)) {
364 +                        foundMatch = true;
365 +                        index = part.length();
366 +                    }
367 +                // all of the start of the string is matched
368 +                } else {
369 +                    foundMatch = true;
370 +                }
371 +                // if the start matched, we want to check the rest...
372 +                if (foundMatch) {
373 +                    while (hostst.hasMoreTokens()) {
374 +                        part = hostst.nextToken();
375 +                        // if the next section can't be matched
376 +                        // then this isn't in the source
377 +                        if(source.substring(index).indexOf(part) == -1) {
378 +                            foundMatch = false;
379 +                            // we don't want to look through any more of it
380 +                            break;
381 +                        } else {
382 +                            foundMatch = true;
383 +                            index += source.substring(index).indexOf(part) + part.length();
384 +                        }
385 +                    }
386 +                    // if we reach here and we've found a match
387 +                    // we want to check that the last part
388 +                    // of the wildcard string is the last part
389 +                    // of the source, if it is, we break out
390 +                    // and finish as we've found a match.
391 +                    // if the end of the wildcard is a *, then
392 +                    // we don't care
393 +                    if (!host.endsWith("*") && foundMatch) {
394 +                        if ((source.endsWith(part))) {
395 +                            _logger.write(toString(), Logger.DEBUG, "wildcard match found for - " + source + " in - " + host);
396 +                            break;
397 +                        // if there is no match, say so so we go round again
398 +                        } else {
399 +                            foundMatch = false;
400 +                        }
401 +                    } else if (foundMatch) {
402 +                        _logger.write(toString(), Logger.DEBUG, "wildcard match found for - " + source + " in - " + host);
403 +                        break;
404 +                    }
405 +                }
406 +            }
407 +        }
408 +        return foundMatch;
409 +    }
410 +
411 +
412 +    /**
413 +     * Build the properties as a Configuration to be
414 +     * returned to the caller
415 +     *
416 +     * @param fileList the list of files to build the configuration from
417 +     *
418 +     * @return the built Configuration
419 +     */
420 +    private Configuration buildConfiguration(String fileList) {
421 +        Configuration config = null;
422 +
423 +        // if there is an entry
424 +        if (!fileList.equals("")) {
425 +            try {
426 +                
427 +                // build the properites here from the filelist....
428 +                StringTokenizer st = new StringTokenizer(fileList, ";");
429 +                
430 +                // some holders for variables
431 +                File currentFile;
432 +                long lastModified = 0, newLastModified = 0;
433 +                Properties properties = null, prevProperties = null;
434 +                
435 +                // the root of all configurations will be the system config
436 +                // so we need to open the properties of that
437 +                Properties defaultProperties = new Properties();
438 +                
439 +                // This loop then iterates over the file list
440 +                // creates the properties to be passed to the
441 +                // Configuration constructor
442 +                while (st.hasMoreTokens()) {
443 +                    properties = new Properties(defaultProperties);
444 +                    currentFile = new File(_configPath, st.nextToken());
445 +                    newLastModified = currentFile.lastModified();
446 +                    if (newLastModified > lastModified) {
447 +                        lastModified = newLastModified;
448 +                    }
449 +                    properties.load(new FileInputStream(currentFile));
450 +                    defaultProperties = properties;
451 +                }
452 +
453 +                // this creates the configuration, all nice, ready to be returned
454 +                ConfigurationServant ref = new ConfigurationServant(properties, fileList, lastModified);
455 +                org.omg.CORBA.Object objRef = _refman.getRootPOA().servant_to_reference(ref);
456 +                config = ConfigurationHelper.narrow(objRef);
457 +                _logger.write(toString(), Logger.DEBUG, "returning built configuration");
458 +            } catch (Exception e) {
459 +                // not sure what to do here
460 +                // so we just log the error
461 +                _logger.write(toString(), Logger.ERROR, "ERROR - " + e);
462 +            }
463 +            
464 +        // if there isn't an entry for the requested config
465 +        } else {
466 +            _logger.write(toString(), Logger.DEBUG, "no configured config, returning " + _systemConfigFile);
467 +            config = _systemConfig;
468 +        }
469 +        return config;
470 +    }
471 +
472   //---ACCESSOR/MUTATOR METHODS---
473  
474   //---ATTRIBUTES---
# Line 325 | Line 509 | class ConfigurationManagerServant extends Configuratio
509       * An instance of the system config
510       */
511      private Configuration _systemConfig;
512 +    
513 +    /**
514 +     * The system config file represented by a
515 +     * properties object.
516 +     */
517 +    private Properties _systemConfigHolder;
518      
519   //---STATIC ATTRIBUTES---
520      

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines