Javolution 6.0.0 java
javolution.lang.Configurable< T > Class Template Referenceabstract
Inheritance diagram for javolution.lang.Configurable< T >:
[legend]
Collaboration diagram for javolution.lang.Configurable< T >:
[legend]

Classes

interface  Listener
 

Public Member Functions

 Configurable ()
 
get ()
 
String getName ()
 
Permission< Configurable< T > > getReconfigurePermission ()
 
reconfigure (T newValue)
 

Static Public Attributes

static Permission< Configurable<?> > RECONFIGURE_PERMISSION
 

Protected Member Functions

abstract T getDefault ()
 
initialized (T value)
 
parse (String str)
 
reconfigured (T oldValue, T newValue)
 

Private Attributes

String name
 
final Permission< Configurable< T > > reconfigurePermission
 
volatile T value
 

Detailed Description

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: [code] class Document { private static final Font FONT = Font.decode(System.getProperty("pkg.Document#FONT") != null ? System.getProperty("FONT") : "Arial-BOLD-18"); }[/code]

With the following: [code] class Document { public static final Configurable<Font> FONT = new Configurable<Font>() { @Override protected Font getDefault() { new Font("Arial", Font.BOLD, 18); } }; }[/code]

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. [code] class Index { // Holds the number of unique preallocated instances (default

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."); }
} }[/code]

Author
Jean-Marie Dautelle
Version
6.0, July 21, 2013

Definition at line 78 of file Configurable.java.

Constructor & Destructor Documentation

◆ Configurable()

Creates a new configurable. If a system property exist for this configurable's 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.

Definition at line 142 of file Configurable.java.

142  {
143  reconfigurePermission = new Permission<Configurable<T>>(
144  Configurable.class, "reconfigure", this);
145  String name = getName();
146  T defaultValue = getDefault();
147  if (name != null) {
148  try { // Checks system properties.
149  String property = System.getProperty(name);
150  if (property != null) {
151  defaultValue = parse(property); // Supersedes.
152  LogContext.debug(name, ", System Properties Value: ",
153  defaultValue);
154  }
155  } catch (SecurityException securityError) {
156  // Ok, current runtime does not allow system properties access.
157  }
158  }
159  this.name = name;
160  this.value = initialized(defaultValue);
161  Object[] listeners = OSGiServices.getConfigurableListeners();
162  for (Object listener : listeners) {
163  ((Listener) listener).configurableInitialized(this, this.value);
164  }
165  }

Member Function Documentation

◆ get()

Returns this configurable value.

Reimplemented in javolution.context.LocalContext.Parameter< T >.

Definition at line 170 of file Configurable.java.

170  {
171  return value;
172  }

Referenced by javolution.context.internal.ConcurrentContextImpl.ConcurrentContextImpl(), javolution.lang.Initializer.initializeLoadedClasses(), javolution.test.Perfometer< T >.measure(), javolution.test.Perfometer< T >.print(), and javolution.test.Perfometer< T >.printDetails().

Here is the caller graph for this function:

◆ getDefault()

abstract T javolution.lang.Configurable< T >.getDefault ( )
abstractprotected

Returns this configurable default value (always different from

null

).

Referenced by javolution.lang.Configurable< javolution.context.LogContext.Level >.Configurable(), and javolution.lang.Configurable< javolution.context.LogContext.Level >.parse().

Here is the caller graph for this function:

◆ getName()

String javolution.lang.Configurable< T >.getName ( )

Returns this configurable name. By convention, the name of the configurable is the name of the static field holding the configurable (e.g. "javolution.context.ConcurrentContext#CONCURRENCY"). This method should be overridden if the enclosing class needs to be impervious to obfuscation or if the enclosing class defines multiple configurable fields.

Exceptions
UnsupportedOperationExceptionif the enclosing class has multiple configurable static fields.

Definition at line 185 of file Configurable.java.

185  {
186  if (name != null)
187  return name; // Already set.
188  Class<?> thisClass = this.getClass();
189  Class<?> enclosingClass = thisClass.getEnclosingClass();
190  String fieldName = null;
191  for (Field field : enclosingClass.getFields()) {
192  if (field.getType().isAssignableFrom(thisClass)) {
193  if (fieldName != null) // Indistinguishable field types.
194  throw new UnsupportedOperationException(
195  "Multiple configurables static fields in the same class" +
196  "requires the Configurable.getName() method to be overriden.");
197  fieldName = field.getName();
198  }
199  }
200  return (fieldName != null) ? enclosingClass.getName() + "#" + fieldName
201  : null;
202  }

