adam bien's blog

Building Plug-ins With Java EE 6 📎

"In computing, a plug-in (or plugin) is a set of software components that adds specific abilities to a larger software application. If supported, plug-ins enable customizing the functionality of an application…"
[from http://en.wikipedia.org/wiki/Plug-in_(computing)]

In Java an application usually defines an interface which is implemented then by an extension. The challenge: the application has to locate the implementation without being dependent on it. Java EE 6 / CDI solves this with a single class - javax.enterprise.inject.Instance.

The extension interface:

public interface ConfigurationProvider {
    
    public Map<String,String> getConfiguration();

}

…is going to be (lazy) loaded at startup:

import javax.enterprise.inject.Instance;
import javax.inject.Inject;

@Startup
@Singleton
public class Configuration {

    private Map<String, String> configuration;

    @Inject
    private Instance<ConfigurationProvider> configurationProvider;

   @PostConstruct
    public void fetchConfiguration() {
        this.configuration = new HashMap<String, String>() {{
            put("version", "0.7");
			//...other defaults
        }};
        this.unconfiguredFields = new HashSet<String>();
        mergeWithCustomConfiguration();
    }
    //...

}

If there is an implementation of ConfigurationProvider it will be instantiated by the CDI-container. javax.enterprise.inject.Instance implements java.lang.Iterable, so you can easily locate all implementations without knowing from where they are coming.
Plugin-ins in Java EE 6 are just commodity. There are no patterns, frameworks or libraries required to extend an application core with extension points...

The code above enables a Java EE 6 Configurator to be extended with different configuration sources (file, xml, DB).
[x-ray's cache flushing was configured that way. You will also find the code above in the git repo, See also page 98 "Configuration Over Convention with Inversion of Control" in Real World Java EE Night Hacks--Dissecting the Business Tier.]