The goal of this tutorial is to introduce Kilim in a hands-on, no-nonsense way, through building and configuring an example.
The example chosen here is a web application acting as a classical web log. The purpose of this application is to display, add and remove some text entries. Optional functionality will be configured in, such as several kinds of data storage (persistent via flat files or a database, non-persistent), an authentification module, as well as extending the functionality.
This example will, hopefully, demonstrate the intrinsic power of Kilim as well as its better side-effects such as making the application architecture more explicit.
At the heart of WebLog is a servlet, called WebLog with a lot of originality. This servlet is to be deployed under Tomcat 4.0.3. The servlet will first only read some text and print it on screen. The reading backend is separate.
A simple way of doing this would be to write the WebLog servlet thus: simple WebLog implementation (along with the classes EntriesReader and FlatFileEntriesReader).
Obviously this approach has a number of problems:
These issues are what Kilim is made to address.
Kilim uses two Kilim Configuration Files (kcf
files): a bootstrap (kernel) kcf and a runtime kcf. The bootstrap kcf is compiled into a Java class at compile time, while the runtime kcf is read and decoded at execution time.
Discussion:
The kernel kcf complies with the Kilim configuration DTD.
The kernel kcf normally fills three roles:
In this case, the methods to read the runtime kcf are defined in KKernelBoot.kcf
, which is paired with the org.objectweb.jonathan.libs.kernel.KKernel
kernel. The kernel reads the runtime configuration file and instanciates components. KKernel
looks for a jonathan/KKernel/runtime_configuration_file
String property, then looks for this file to use as a runtime kcf.
Our bootstrap kcf does not define any special configuration, although that would have been possible (typically, all of Jonathan's configuration is defined in the bootstrap kcf).
The bootstrap kcf is compiled by using the org.objectweb.jonathan.tools.Kcf2java
tool, provided in kilim-tools.jar
.
Discussion:
The runtime kcf also complies with the DTD.
Here we externalise the architecture of our application: we use a component called Entries Reader
. This component is defined through the use of an ASSEMBLAGE
element. An assemblage represents an element instanciation. For this to happen, an assemblage needs to provide two pieces of information:
org.objectweb.jonathan.apis.kernel.Factory
;Note that the factory is defined as an atom. Atoms represent instances of classes which have a default constructor. The instance is created at runtime when needed. The difference between atoms and assemblages is that contexts are not propagated to atoms. It is of course possible for factories to be assemblages themselves (but at some point the recursion has to stop).
See the new corresponding source code:
Note that FlatFileEntryReader
itself does not require any Kilim-specific code changes. Its configuration may be done through construction, only the factory requires to handle Contexts
. Of course, it is also possible for the factory to pass the Context
instance to FlatFileEntryReader
at construction time. The Context
may then be used at any time (construction, execution) by the object.
Also noteworthy is that the factory only receives a local Context
, i.e. a Context
representing the configuration of the element to be built rather than the whole configuration of the application.
All this is well and good, but suppose we want to add some extra functionality to our web log. For instance, let us modify it so that it does the following:
For this all we have to do is to write the necessary code and modify the configuration. In particular, we now have two different implementations of EntriesReader, which we can choose between without having to recompile the application.
Here are the final files:
Download the source files:
Note: you will need Jakarta Ant version 1.5 to build.
Last updated: 2002-06-28