Referenced by javolution.lang.Configurable< javolution.context.LogContext.Level >.Configurable(), javolution.osgi.internal.ConfigurableListenerImpl.configurableInitialized(), and javolution.osgi.internal.ConfigurableListenerImpl.configurableReconfigured().

Here is the caller graph for this function:

◆ getReconfigurePermission()

Permission<Configurable<T> > javolution.lang.Configurable< T >.getReconfigurePermission ( )

Returns the permission to configure this instance.

Definition at line 207 of file Configurable.java.

207  {
208  return reconfigurePermission;
209  }

◆ initialized()

T javolution.lang.Configurable< T >.initialized ( value)
protected

This methods is called when the configurable is initialized. Developers may override this method to perform any initialization logic (e.g. input validation).

Parameters
valuethe requested value for this configurable.
Returns
the actual value of this configurable.

Definition at line 252 of file Configurable.java.

252  {
253  return value;
254  }

Referenced by javolution.lang.Configurable< javolution.context.LogContext.Level >.Configurable().

Here is the caller graph for this function:

◆ parse()

T javolution.lang.Configurable< T >.parse ( String  str)
protected

Parses the specified text to return the corresponding value. This method is used to initialize this configurable from system properties. The default implementation uses the TextContext to retrieve the text format (based on DefaultTextFormat class annotation).

Definition at line 264 of file Configurable.java.

264  {
265  Class<? extends T> type = (Class<? extends T>) getDefault().getClass();
266  return TextContext.getFormat(type).parse(str);
267  }

Referenced by javolution.lang.Configurable< javolution.context.LogContext.Level >.Configurable().

Here is the caller graph for this function:

◆ reconfigure()

T javolution.lang.Configurable< T >.reconfigure ( newValue)

Reconfigures this instance with the specified value if authorized by the SecurityContext. This method returns the actual new value which may be different from the requested new value (see reconfigured(Object, Object)).

Parameters
newValuethe requested new value.
Returns
the actual new value.
Exceptions
SecurityExceptionif the permission to reconfigure this configurable is not granted.
UnsupportedOperationExceptionif this configurable does not support dynamic reconfiguration.

Definition at line 224 of file Configurable.java.

224  {
225  SecurityContext.check(reconfigurePermission);
226  synchronized (this) {
227  T oldValue = this.value;
228  this.value = reconfigured(oldValue, newValue);
229  Object[] listeners = OSGiServices.getConfigurableListeners();
230  for (Object listener : listeners) {
231  ((Listener) listener).configurableReconfigured(this, oldValue,
232  this.value);
233  }
234  return this.value;
235  }
236  }

◆ reconfigured()

T javolution.lang.Configurable< T >.reconfigured ( oldValue,
newValue 
)
protected

This methods is called when the configurable is reconfigured. Developers may override this method to perform any reconfiguration logic (e.g. hard limiting values).

Parameters
oldValuethe old value.
newValuethe requested new value.
Returns
the actual new value of this configurable.
Exceptions
UnsupportedOperationExceptionif this configurable does not support dynamic reconfiguration.

Definition at line 280 of file Configurable.java.

280  {
281  return newValue;
282  }

Referenced by javolution.lang.Configurable< javolution.context.LogContext.Level >.reconfigure().

Here is the caller graph for this function:

Member Data Documentation

◆ name

◆ RECONFIGURE_PERMISSION

Permission<Configurable<?> > javolution.lang.Configurable< T >.RECONFIGURE_PERMISSION
static
Initial value:
= new Permission<Configurable<?>>(
Configurable.class, "reconfigure")

Holds the general permission to reconfigure configurable instances (action "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.

Definition at line 115 of file Configurable.java.

◆ reconfigurePermission

◆ value


The documentation for this class was generated from the following file:
javolution.lang.Configurable.name
String name
Definition: Configurable.java:121
javolution.lang.Configurable.parse
T parse(String str)
Definition: Configurable.java:264
javolution.lang.Configurable.value
volatile T value
Definition: Configurable.java:131
javolution.lang.Configurable.getDefault
abstract T getDefault()
javolution.lang.Configurable.initialized
T initialized(T value)
Definition: Configurable.java:252
javolution.lang.Configurable.getName
String getName()
Definition: Configurable.java:185
javolution.lang.Configurable.reconfigured
T reconfigured(T oldValue, T newValue)
Definition: Configurable.java:280
javolution.lang.Configurable.Configurable
Configurable()
Definition: Configurable.java:142
javolution.lang.Configurable.reconfigurePermission
final Permission< Configurable< T > > reconfigurePermission
Definition: Configurable.java:126