Java Doc for Configurable.java in  » Development » Javolution » javolution » lang » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Development » Javolution » javolution.lang 
Source Cross Reference  Class Diagram Java Document (Java Doc) 


java.lang.Object
   javolution.lang.Configurable

Configurable
public class Configurable (Code)

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 (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 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:[code] public static final Configurable AIRPORT_TABLE = new Configurable("Airports"); ... String sql = "SELECT * FROM " + AIRPORT_TABLE // AIRPORT_TABLE.get() is superfluous + " WHERE State = '" + state + "'";[/code]

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 implements Comparator, Serializable { public static final Configurable REHASH_SYSTEM_HASHCODE = new Configurable(isPoorSystemHash()); // Test system hashcode. ... public abstract class ConcurrentContext extends Context { public static final Configurable MAXIMUM_CONCURRENCY = new Configurable(Runtime.getRuntime().availableProcessors() - 1); // No algorithm parallelization on single-processor machines. ... public abstract class XMLInputFactory { public static final Configurable> DEFAULT = new Configurable>(XMLInputFactory.Default.class); // Default class implementation is a private class. ... [/code]

Reconfiguration is allowed at run-time as configurable can be Configurable.notifyChange 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:[code] 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); } } }[/code] This listener is registered in the web.xml file:[code] mypackage.Configuration [/code] The property file contains the full names of the configurables static fields and the textual representation of their new values:[code] # 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 [/code]

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

Note: Any type for which a text format is TextFormat.getInstance(Class) known can be configured from String properties.


author:
   Jean-Marie Dautelle
version:
   5.1, July 4, 2007

Inner Class :abstract public static class Logic

Field Summary
final static  LogicLOGIC
     Convenience method to read the specified properties (key/value mapping) and reconfigures accordingly.

Constructor Summary
public  Configurable(Object defaultValue)
     Default constructor.

Method Summary
final public  Objectget()
     Returns the current value for this configurable.
protected  voidnotifyChange()
     Notifies this configurable that its runtime value has been changed. The default implementation does nothing.
public  StringtoString()
     Returns the string representation of the value of this configurable.

Field Detail
LOGIC
final static Logic LOGIC(Code)
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 javolution.text.TextFormat.getInstance(Class) . public static void read(j2me.util.Map properties) { j2me.util.Iterator i = properties.entrySet().iterator(); while (i.hasNext()) { j2me.util.Map.Entry entry = (j2me.util.Map.Entry) i.next(); String key = String.valueOf(entry.getKey()); Object value = entry.getValue(); try { int sep = key.indexOf('#'); if (sep < 0) // Not a configurable property. continue; // Found a configurable property being superseded. javolution.context.LogContext.info("Configure " + key + " to " + value); String className = key.substring(0, sep); String fieldName = key.substring(sep + 1); Class cls = Reflection.getClass(className); Configurable cfg = (Configurable) cls.getDeclaredField( fieldName).get(null); Object previous = cfg.get(); if ((previous == null) || !(value instanceof String)) { // No automatic conversion, use value directly. LOGIC.configure(cfg, value); continue; } String str = (String) value; if (previous instanceof String) { LOGIC.configure(cfg, value); continue; } javolution.text.TextFormat format = javolution.text.TextFormat.getInstance(previous.getClass()); if (format != null) { LOGIC.configure(cfg, format.parse(javolution.Javolution.j2meToCharSeq(str))); continue; } javolution.context.LogContext.warning(javolution.text.Text.valueOf( "No text format found for type " + previous.getClass() + " (" + key + "), please register the text format" + " using TextFormat.setInstance(Class, TextFormat) static method")); } catch (Exception ex) { javolution.context.LogContext.warning(javolution.text.Text.valueOf("Cannot set property " + key + "(" + ex.toString() + ")")); } } } /*




Constructor Detail
Configurable
public Configurable(Object defaultValue)(Code)
Default constructor.




Method Detail
get
final public Object get()(Code)
Returns the current value for this configurable. the current value.



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



toString
public String toString()(Code)
Returns the string representation of the value of this configurable. String.valueOf(this.get())



Methods inherited from java.lang.Object
native protected Object clone() throws CloneNotSupportedException(Code)(Java Doc)
public boolean equals(Object obj)(Code)(Java Doc)
protected void finalize() throws Throwable(Code)(Java Doc)
final native public Class getClass()(Code)(Java Doc)
native public int hashCode()(Code)(Java Doc)
final native public void notify()(Code)(Java Doc)
final native public void notifyAll()(Code)(Java Doc)
public String toString()(Code)(Java Doc)
final native public void wait(long timeout) throws InterruptedException(Code)(Java Doc)
final public void wait(long timeout, int nanos) throws InterruptedException(Code)(Java Doc)
final public void wait() throws InterruptedException(Code)(Java Doc)

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.