public class Configurable extends 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:[code]
class Document {
private static final Font DEFAULT_FONT
= Font.decode(System.getProperty("DEFAULT_FONT") != null ?
System.getProperty("DEFAULT_FONT") : "Arial-BOLD-18");
...
}[/code]
With the following (using this class):[code]
class Document {
public static final Configurable DEFAULT_FONT
= new Configurable(new Font("Arial", Font.BOLD, 18));
...
}[/code]
Not only the second example is cleaner, but the actual configuration
data can come from anywhere, for example from the OSGI Configuration
Admin package (org.osgi.service.cm
).
Low level code does not need to know.
Configurable instances have the same textual representation as their
current values. For example:[code]
public static final Configurable
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.[code]
public abstract class FastComparator
Dynamic configuration
is allowed/disallowed based
upon the current {SecurityContext}. Configurables are automatically
notified
of
any 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:[code]
public class Configuration 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);
}
}
}[/code]
This listener is registered in the web.xml
file:[code]
Here is an example of reconfiguration from a xml file:[code]
FileInputStream xml = new FileInputStream("D:/configuration.xml");
Configurable.read(xml);[/code]
and the configuration file:[code]
Constructor and Description |
---|
Configurable(Object defaultValue)
Creates a new configurable having the specified default value.
|
Modifier and Type | Method and Description |
---|---|
static void |
configure(Configurable cfg,
Object newValue)
Sets the run-time value of the specified configurable.
|
Object |
get()
Returns the current value for this configurable.
|
Class |
getContainer()
Returns the container class of this configurable (the class
where this configurable is defined as a
public static field. |
Object |
getDefault()
Returns the default value for this configurable.
|
static Configurable |
getInstance(String name)
Returns the configurable instance having the specified name.
|
String |
getName()
Returns the field name of this configurable (for example
"javolution.context.ConcurrentContext#MAXIMUM_CONCURRENCY" )
for ConcurrentContext.MAXIMUM_CONCURRENCY . |
protected void |
notifyChange(Object oldValue,
Object newValue)
Notifies this configurable that its runtime value is going to be changed.
|
static void |
read(InputStream inputStream)
Convenience method to read configurable values from the specified
XML stream.
|
static void |
read(Properties properties)
Convenience method to read the specified properties and reconfigure
accordingly.
|
String |
toString()
Returns the string representation of the value of this configurable.
|
public Configurable(Object defaultValue)
defaultValue
- the default value.IllegalArgumentException
- if defaultValue
is
null
.public Object get()
null
).public Object getDefault()
null
).public Class getContainer()
public static
field.null
if unknown (e.g. J2ME).public String getName()
"javolution.context.ConcurrentContext#MAXIMUM_CONCURRENCY"
)
for ConcurrentContext.MAXIMUM_CONCURRENCY
.null
if the name
of this configurable is unknown (e.g. J2ME).protected void notifyChange(Object oldValue, Object newValue) throws UnsupportedOperationException
oldValue
- the previous value.newValue
- the new value.UnsupportedOperationException
- if dynamic reconfiguration of
this configurable is not allowed (regardless of the security
context).UnsupportedOperationException
public String toString()
public static Configurable getInstance(String name)
ConcurrentContext.MAXIMUM_CONCURRENCY
.
Note: OSGI based framework should ensure that class loaders
of configurable instances are known to the Reflection
utility
class.
name
- the name of the configurable to retrieve.null
if it
cannot be found.public static void configure(Configurable cfg, Object newValue) throws SecurityException
notifyChange(java.lang.Object, java.lang.Object)
is called. This method
raises a SecurityException
if the specified
configurable cannot be reconfigured
.cfg
- the configurable being configured.newValue
- the new run-time value.IllegalArgumentException
- if value
is
null
.SecurityException
- if the specified configurable cannot
be modified.public static void read(Properties properties)
TextFormat.getInstance(Class)
text format}. For example:[code]
javolution.util.FastComparator#REHASH_SYSTEM_HASHCODE = true
javolution.context.ConcurrentContext#MAXIMUM_CONCURRENCY = 0
javolution.xml.stream.XMLInputFactory#CLASS = com.foo.bar.XMLInputFactoryImpl
[/code]
Conversion of String
values to actual object is
performed using TextFormat.getInstance(Class)
.
Note: OSGI based framework should ensure that class loaders
of configurable instances are known to the Reflection
utility
class.
properties
- the properties.public static void read(InputStream inputStream)
Note: OSGI based framework should ensure that class loaders
of configurable instances are known to the Reflection
utility.
inputStream
- the input stream holding the xml configuration.Copyright © 2005–2018 Javolution. All rights reserved.