Added by Peter Lawrey, last edited by Peter Lawrey on Dec 04, 2008  (view change)

Labels:

Enter labels to add to this page:
Wait Image 
Looking for a label? Just start typing.

Overview

Essence Java Configuration File allows you to configure Java applications using configuration files written as Java classes, dynamically if needed.

This page describes how to use Essence File to support Java Configuration Files.

This method of building a "container" POJO is very fast and very simple (perhaps too simple), but it is fast. In the test which created a container from file, with three components, the warmed up time to create the container was around 1,700 nano-seconds and the total time to run the test, creating one million containers, was around 3.5 seconds.

Essence JCF 1.02 Download

Examples of use

Read a Java Configuration File from disk.

The file extension is not important and can be .java . I use .jcf here to distinguish the file as a configuration file.
// load and compile a class eg.FooBarTee2 from the file eg/FooBarTee2.jcf
Class clazz = CompilerUtils.loadFromResource("eg.FooBarTee2", "eg/FooBarTee2.jcf");
// create an instance of the class.
Object fooBarTee2 = clazz.getConstructor(String.class).newInstance(getName());
// get a component of the class.
Foo foo = (Foo) clazz.getDeclaredField("foo").get(fooBarTee2);

Compile in memory

This example compiles in memory, but writes the file to disk when in debug mode so the class can be debugged.
In this example, the static code is compiled against one version of the class, but is substituted at runtime with a different version. This works provided the signature of public methods and fields does not change.
Note: static final fields cannot be reliably changed this way.

// this writes the file to disk only when debugging is enabled.
CachedCompiler cc = CompilerUtils.DEBUGGING ?
    new CachedCompiler(new File(parent, "src/test/java"), new File(parent, "target/compiled")) :
    CompilerUtils.CACHED_COMPILER;

// the bit that changes.
String text = "generated test " + new Date();
cc.loadFromJava(EG_FOO_BAR_TEE, "package eg;\n" +
        '\n' +
        "import eg.components.BarImpl;\n" +
        "import eg.components.TeeImpl;\n" +
        "import eg.components.Foo;\n" +
        '\n' +
        "public class FooBarTee{\n" +
        "    public final String name;\n" +
        "    public final TeeImpl tee;\n" +
        "    public final BarImpl bar;\n" +
        "    public final BarImpl copy;\n" +
        "    public final Foo foo;\n" +
        '\n' +
        "    public FooBarTee(String name) {\n" +
        "        // when viewing this file, ensure it is synchronised with the copy on disk.\n" +
        "        System.out.println(\"" + text + "\");\n" +
        "        this.name = name;\n" +
        '\n' +
        "        tee = new TeeImpl(\"test\");\n" +
        '\n' +
        "        bar = new BarImpl(tee, 55);\n" +
        '\n' +
        "        copy = new BarImpl(tee, 555);\n" +
        '\n' +
        "        // you should see the current date here after synchronisation.\n" +
        "        foo = new Foo(bar, copy, \"" + text + "\", 5);\n" +
        "    }\n" +
        '\n' +
        "    public void start() {\n" +
        "    }\n" +
        '\n' +
        "    public void stop() {\n" +
        "    }\n" +
        '\n' +
        "    public void close() {\n" +
        "        stop();\n" +
        '\n' +
        "    }\n" +
        "}\n");

// add a debug break point here and step into this method.
FooBarTee fooBarTee = new FooBarTee("test foo bar tee");
Foo foo = fooBarTee.foo;
assertNotNull(foo);
assertEquals(text, foo.s);