getConfiguration
which must be
given the name of a component. The name of the component can be any String
which can be used to identify it within easyconf.
ComponentConfiguration conf = ConfReader.getConfiguration("my-component");
The result is an object with the configuration of the given
component. The component configuration contains a properties based configuration
and a XML based configuration. Let's start with the first one. Note that we'll asume from
now on that we'll be working with the configuration of a component called
my-component.
ComponentConfiguration
you can just read the property values which are automatically converted to
integers, booleans, lists, etc. Following are two examples of how to read typed
properties:
List portlets = conf.getProperties().getList("static.portlets");
boolean disable = conf.getProperties().getBoolean("registration.disable");
On the other hand the XML configuration can be converted to any object
by filling a digester rules file. The XML configuration of the previous example
will be read from a file named my-component.xml and its digester
rules from my-component.digesterRules.xml. The XML parsing converts
the configuration to a set of user defined object classes. For example
imagine that we want to configure some databases specifying it's tables and
types of tables. We define two classes: DatabaseConf and
Table. Then we write the XML configuration in
my-component.xml, for example:
<database>
<tables>
<table tableType="type1"/>
</tables>
</database>
Next we write the Digester rules in my-component.digesterRules.xml:
<!DOCTYPE digester-rules PUBLIC
"-//Jakarta Apache //DTD digester-rules XML V1.0//EN"
"digester-rules.dtd">
<digester-rules>
<pattern value="database">
<object-create-rule classname="com.foo.bar.DatabaseConf"/>
<pattern value="tables/table">
<object-create-rule classname="com.foo.bar.Table"/>
<set-properties-rule/>
<set-next-rule methodname="addTable" paramtype="com.foo.bar.Table"/>
</pattern>
</pattern>
</digester-rules>
Finally, just one line of code is necesary to obtain the objects which
have resulted from parsing the configuration. Easyconf calls the result an
object graph because it returns one object which can contain other objects
inside, which can contain even more objects, etc. Here is an example of code
of retrieving and using the object graph of the previous XML configuration:
DatabaseConf dbconf = (DatabaseConf) conf.getConfigurationObject();
Table firstTable = dbconf.getTables().get(0);
String tableType = firstTable.getTableType();
For more information about how to write the rules files check the digester xml rules documentation A variant of this example is using properties from the properties based configuration in the XML file. For example:
<database>
<tables>
<table tableType="${table.type1}"/>
</tables>
</database>
Where table.type1 is defined in a properties file as:
table.type1=type1This allows to change this value for different environments.
In a next section is a more detailed description of a component configurations.
<%@ taglib uri="/WEB-INF/tld/easyconf.tld" prefix="easyconf" %>
<easyconf:property id="static_portlets"
component="xpression-ui"
property="static.portlets"
type="java.util.List"/>
<logic:iterate id="item" name="static_portlets">
<bean:write name="item"/>
</logic:iterate>
<easyconf:property id="registration_disabled"
component="my-component"
property="registration.disabled"/>
<logic:equal name="registration_disabled" value="true">
<bean:message key="registration.disabled"/>
</logic:equal>
Los valores soportados en el parametro type son: java.util.List, java.lang.Integer , java.lang.String
java.lang.Double, java.lang.Float, java.lang.Byte
java.math.BigDecimal, java.lang.BigInteger, java.lang.Boolean
java.lang.Short y java.lang.Long.
ComponentConfiguration which is
composed of:
ComponentProperties
object from serveral 'properties' files.
${componentName}.xml: Contains the specific XML with the
configuration of the component.
${componentName}.digesterRules.xml: Contains the rules which
the parser should use to convert the XML file to objects
${variableName}.
For example, the following XML fragment can be used:
<welcome-msg>
Bienvenido a ${server.name}
</welcome-msg>
server.name as explained next.
include-and-override. This property is multivaluated
so that several files can be included. In case of collision of values among the
included files the ones specified last will override the previous ones.
A usual pattern is to include the following properties in
global-configuration.properties:
include-and-override=global-configuration-env.properties
include-and-override=global-configuration-prj.properties
java ......... -Dinstallation-dir=/opt/application
readme.file.path=${installation-dir}/README
java ......... -Dconfiguration-environment=production
global-configuration.properties you can set the following
property:
include-and-override=global-configuration-${configuration-environment}.properties
global-configuration-production.properties. Using this pattern
you can have an application EAR, WAR or JAR which contains the configuration
for all the environments and then set a system property when running the
application to select which configuration should be used.
As usual, the referenced files that do not exist they will be silently ignored.
If the JVM property is not defined no file will be loaded.
long-property=5
boolean-poroperty=true
List. For example:
valid-values=one,two,three,four
Important:
if the value of a property contains a comma but you do not want
it converted to a list the comma should be scaped with a double
slash. For example:
a-property=Once upon a time\\, there was a princess...
database-type=oracle
sql-file=insert-${database-type}.sql