public abstract class Configurable<T> extends Object
An element which is configurable without presupposing how the configuration is done.
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 FONT = Font.decode(System.getProperty("pkg.Document#FONT") != null ? System.getProperty("FONT") : "Arial-BOLD-18"); }
With the following:
class Document { public static final Configurable<Font> FONT = new Configurable<Font>() { @Override protected Font getDefault() { new Font("Arial", Font.BOLD, 18); } }; }
Not only the second example is cleaner, but the actual configuration data can come from anywhere, from system properties (if property defined), OSGi Configuration Admin service, another bundle, etc. Low level code does not need to know.
Configurables may perform any logic upon initialization or
update. Users are notified of configuration events through
the OSGi Configurable.Listener
service.
class Index { // Holds the number of unique preallocated instances (default {@code 1024}). public static final Configurable<Integer> UNIQUE = new Configurable<Integer>() { @Override protected Integer getDefault() { return 1024; } @Override protected Integer initialized(Integer value) { return MathLib.min(value, 65536); // Hard-limiting } @Override protected Integer reconfigured(Integer oldCount, Integer newCount) { throw new UnsupportedOperationException("Unicity reconfiguration not supported."); } } }
Modifier and Type | Class and Description |
---|---|
static interface |
Configurable.Listener
Services to be published by any one interested in being informed of
configurable changes.
|
Modifier and Type | Field and Description |
---|---|
static SecurityContext.Permission<Configurable<?>> |
RECONFIGURE_PERMISSION
Holds the general permission to reconfigure configurable instances
(action
"reconfigure" ). |
Constructor and Description |
---|
Configurable()
Creates a new configurable.
|
Modifier and Type | Method and Description |
---|---|
T |
get()
Returns this configurable value.
|
protected abstract T |
getDefault()
Returns this configurable default value (always different from
null ). |
String |
getName()
Returns this configurable name.
|
SecurityContext.Permission<Configurable<T>> |
getReconfigurePermission()
Returns the permission to configure this instance.
|
protected T |
initialized(T value)
This methods is called when the configurable is initialized.
|
protected T |
parse(String str)
Parses the specified text to return the corresponding value.
|
T |
reconfigure(T newValue)
Reconfigures this instance with the specified value if authorized
by the
SecurityContext . |
protected T |
reconfigured(T oldValue,
T newValue)
This methods is called when the configurable is reconfigured.
|
public static SecurityContext.Permission<Configurable<?>> RECONFIGURE_PERMISSION
"reconfigure"
).
Whether or not that permission is granted depends on the current
SecurityContext
. It is possible that the general permission
to reconfigure a configurable is granted but revoked for a specific
instance. Also, the general permission to reconfigure a configurable
may be revoked but granted only for a specific instance.public Configurable()
name
, the
the parsed
value of the property supersedes the
default
value of this configurable.
For example, running the JVM with
the option -Djavolution.context.ConcurrentContext#CONCURRENCY=0
disables concurrency support.public T get()
public String getName()
UnsupportedOperationException
- if the enclosing class has
multiple configurable static fields.public SecurityContext.Permission<Configurable<T>> getReconfigurePermission()
public T reconfigure(T newValue)
SecurityContext
. This method returns the actual new
value which may be different from the requested new value
(see reconfigured(Object, Object)
).newValue
- the requested new value.SecurityException
- if the permission to reconfigure this
configurable is not granted.UnsupportedOperationException
- if this configurable does not
support dynamic reconfiguration.protected abstract T getDefault()
null
).protected T initialized(T value)
value
- the requested value for this configurable.protected T parse(String str)
TextContext
to retrieve the text format
(based on DefaultTextFormat
class annotation).protected T reconfigured(T oldValue, T newValue)
oldValue
- the old value.newValue
- the requested new value.UnsupportedOperationException
- if this configurable does not
support dynamic reconfiguration.Copyright © 2005-2013 Javolution. All Rights Reserved.