Java Properties Purgatory Part 2
- Using XML for configuration
- Component Configuration in J2EE
- Component Configuration using the Preferences API
- A Common CCI architecture
- Conclusions
Abstract: In Part 1 I looked at the sad state of component configuration in the Java world. Today, component developers often rely on property files to configure and manage their components. Unfortunately, this is done in an ad hoc way, with each component developer reinventing the wheel. As a result, Java components are unnecessarily difficult to develop, learn, deploy, and maintain. This is an industry-wide problem, costing countless hours of developer time.
To provide a common vocabulary for discussing component configuration, I introduced the notion of a Component Configuration Interface (CCI). A CCI is any configuration contract specified by a component developer. A CCI is typically not a Java interface, and can take a variety of forms: properties files, serialized Java objects, system properties, XML documents, etc.
I proposed four criteria for designing a standard CCI for Java components: support for structure, lookup, scope, and metadata. In this second part I will use these criteria to
Introduce XML as a configuration option
Examine the weaknesses of current XML CCIs: the Preferences API and J2EE container configuration
Propose a common CCI architecture for all Java components
Using XML for configuration
As of SDK version 1.4, XML parser support is now a standard part of the Java language. Using XML as a configuration format provides an immediate and obvious answer for one of the four CCI criteria--structure. In Part 1 we looked at the difficulty in representing even simple multi-valued strings in property files. With XML, this problem disappears. For example, multiple values for security providers might be specified as
<providers> <provider>sun.security.provider.Sun</provider> <provider>com.sun.net.ssl.internal.ssl.Provider</provider> <provider>com.sun.rsajca.Provider</provider> <provider>com.sun.crypto.provider.SunJCE</provider> <provider>sun.security.jgss.SunProvider</provider> </providers>
This looks so simple that it is difficult to believe the power and benefit to be gained by adopting XML for component configuration. The advantages are overwhelming:
There is only one syntax to learn, so there is no need to remember that some properties delimit values with spaces, others with colons, etc.
There is no need to write special parsers for configuration data--the XML parser is built-in.
XML parsers can validate that the basic syntax of configuration data is correct.
Component developers can use an XML Schema or Document Type Declaration (DTD) to specify the exact structure they expect to receive. This is to CCIs what strongly-typed languages are to APIs.
There are an enormous number of tools available to create and manage XML data.
XML is language neutral, so the same concepts can be applied to non-Java applications. This is important in the real world where most complex applications are not 100% Java (or anything else).
Given all these benefits, you should make XML your standard for all component configuration. However, there are a few potential counterarguments to consider as you choose this course. Here are three (weak) arguments against XML for component configuration in Java:
XML has considerable potential for ambiguity and confusion.
XML is overkill for simple configuration.
XML-based configuration is not backwards-compatible with existing code.
We will look at each of these in turn.
XML: Potential for confusion?
The concern here is that XML is no better than property files, since there are many ways to encode the same data in XML. For example, the following two XML fragments convey the same basic semantics:
<server codeurl="http://www.develop.com"> <!-- etc. --> </server>
<server> <codeurl>http://www.develop.com</codeurl> <!-- etc. --> </server>
If a deployer specifies codeurl as an attribute, but the component was expecting a codeurl element, then things will break down. How is this any better than misspelling a property name or choosing the wrong delimiter? In three ways:
Even though errors can occur, they are now being detected by an XML parser, which is presumably thoroughly tested, instead of a one-off configuration parser written by a component developer.
Schemas and DTDs can be used to automatically detect a wide range of incorrect information.
XML-aware tools can help prevent these errors from occurring in the first place.
In short, XML provides a full-fledged type system. Yes, confusion is still possible. However, XML gives you a vocabulary that is as rich for CCIs as the Java language is for APIs.
XML: Overkill for simple configuration?
It is true that XML may be more than you need. There is nothing quite as simple as setting a property on the command line, e.g.:
java -Djava.security.policy=my.policy MyApplication
However, there is nothing to say that command-line properties, property files, and XML cannot coexist as configuration options for the same component--as long as there are well-defined rules for precedence that apply to all components. More on this later.
XML and backward compatibility?
The current installed based of Java applications includes thousands of shell scripts to correctly set various CCI properties. A wholesale industry switch to XML would require rewriting these scripts. Also, such a switch would be problematic where pre-1.4 versions of Java are still in use. So, XML cannot be a complete solution. A general-purpose CCI will need a backwards-compatibility mode that includes the various ad hoc solutions currently in place, without encouraging their continued use in the future.
Where are we?
XML alone solves exactly one of the four problems for component configuration, providing structure and type for configuration data. Next we will look at some existing XML CCIs to see how they deal with the other three elements of CCI: lookup, scope, and metadata.