J avolution v5.2 (J2SE 1.5+)

javolution.lang
Class Configurable<T>

java.lang.Object
  extended by javolution.lang.Configurable<T>

public class Configurable<T>
extends java.lang.Object

This class facilitates separation of concerns between the configuration logic and the application code.

Does your class need to know or has to assume that the configuration is coming from system properties ??

The response is obviously NO!

Let's compare the following examples:

      class Document {
          private static final Font DEFAULT_FONT
              = Font.decode(System.getProperty("DEFAULT_FONT") != null ? System.getProperty("DEFAULT_FONT") : "Arial-BOLD-18");
          ...
      }
With the following (using this class):
      class Document {
          public static final Configurable<Font> DEFAULT_FONT = new Configurable<Font>(new Font("Arial", Font.BOLD, 18));
          ...
      }
Not only the second example is cleaner, but the actual configuration data can come from anywhere (even remotely). Low level code does not need to know.

Furthermore, with the second example the configurable data is automatically documented in the JavaDoc (public). Still only instances of Configurable.Logic may set this data. There is no chance for the user to modify the configuration by accident.

Configurable instances have the same textual representation as their current values. For example:

       public static final Configurable<String> AIRPORT_TABLE
            = new Configurable<String>("Airports");
       ...
       String sql = "SELECT * FROM " + AIRPORT_TABLE // AIRPORT_TABLE.get() is superfluous 
           + " WHERE State = '" + state  + "'";

Unlike system properties (or any static mapping), configuration parameters may not be known until run-time or may change dynamically. They may depend upon the current run-time platform, the number of cpus, etc. Configuration parameters may also be retrieved from external resources such as databases, XML files, external servers, system properties, etc.

      public abstract class FastComparator<T> implements Comparator<T>, Serializable  {     
          public static final Configurable<Boolean> REHASH_SYSTEM_HASHCODE 
              = new Configurable<Boolean>(isPoorSystemHash()); // Test system hashcode. 
      ...
      public abstract class ConcurrentContext extends Context {
          public static final Configurable<Integer> MAXIMUM_CONCURRENCY 
              = new Configurable<Integer>(Runtime.getRuntime().availableProcessors() - 1);
                  // No algorithm parallelization on single-processor machines.
     ...
     public abstract class XMLInputFactory {    
          public static final Configurable<Class<? extends XMLInputFactory>> DEFAULT 
              = new Configurable<Class<? extends XMLInputFactory>>(XMLInputFactory.Default.class);
                  // Default class implementation is a private class. 
     ...
     

Reconfiguration is allowed at run-time as configurable can be notified of changes in their configuration values. Unlike system properties, configurable can be used in applets or unsigned webstart applications.

Here is an example of configuration of a web application from a property file:

      public class Configuration extends Configurable.Logic implements ServletContextListener {
          public void contextInitialized(ServletContextEvent sce) {
              try {
                  ServletContext ctx = sce.getServletContext();
               
                  // Loads properties.
                  Properties properties = new Properties();
                  properties.load(ctx.getResourceAsStream("WEB-INF/config/configuration.properties"));
               
                  // Reads properties superceeding default values.
                  Configurable.read(properties);
                  
              } catch (Exception ex) {
                  LogContext.error(ex);
              }
          }
      }
This listener is registered in the web.xml file:
      <web-app>
          <listener>
              <listener-class>mypackage.Configuration</listener-class>
           </listener>
      </web-app>
The property file contains the full names of the configurables static fields and the textual representation of their new values:
      # File configuration.properties
      javolution.util.FastComparator#REHASH_SYSTEM_HASHCODE = true
      javolution.context.ConcurrentContext#MAXIMUM_CONCURRENCY = 0
      javolution.xml.stream.XMLInputFactory#DEFAULT = com.foo.bar.XMLInputFactoryImpl
      

Configuration settings are global (affect all threads). For thread-local environment settings LocalContext.Reference instances are recommended.

Note: Any type for which a text format is known can be configured from String properties.

Version:
5.1, July 4, 2007
Author:
Jean-Marie Dautelle

Nested Class Summary
static class Configurable.Logic
          This class represents a configuration logic capable of setting Configurable values.
 
Constructor Summary
Configurable(T defaultValue)
          Default constructor.
 
Method Summary
 T get()
          Returns the current value for this configurable.
protected  void notifyChange()
          Notifies this configurable that its runtime value has been changed.
static void read(java.util.Map properties)
          Convenience method to read the specified properties (key/value mapping) and reconfigures accordingly.
 java.lang.String toString()
          Returns the string representation of the value of this configurable.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Configurable

public Configurable(T defaultValue)
Default constructor.

Method Detail

get

public final T get()
Returns the current value for this configurable.

Returns:
the current value.

toString

public java.lang.String toString()
Returns the string representation of the value of this configurable.

Overrides:
toString in class java.lang.Object
Returns:
String.valueOf(this.get())

read

public static void read(java.util.Map properties)
Convenience method to read the specified properties (key/value mapping) and reconfigures accordingly. The configurables are identified by their field names (e.g. "javolution.context.ConcurrentContext#MAXIMUM_CONCURRENCY"). Conversion of String values is performed using TextFormat.getInstance(Class).


notifyChange

protected void notifyChange()
Notifies this configurable that its runtime value has been changed. The default implementation does nothing.


J avolution v5.2 (J2SE 1.5+)

Copyright © 2005 - 2007 Javolution.