cern.cmw
Class Config

java.lang.Object
  extended by cern.cmw.Config

public final class Config
extends Object

This class maintains a set of the configuration properties that can be used to customize the CMW behaviour. The property set includes the current system properties and the properties loaded from the CMW configuration file. The location of the configuration file (an absolute or relative file path or a URL) can be defined using the cmw.config or cmw.defaultConfig system properties - see load() for more details. Example: the main(java.lang.String[]) method of this class


Method Summary
static String configFile()
          If loaded, returns a path or a URL of the configuration file that was used, otherwise returns null.
static Properties getProperties()
          Returns the current Config property set.
static String getProperty(String key)
          Returns the value of the property with the specified key.
static void load()
          Loads the CMW configuration file: the file specified by the cmw.config or cmw.defaultConfig property).
static void load(String configFile)
          Adds properties defined in the specified file to the Config property set.
static boolean loaded()
          Returns true if the configuration file has been loaded.
static void main(String[] args)
          For test.
static void setProperty(String key, String value)
          Assigns the value to the property with the specified key.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getProperties

public static Properties getProperties()
Returns the current Config property set.


getProperty

public static String getProperty(String key)
Returns the value of the property with the specified key. This method is a shortcut for getProperties().getProperty(key).


setProperty

public static void setProperty(String key,
                               String value)
Assigns the value to the property with the specified key. If the property is not found in the current Config property set, it will be added to the set with the specified value. This method is a shortcut for getProperties().setProperty(key, value).


loaded

public static boolean loaded()
Returns true if the configuration file has been loaded.


configFile

public static String configFile()
If loaded, returns a path or a URL of the configuration file that was used, otherwise returns null.


load

public static void load()
                 throws InternalException
Loads the CMW configuration file: the file specified by the cmw.config or cmw.defaultConfig property). This method first tries to get the file name from the cmw.config property. If the property is undefined, then it tries cmw.defaultConfig. If both properties are undefined, then the call returns, otherwise it tries to read the file using the load(String file) method.

Throws:
InternalException - thrown if an error occured when reading from the file
See Also:
load(String)

load

public static void load(String configFile)
                 throws InternalException
Adds properties defined in the specified file to the Config property set.

Parameters:
configFile - an absolute or relative file path, or a URL of the configuration file
Throws:
InternalException - thrown if an error occured when reading from the file

main

public static void main(String[] args)
For test. Here is the source code:

   public static void main(String[] args)
   {
      String prefix = null;
      if (args.length > 0) prefix = args[0];

      //
      // Load the CMW configuration file
      //
      try
      {
         Config.load();
      }
      catch(cern.cmw.InternalException ex)
      {
         System.out.println(ex.getMessage());
         System.exit(0);
      }

      if (loaded()) System.out.println("Config loaded from: "+configFile());
      else System.out.println("CMW configuration file not specified");

      //
      // Print properties.
      // If 'prefix' is not specified print all properties, otherwise print
      // only the properties with a key starting with 'prefix'.
      //
      System.out.println("\nPROPERTIES:\n");
      java.util.Properties props = Config.getProperties();
      java.util.Enumeration e = props.propertyNames();
      while(e.hasMoreElements())
      {
         String key = (String)e.nextElement();
         if (prefix != null && !key.startsWith(prefix)) continue;
         System.out.println(key+" = "+Config.getProperty(key));
      }
   }
   
This function can be used to check values of the configuration properties in your current environment. For example: java cern.cmw.Config will print all configuration properties, and java cern.cmw.Config cmw will print properties of the "cmw" group.



Copyright © 2007 CERN. All Rights Reserved.