KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javolution > lang > Configurable


1 /*
2  * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
3  * Copyright (C) 2005 - Javolution (http://javolution.org/)
4  * All rights reserved.
5  *
6  * Permission to use, copy, modify, and distribute this software is
7  * freely granted, provided that this notice is preserved.
8  */

9 package javolution.lang;
10
11 /**
12  * <p> This class centralizes run-time configuration parameters and
13  * allows for custom (plug-in) configuration logic.</p>
14  *
15  * <p> Unlike system properties (or any static mapping), configuration
16  * parameters may not be known until run-time or may change dynamically.
17  * For example, they may depend upon the current run-time platform,
18  * the number of cpus, etc. Configuration parameters may also be retrieved
19  * from external resources such as databases, XML files,
20  * external servers, system properties, etc.</p>
21  *
22  * <p> Regardless of the user configuration constraints, application classes
23  * need only to create static instances of this class and can be
24  * {@link #notifyChange() notified} of any change. A
25  * <code>"public static final"</code> modifier is recommended for
26  * users to identify configurable parameters. For example:[code]
27  * ...
28  * class FastComparator {
29  * public static final Configurable<Boolean> REHASH_SYSTEM_HASHCODE
30  * = new Configurable<Boolean>(isPoorSystemHash()); // Test system hashcode.
31  * ...
32  * class ConcurrentContext {
33  * public static final Configurable<Integer> MAX_CONCURRENCY
34  * = new Configurable<Integer>(Runtime.getRuntime().availableProcessors() - 1);
35  * // No algorithm parallelization on single-processor machines.
36  * ...
37  * class XMLInputFactory {
38  * public static final Configurable<Class> CLASS
39  * = new Configurable<Class>(XMLInputFactory.Default.class);
40  * ...
41  * [/code]</p>
42  *
43  * <p> Configuration can only be performed through a configuration
44  * {@link Logic logic} typically performed at start-up. For example:[code]
45  * private static final Runnable CONFIGURATION = new Configurable.Logic() {
46  * public void run() {
47  * configure(ConcurrentContext.MAX_CONCURRENCY, 0); // No concurrency.
48  * configure(XMLInputFactory.CLASS, MyXMLInputFactory.class);
49  * }
50  * };
51  *
52  * public static main(String[] ...) {
53  * CONFIGURATION.run();
54  * }[/code]
55  * Reconfiguration is allowed at run-time as configurable can be
56  * {@link Configurable#notifyChange() notified} of changes in their
57  * configuration values. Unlike system properties, configurable can be
58  * used in applets or unsigned webstart applications.</p>
59  *
60  * <p> Configuration settings are global (affect all threads). For thread-local
61  * environment settings {@link javolution.context.LocalContext.Reference
62  * LocalContext.Reference} instances are recommended.</p>
63  *
64  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
65  * @version 4.0, September ,4 2006
66  */

67 public class Configurable/*<T>*/ {
68
69     /**
70      * Holds the current value.
71      */

72     private Object JavaDoc/*{T}*/ _value;
73     
74     /**
75      * Default constructor.
76      */

77     public Configurable(Object JavaDoc/*{T}*/ defaultValue) {
78         _value = defaultValue;
79     }
80
81     /**
82      * Returns the current value for this configurable.
83      *
84      * @return the current value.
85      */

86     public Object JavaDoc/*{T}*/ get() {
87         return _value;
88     }
89         
90     /**
91      * Notifies this configurable that its runtime value has been changed.
92      * The default implementation does nothing.
93      */

94     protected void notifyChange() {
95     }
96         
97     /**
98      * This class represents a configuration logic which may be called
99      * at any time (configurating is always thread-safe).
100      */

101     public static abstract class Logic implements Runnable JavaDoc {
102         
103         /**
104          * Sets the run-time value of the specified configurable.
105          *
106          * @param configurable the configurable being configurated.
107          * @param value the new run-time value.
108          */

109         protected final /*<T>*/ void configure(Configurable/*<T>*/ configurable, Object JavaDoc/*{T}*/ value) {
110             configurable._value = value;
111             configurable.notifyChange();
112         }
113     }
114
115 }
Popular Tags