Java Development News:

Escaping Properties Purgatory

By Stuart Halloway

01 Apr 2004 | TheServerSide.com

Properties Basics

Throughout Java's core API, properties are used to give users and administrators more control over their applications. Consider the following three approaches to configuring a context for the Java Naming and Directory Interface (JNDI).

Listing 1. Configuration interlaced with code 

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.util.Properties;

public class TestProperties
{
  public Context getIntialContext() throws NamingException
  {
    Properties env = new Properties();
    env.setProperty(Context.INITIAL_CONTEXT_FACTORY,
                    "com.sun.jndi.ldap.LdapCtxFactory");
    env.setProperty(Context.PROVIDER_URL,
                    "ldap://localhost:389/o=developmentor, ou=java");
    return new InitialContext(env);
  }
}

Listing 2. Configuration passed on the command line 

>java -Djava.naming.factory.initial=com.sun.jndi.ldap.LdapCtxFactory
      -Djava.naming.provider.url=ldap://localhost:389/o=developmentor, ou=java
      YourApplication

Listing 3. Configuration in a properties file

#file jndi.properties
java.naming.factory.initial=com.sun.jndi.ldap.LdapCtxFactory
java.naming.provider.url=ldap://localhost:389/o=developmentor, ou=java

In Listing 1, the Properties class is used to specify two name-value pairs that will be passed to the InitialContext constructor. The InitialContext constructor is a JNDI-provided factory method that loads different JNDI providers depending on the parameters passed in.

In Listing 2, these same name-value pairs are passed into the application on the command line that launches Java. The application (not shown here) can now simply call a no-arguments version of the InitialContext constructor, and the JNDI library will infer the necessary configuration information. Another variant, shown in Listing 3, is to specify the properties in a file called a properties file.  

All three listings lead to the same result--loading a JNDI provider that accesses LDAP on the local machine. However, the first listing leaves all control in the hands of the developer,  while the latter two listings shift control to whoever launches the application, i.e. an administrator or an end user. This shift has profound effects:

  • Certain kinds of changes are now far less expensive. If we want to change any aspect of the program's behavior that is controlled by a property, we do not need the services of a Java developer to do so.. 
  • By giving control to an administrator, we encourage black-box reuse. Our components can be configured for different scenarios without requiring any knowledge of the source code, or even of Java.
  • Configuration changes can be made on live deployment systems without a compile/debug/test cycle.

In short, separating configuration from code makes reuse more likely, makes modifications cheaper, and decouples configuration changes from the development cycle.

Designing a Component Configuration Interface

When people think about code reuse, they typically picture making method or function calls into a reusable library. Such a library presents an Application Programming Interface (API) to its clients. Java provides great feature support for creating reusable APIs: interfaces, inheritance, encapsulation, dynamic proxies, reflection, JavaBeans, and so on. 

However, reconfiguring an application is also a form of reuse. When you modify a component's behavior by changing a configuration setting you are using a Component Configuration Interface (CCI). A CCI is not a Java interface, in the sense of the interface keyword. But it is an interface nonetheless, in that it specifies a contract between a component and a user of that component.  

The CCI concept is not prominent in the Java documentation, which is a bit odd since this kind of reuse is more common than API-level reuse. To prove this to yourself simply compare the reach of APIs and CCIs:

Table 1. APIs vs. CCIs as reuse mechanisms

  APIs CCIs
Who? developers developers, admins, end-users
When? development cycle anytime
Where? development machine deployment machine

On every axis, configuration reaches a broader audience. So, one might hope that Java  would have terrific support for creating reusable CCIs, just as it does for creating APIs. Some support is present, but it is fragmented and not standardized. 

Application programming interfaces (APIs) are described in some language, such as Java. Similarly, Component Configuration Interfaces (CCIs) deserve their own configuration "language". Designing such a language has much in common with designing a programming language. You need the following elements:

  • Structure. Producers and consumers of configuration information must agree on some structure for passing that information, and possibly a type system that allows structures to be validated for particular uses.
  • Lookup. There must be some way to locate the configuration information when it is needed.
  • Scope. Configuration information must bind to some specific slice of code and data. This slice could be some combination of server farm(s), machine(s), process(es), thread(s), object(s), or something more esoteric.
  • Metadata. Configuration information should expose metadata in a standard format. Tools can then process this metadata and tell users how the configuration information can be used.

At this point these elements may seem fairly abstract, so to illustrate them I will show how they are realized in three different Java CCIs: JNDI configuration, security policy configuration, and Remote Method Invocation (RMI) configuration. As a control group, I will also look at how the Java API matches up against these elements. The conclusions are summarized in Table 2.

Table 2. CCI Comparison

  Java platform JNDI CCI Security CCI RMI CCI
Structure Java language properties file custom format properties file 
Lookup classpath, custom loaders ad hoc rules ad hoc rules system props only
Scope static, instance, class loader, others poorly specified poorly specified poorly specified
Metadata binary class format, reflection none none none

