Added:
trunk/israfil-micro-container/src/site/apt/lifecycle.apt
Modified:
trunk/israfil-micro-container/src/site/site.xml
Log:
add lifecycle page for describing components with startup() logic. Not
finished, just don't want to lose progress.
Added: trunk/israfil-micro-container/src/site/apt/lifecycle.apt
==============================================================================
--- (empty file)
+++ trunk/israfil-micro-container/src/site/apt/lifecycle.apt Mon Aug 4
10:55:25 2008
@@ -0,0 +1,111 @@
+ ------
+ Israfil Micro-Container : Component Lifecycle
+ ------
+ Christian Edward Gruber
+ ------
+ May 2, 2007
+
+
+ Since one bad pattern in java is to engage in all sorts of side behaviours
+ during construction, components can separate construction from initialization
+ by implementing the Startable interface. Startable components are started
+ before they are provided to any caller.
+
+%{toc}
+
+The Startable interface
+
+ The Startable interface is quite simple:
+
+-------------------
+
+package net.israfil.micro.container;
+
+public interface Startable {
+
+ public void start();
+
+ public boolean isRunning();
+
+}
+
+-------------------
+
+ Implementors should start whatever they need to in order to be ready to
+ accept calls, including marshalling any resources necessary, etc.
+
+ Care should be taken to time-out appropriately, as the container offers
+ its thread to the component during this initialization, and faulty
+ initialization can lock the container.
+
+Registering components with dependencies
+
+ Registration of components with dependencies is a bit more
complicated, as it
+ requires an adapter:
+
+-------------------
+
+public void registerType(Object key, AutoWiringAdapter componentAdapter);
+
+-------------------
+
+ AutoWiringAdapters can be created on the fly, for instance by using
an anonymous
+ inner class that implements AutoWiringAdapter or extending AbstractAutoWiringAdapter:
+
+-------------------
+
+container.registerType(ComponentThree.class,new AbstractAutoWiringAdapter(
+ ComponentThree.class, new Object[] {ComponentOne.class}
+ ) {
+ public Object create(Object[] params) throws
IllegalAccessException, InstantiationException {
+ return new ComponentThreeImpl((ComponentOne)params[0]);
+ }
+});
+
+-------------------
+
+ Another option is to create a constant adapter on the component itself
+
+-------------------
+
+public class ComponentThreeImpl implements ComponentThree {
+ public static final AutoWiringAdapter adapter = new AbstractAutoWiringAdapter(
+ ComponentThree.class, new Object[] {ComponentOne.class}
+ ) {
+ public Object create(Object[] params) throws
IllegalAccessException, InstantiationException {
+ return new ComponentThreeImpl((ComponentOne)params[0]);
+ }
+ });
+
+ private final ComponentOne one;
+
+ public ComponentThreeImpl(ComponentOne one) {
+ this.one = one;
+ }
+
+ public void doStuff() { one.whatever(); }
+}
+
+-------------------
+
+ Having created this adapter constant, you can then more easily
register the
+ component in the following way:
+
+-------------------
+
+container.registerType(ComponentThree.class,ComponentThreeImpl.adapter);
+
+-------------------
+
+Retrieving components
+
+ Retrieving components is quite simple, using the getComponent method.
+
+-------------------
+
+ComponentThree three = (ComponentThree)container.getComponent(ComponentThree.class);
+three.doStuff();
+
+-------------------
+
+
Modified: trunk/israfil-micro-container/src/site/site.xml
==============================================================================
--- trunk/israfil-micro-container/src/site/site.xml (original)
+++ trunk/israfil-micro-container/src/site/site.xml Mon Aug 4 10:55:25 2008
@@ -5,7 +5,8 @@
</menu>
<menu name="Overview">
<item name="Introduction" href="index.html"/>
- <item name="Usage" href="usage.html"/>
+ <item name="Usage" href="usage.html"/>
+ <item name="Lifecycle" href="lifecycle.html"/>
<!-- <item name="FAQ" href="faq.html"/> -->
</menu>
<!--