Author: Timo Laitinen
Date: 2005-05-26
1. Situation
This example is based on a hypothetical situation, where:
- There is an application that uses a
java.text.DateFormat
object. - The details of the kind of
java.text.DateFormat
used is put into a configuration file for easier modification.
The problem is solved by using both JICE and Java properties files and the two solutions is compared.
2. Hardcoding details into Java code
Suppose that initially no configuration files would be used and the application had a following initDateFormat()
method that would initialize the java.text.DateFormat
instance:
public java.text.DateFormat initDateFormat() { java.text.DateFormat dateFormat; String pattern = "dd.MM.yyyy HH:mm:ss z"; String language = "fi"; String country = "FI"; java.util.Locale locale = new java.util.Locale(language,country); dateFormat = new java.text.SimpleDateFormat(pattern,locale); return dateFormat; }
Next, the properties of the java.text.DateFormat
are moved into a configuration file instead of hardcoding them into the Java code.
3. Configuring the application with Java properties files
If Java properties files would be used as configuration files, we could create a properties file named DateFormat.properties
that would contain the properties:
dateformat.pattern=dd.MM.yyyy HH:mm:ss z dateformat.locale.language=fi dateformat.locale.country=FI
The method initDateFormat()
could be refactored to contain the code that read the properties file and initialized the java.text.SimpleDateFormat
instance accordingly:
public java.text.DateFormat initDateFormat() { try { java.util.Properties properties = new java.util.Properties(); // locate the properties file java.io.File file = new java.io.File("DateFormat.properties") // load the file properties.load(new FileInputStream(file)); // read the properties String pattern = properties.getProperty("dateformat.pattern"); String language = properties.getProperty("dateformat.locale.language"); String country = properties.getProperty("dateformat.locale.country"); // create the dateformat java.util.Locale locale = new java.util.Locale(language,country); java.text.DateFormat dateFormat = new java.text.SimpleDateFormat(pattern, locale); return dateFormat; } catch(Exception e){ // handle the exception.. } }
4. Configuring the application with JICE
If JICE would be used as the configuration system, a JIC file named DateFormat.jic
would be created:
<dateFormat xmlns="http://www.jicengine.org/jic/2.1" class="java.text.SimpleDateFormat" args="pattern,locale">
<pattern>dd.MM.yyyy HH:mm:ss z</pattern>
<locale class="java.util.Locale" args="language,country">
<language>fi</language>
<country>FI</country>
</locale>
</dateFormat>
The DateFormat.jic
contains now all the details of the java.text.DateFormat
. The method initDateFormat()
would be refactored to something like this:
public java.text.DateFormat initDateFormat() { try { // locate the jic file org.jicengine.io.Resource jicFile = new org.jicengine.io.FileResource(new java.io.File("DateFormat.jic")); // process the file with JIC Engine Object result = org.jicengine.JICEngine.build(jicFile); // we assume that we got a DateFormat instance java.text.DateFormat dateFormat = (java.text.DateFormat) result; return dateFormat; } catch(Exception e){ // handle the exception.. } }
5. Conclusions
In the solution with JICE, the method initDateFormat()
contained only the code that located the JIC file and used JIC Engine for processing it. In the solution that used Java properties, all the individual properties needed to be handled in the Java code after the properties file was loaded.
If the application size would grow, so would the number of properties in the properties and the amount of code for handling the properties. In theory, the configuration processing code wouldn't increase in a JICE-based solution because the JIC Engine would perform all the processing.
The file DateFormat.jic
contained all the details of the java.text.DateFormat
. The jic file could be easily changed so that the java.text.DateFormat
instance would be obtained from some factory instead of specifying it in the jic file. If JICE is used as the configuration system, all the flexibility of Java is available! The only thing hardcoded into the application is that there is a file DateFormat.jic
that yields a java.text.DateFormat
instance. Nothing more.