- Introduction
- Properties Basics
- Designing a Component Configuration Interface (CCI)
- Where Are We?
- Resources
Designing a Component Configuration Interface (CCI)
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're 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, as shown in Table 1.
Table 1 APIs Versus CCIs as Reuse Mechanisms
|
APIs |
CCIs |
Who? |
developers |
developers, admins, end users |
When? |
development cycle |
any time |
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. Such support is indeed present, but it's 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's 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'll show how they're realized in three different Java CCIs: JNDI configuration, security policy configuration, and Remote Method Invocation (RMI) configuration. As a control group, I'll also discuss 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 the Java 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 hoped. 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:
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 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 in the "Resources" section at the end of this article)
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 can't 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 look up some configuration settings, to what do we apply them? 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 described in the previous section, RMI configuration depends on the system properties collection. Therefore, there is basically one spatial scope that includes everythingand 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're about to make an RMI call. This plan will fall apart for two major reasons:
While you can control your own code, you can't control the RMI plumbing, which is doing things "behind your back."
The RMI documentation doesn't specify when configuration values are read, or if they're cached.
The RMI CCI documentation says nothing about temporal scope, so it's probably unsafe to make any assumptions. Properties will be read by the time they're first needed, and then after that things get pretty vague. Maybe they'll be cached. Maybe they'll be read from the file each time they're needed. You could write a set of experiments to figure out whether configuration settings were cached or reread from the file. However, any conclusions you draw from these experiments could not be considered portablein 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.
The USER_HOME files contain settings scoped to a specific user account.
System properties are scoped to a specific VM instance.
NOTE
Because JAVA_HOME and USER_HOME are simply environment variables, you could modify the Security CCI scopes by changing the values of these environment variables.
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 article, 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:
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've 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, and so on.
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 isn't 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 componentif only they had known some magic CCI setting.