As the table suggests, the Java platform has a rich and flexible implementation of structure, lookup, scope, and metadata. Unfortunately, the CCIs are not similarly well-equipped. Because there are no complete standards for CCIs, each subsystem must define its own CCI contract. All the contracts are different, which imposes an unnecessary learning curve on developers.

Structure

Whereas Java the language has a rich type system, CCIs often eschew this in favor of simpler, text-based formats. For example, the reference implementation of security policy is configured using a policy file. The policy file defines its own custom serialized format, with entries that look like this: 

  grant codeBase "file:${java.home}/lib/ext/*" {
    permission java.security.AllPermission;
  };

Using this custom format is an unnecessary burden for everyone. It necessitates having a special parser built into the standard library, plus custom tools such as policytool. (Of course, the obvious solution for complex data such as security policy entries is XML. More on this in Part 2. )

What about simple configuration scenarios that require nothing more complex than name-value pairs? Surely properties are suitable for this situation. JNDI, RMI, and Security all expose at least part of their CCI through  properties. However, reality has a way of being more complex then designers originally hope. JNDI, RMI, and Security all have configuration properties that are not simple name-value pairs. In particular, they all need the ability to express multi-valued entries. JNDI places multiple values on one line in a property file using the colon character as a delimiter:

  java.naming.factory.object=com.sun.jndi.ldap.AttrsToCorba:com.wiz.from.Person

The RMI CCI uses the space character as a delimiter, e.g.

  java.rmi.server.codebase=http://myserver.com/classes http://yourserver.com/classes

The Security CCI does it yet another way, using multiple keys that differ only by a numerical value after the last dot:

  # List of providers and their preference orders (see above):
  #
  security.provider.1=sun.security.provider.Sun
  security.provider.2=com.sun.net.ssl.internal.ssl.Provider
  security.provider.3=com.sun.rsajca.Provider
  security.provider.4=com.sun.crypto.provider.SunJCE
  security.provider.5=sun.security.jgss.SunProvider

Again, the lack of a standard harms everyone. Subsystem developers have to design their own ad hoc type system on top of property files and then write parsers for it. The proliferation of these parsers inside the core API causes unnecessary code bloat, and the different formats and rules make learning each new subsystem more of an adventure than it needs to be. 

Lookup

CCIs need to provide some lookup mechanism to load the correct configuration information. Again, a quick look at JNDI, Security, and RMI shows that all of them have different lookup rules. 

JNDI settings can be read from a variety of different locations:

  • JAVA_HOME/lib/jndi.properties
  • any jndi.properties file visible to the current class loader
  • system properties, either passed on the command line or set using java.lang.System
  • properties explicitly passed to the InitialContext constructor
  • for Applets, properties set in AppletContext
  • provider resource files (naming convention documented at [1]

If two of the sources above give conflicting information, some kind of resolution policy is needed. A resolution policy determines whether the two conflicting settings are combined in some way, or if one setting overrides the other. The resolution policy for JNDI configuration is sparsely documented at [1]

Security settings are also found in a variety of different locations:

  • JAVA_HOME/lib/security/java.policy
  • JAVA_HOME/lib/security/java.security
  • USER_HOME/.java.policy
  • system properties
  • other file loads triggered by system properties
  • other file loads triggered by entries in the .policy and .security files

Again, if two of these sources give conflicting information, there is a resolution policy to determine which one wins. I cannot find the entire security resolution policy documented in any one place. However, one example of its esoterica should be enough to make you grimace: 

  java -Djava.security.policy=my.policy SomeApplication
  java -Djava.security.policy==my.policy SomeApplication

One of the commands listed above tells the Virtual Machine (VM) to ignore all settings in the JAVA_HOME version of java.policy in favor of my.policy. The other command tells the VM to merge the settings from both locations. Can you tell which is which?

The RMI settings must come as system properties. This is simple and easy to remember but will cause problems in a moment when we look at scope.

Scope

Closely related to lookup is the notion of scope. Once we lookup some configuration settings, what do we apply them to? As I use the term here, scope means a set of objects (spatial scope) over a period of time (temporal scope). Some configuration options may have a very broad scope, affecting all the objects in a process for the entire lifetime of the process. Other options may have more limited scope, either in time or in space. The JNDI, Security, and RMI CCIs all have severe problems with anything but very coarse-grained scopes.

As we saw in the previous section, RMI configuration depends on the System properties collection. Therefore, there is basically one spatial scope that includes everything--and no standard way to run two different sets of RMI settings in the same VM. You might be tempted to set the relevant System properties every time you are about to make an RMI call. This plan will fall apart for two major reasons:

  • While you can control your own code, you cannot control the RMI plumbing which is doing things "behind your back"
  • The RMI documentation does not specify when configuration values are read, or if they are cached.

The  RMI CCI documentation says nothing about temporal scope, so it is probably unsafe to make any assumptions. Properties will be read by the time they are first needed, and then after that things get pretty vague. Maybe they will be cached. Maybe they will be read from the file each time they are needed. You could write a set of experiments to figure out if configuration settings were cached or reread from the file. However, any conclusions you draw from these experiments could not be considered portable--in the absence of documentation the behavior of the reference implementation would have to be considered implementation detail.

The Security CCI provides more flexible spatial scopes than does RMI:

  • The JAVA_HOME files contain settings scoped to all users of the same VM.1 
  • The USER_HOME files contain settings scoped to a specific user account.1 
  • System properties are scoped to a specific VM instance.

Security configuration is similar to RMI configuration in that the temporal scope of most settings is undocumented. 

The JNDI configuration settings are much more flexible in terms of spatial scope. This is because JNDI looks for jndi.properties files that are visible to the context class loader. The workings of the context class loader are beyond the scope of this paper, but see [4] for details. Basically Java's class loader architecture allows a Java programmer to partition a Java process into subspaces based on collections of classes and other resources. The context class loader is a thread-local storage slot that you can use to specify a default class loader for use by the current thread. By tying JNDI configuration to Java's underlying class loader scoping mechanism, the JNDI CCI gives a sophisticated Java programmer excellent fine-grained control over JNDI settings. 

Unfortunately the temporal scope story is not so good. The JavaDoc for JNDI explicitly punts on this issue, stating that 

"It is implementation-dependent when environment properties are used and/or verified for validity. For example, some of the security-related properties are used by service providers to "log in" to the directory. This login process might occur at the time the context is created, or the first time a method is invoked on the context. When, and whether this occurs at all, is implementation-dependent. When environment properties are added or removed from the context, verifying the validity of the changes is again implementation-dependent. For example, verification of some properties might occur at the time the change is made, or at the time the next operation is performed on the context, or not at all." [1]

To summarize, the three CCIs we have analyzed here have varying support for spatial scope, and have essentially no support for temporal scope. To make matters worse, each of the three CCIs implements scope in a totally different way. This increases the challenge for developers learning Java. To add insult to injury, many of the scope rules are poorly or incompletely documented.

Metadata

The Java binary class format stores rich metadata about class and interface types. Java tools can then use reflection to parse that metadata and provide services that make developers more productive: automatic generation of implementation class stubs, popup lists of legal method calls at a particular point in the code, etc.

Unfortunately, there is no standard metadata for CCIs. As a result, tool support is almost nonexistent. While Java editors can show a list of available methods at any given point in your code, there is no corresponding tool that can show you a list of available configuration options. Not only is there no metadata, there is not even a standard Javadoc location for a package's configuration information. Finding out how to configure a particular subsystem requires a manual search of the docs. The JNDI CCI is documented under the API help for the Content class [1]. The RMI and security policy CCIs are each documented on their own custom pages [2], [3].

The absence of metadata and consistent documentation for CCIs is a great hindrance to developer productivity. Developers often spend hours writing some module, only to discover later that the service they implemented was already available in an existing component--if only they had known some magic CCI setting.

Where are we?

In this article we have introduced the concept of a Component Configuration Interface (CCI). We have described four important elements of CCI design: structure, lookup, scope, and metadata. We have used these four elements to analyze three specific Java CCIs: JNDI, RMI, and Security. None of these CCIs are particularly compelling. More importantly, they are dissimilar from each other. Absent a standard, developers must learn each new CCI anew. Nonstandard CCIs lead to wasted time and wasted code, and they make configuration-related bugs more likely. 

In Part 2 we will 

  • Introduce XML as a configuration option
  • Examine the weaknesses of current XML CCIs: the Preferences API and J2EE container configuration
  • Propose a fresh start with a common CCI architecture for all Java components

Resources

The CCI for JNDI is documented across the two URLS at [1].  The CCI for RMI is documented at [2]. The CCI for Security is quite complex and not documented in any single location. One place to start is [3].

For more on class loaders, the context class loader, metadata, and reflection, see [4]. 

[1] http://java.sun.com/j2se/1.4/docs/api/javax/naming/Context.html#RESOURCEFILES
     http://java.sun.com/j2se/1.4/docs/api/javax/naming/InitialContext.html#ENVIRONMENT
[2] http://java.sun.com/j2se/1.4/docs/guide/rmi/javarmiproperties.html
[3] http://java.sun.com/j2se/1.4/docs/guide/security/PolicyFiles.html
[4] Component Development for the Java Platform (free book download)

About the Author

Stuart Dabbs Halloway is the Chief Technical Officer at DevelopMentor (http://www.develop.com), a developer services company that specializes in training programmers in .NET, Java, Web Services, C++, and COM. Stuart is the author of Component Development for the Java Platform, part of the DevelopMentor book series and available for free online. From January 2000 to July 2001, Stuart wrote a monthly Tech Tips column for the Java Developer Connection. He has also written for JavaPro magazine. Stuart regularly speaks at industry events such as JavaOne. Prior to DevelopMentor, Stuart worked as a lead engineer and project manager, shipping successful projects for Prentice Hall, National Geographic, and Duke University's Humanities Computing Facility. He received his B.S. and M.P.P. from Duke University in 1990 and 1994, respectively.


1. Since JAVA_HOME and USER_HOME are simply environment variables, you could modify the Security CCI scopes by changing the values of these environment variables..