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.24 by tdb, Tue May 21 16:47:17 2002 UTC

# Line 1 | Line 1
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.*;
25 > import uk.org.iscream.cms.server.util.*;
26 > import uk.org.iscream.cms.server.componentmanager.*;
27 > import java.net.InetAddress;
28 > import java.net.UnknownHostException;
29   import java.util.*;
30   import java.io.*;
31  
# Line 17 | Line 40 | import java.io.*;
40   *
41   * It also relies on the System.properties to set internal values.
42   *
20 * ###
21 * A point to note is that this class does NOT yet manage
22 * created configurations which may cause memory problems!
23 * ###
24 *
43   * @author  $Author$
44   * @version $Id$
45   */
# Line 44 | Line 62 | class ConfigurationManagerServant extends Configuratio
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();
# Line 79 | Line 97 | class ConfigurationManagerServant extends Configuratio
97       */
98      public Configuration getConfiguration(String source) {
99          _logger.write(toString(), Logger.SYSMSG, "got request for " + source);
100 <        Configuration config = null;
100 >
101          
102          // check to see if we need to reload the system config
103          // because it has changed
# Line 88 | Line 106 | class ConfigurationManagerServant extends Configuratio
106              loadSystemConfig();
107          }
108  
109 <        // we look for this entry in the systemConfig
110 <        String configFile = _systemConfig.getProperty("config." + source);
111 <        _logger.write(toString(), Logger.DEBUG, "looking for config tree in - " + configFile);
112 <
113 <        // if there is an entry
114 <        if (configFile != null) {
109 >        // search config for group membership
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 <                // get the file list of includes etc + the system config
127 <                String fileList = _systemConfigFile + ";" + getIncludedFiles(configFile, "");            
128 <                _logger.write(toString(), Logger.DEBUG, "config tree - " + fileList);
129 <                
130 <                // build the properites here from the filelist....
131 <                StringTokenizer st = new StringTokenizer(fileList, ";");
132 <                
133 <                // some holders for variables
134 <                File currentFile;
135 <                long lastModified, newLastModified;
136 <                Properties properties, prevProperties;
137 <                
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());
126 >                String ip = "Host." + InetAddress.getByName(hostname).getHostAddress();
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  
139 <                // this creates the configuration, all nice, ready to be returned
140 <                ConfigurationServant ref = new ConfigurationServant(properties, fileList, lastModified);
141 <                org.omg.CORBA.Object objRef = _refman.getRootPOA().servant_to_reference(ref);
142 <                config = ConfigurationHelper.narrow(objRef);
143 <
144 <            } catch (Exception e) {
145 <                // not sure what to do here
146 <                // so we just log the error
147 <                _logger.write(toString(), Logger.ERROR, "ERROR - " + e.getMessage());
139 >        Iterator i = groups.iterator();
140 >        String fileList = "";
141 >        while (i.hasNext()) {
142 >            String groupName = (String) i.next();
143 >            _logger.write(toString(), Logger.DEBUG, "looking for config entry for - " + groupName);
144 >            // we look for this entry in the systemConfig
145 >            String configFile = _systemConfig.getProperty("config." + groupName);
146 >            // if there is a config entry then
147 >            if (configFile != null) {
148 >                _logger.write(toString(), Logger.DEBUG, "looking for config tree in - " + configFile);
149 >    
150 >                // get the file list of includes etc + the system config
151 >                String groupFileList = null;
152 >                try {
153 >                    groupFileList = getIncludedFiles(configFile, "") + ";";
154 >                } catch (Exception e) {
155 >                    // not sure what to do here
156 >                    // so we just log the error
157 >                    _logger.write(toString(), Logger.ERROR, "ERROR - " + e);
158 >                }
159 >                if (groupFileList != null) {
160 >                    fileList += groupFileList;
161 >                }
162 >            } else {
163 >                _logger.write(toString(), Logger.DEBUG, "no config entry for - " + groupName);
164              }
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;
165          }
166 +        // add the system config as the final check
167 +        fileList = _systemConfigFile + ";" + fileList;
168 +        _logger.write(toString(), Logger.DEBUG, "config tree - " + fileList);
169          
170 +        // build the configuration
171 +        Configuration config = buildConfiguration(fileList);
172 +        
173          // if this is null at this point, then there will have been an error
174          return config;
175      }
# Line 179 | Line 204 | class ConfigurationManagerServant extends Configuratio
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.server.util.FormatName class
208       * to format the toString()
209       *
210       * @return the name of this class and its CVS revision
# Line 270 | Line 295 | class ConfigurationManagerServant extends Configuratio
295          try {
296              // create the properties for the configuration
297              File systemConfigFile = new File(_configPath, _systemConfigFile);
298 <            Properties systemConfigHolder = new Properties();
299 <            systemConfigHolder.load(new FileInputStream(systemConfigFile));
300 <                        
298 >            _systemConfigHolder = new Properties();
299 >            _systemConfigHolder.load(new FileInputStream(systemConfigFile));
300 >            
301              // create the servant
302 <            ConfigurationServant ref = new ConfigurationServant(systemConfigHolder, _systemConfigFile, systemConfigFile.lastModified());
302 >            ConfigurationServant ref = new ConfigurationServant(_systemConfigHolder, _systemConfigFile, systemConfigFile.lastModified());
303              org.omg.CORBA.Object objRef = _refman.getRootPOA().servant_to_reference(ref);
304              
305              // narrow it to a Configuration
# Line 285 | Line 310 | class ConfigurationManagerServant extends Configuratio
310          }
311      }
312  
313 +    /**
314 +     * Parses the system configuration file
315 +     * for group membership entries.
316 +     *
317 +     * It looks for all entries of group.<name>
318 +     * which contain the given source name
319 +     *
320 +     * @param source the source to find membership for
321 +     *
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 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(groupMatch(source, group)) {
335 +                    groupMembership.add(key.substring(6));
336 +                    _logger.write(toString(), Logger.DEBUG, "group match found for - " + source + " in group - " + key);
337 +                }
338 +            }  
339 +        }
340 +        return groupMembership;
341 +    }
342 +    
343 +    /**
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 +     *
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 match
357 +     *
358 +     * @return if there is a match
359 +     */
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 +            if(StringUtils.wildcardMatch(source, host)) {
366 +                return true;
367 +            }
368 +        }
369 +        // not had a match
370 +        return false;
371 +    }
372 +
373 +    /**
374 +     * Build the properties as a Configuration to be
375 +     * returned to the caller
376 +     *
377 +     * @param fileList the list of files to build the configuration from
378 +     *
379 +     * @return the built Configuration
380 +     */
381 +    private Configuration buildConfiguration(String fileList) {
382 +        Configuration config = null;
383 +
384 +        // if there is an entry
385 +        if (!fileList.equals("")) {
386 +            try {
387 +                
388 +                // build the properites here from the filelist....
389 +                StringTokenizer st = new StringTokenizer(fileList, ";");
390 +                
391 +                // some holders for variables
392 +                File currentFile;
393 +                long lastModified = 0, newLastModified = 0;
394 +                Properties properties = null, prevProperties = null;
395 +                
396 +                // the root of all configurations will be the system config
397 +                // so we need to open the properties of that
398 +                Properties defaultProperties = new Properties();
399 +                
400 +                // This loop then iterates over the file list
401 +                // creates the properties to be passed to the
402 +                // Configuration constructor
403 +                while (st.hasMoreTokens()) {
404 +                    properties = new Properties(defaultProperties);
405 +                    currentFile = new File(_configPath, st.nextToken());
406 +                    newLastModified = currentFile.lastModified();
407 +                    if (newLastModified > lastModified) {
408 +                        lastModified = newLastModified;
409 +                    }
410 +                    properties.load(new FileInputStream(currentFile));
411 +                    defaultProperties = properties;
412 +                }
413 +
414 +                // this creates the configuration, all nice, ready to be returned
415 +                ConfigurationServant ref = new ConfigurationServant(properties, fileList, lastModified);
416 +                org.omg.CORBA.Object objRef = _refman.getRootPOA().servant_to_reference(ref);
417 +                config = ConfigurationHelper.narrow(objRef);
418 +                _logger.write(toString(), Logger.DEBUG, "returning built configuration");
419 +            } catch (Exception e) {
420 +                // not sure what to do here
421 +                // so we just log the error
422 +                _logger.write(toString(), Logger.ERROR, "ERROR - " + e);
423 +            }
424 +            
425 +        // if there isn't an entry for the requested config
426 +        } else {
427 +            _logger.write(toString(), Logger.DEBUG, "no configured config, returning " + _systemConfigFile);
428 +            config = _systemConfig;
429 +        }
430 +        return config;
431 +    }
432 +
433   //---ACCESSOR/MUTATOR METHODS---
434  
435   //---ATTRIBUTES---
# Line 325 | Line 470 | class ConfigurationManagerServant extends Configuratio
470       * An instance of the system config
471       */
472      private Configuration _systemConfig;
473 +    
474 +    /**
475 +     * The system config file represented by a
476 +     * properties object.
477 +     */
478 +    private Properties _systemConfigHolder;
479      
480   //---STATIC ATTRIBUTES---
481      

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines