Lifecycle extensions are additional stages a component can traverse through during it's lifetime. Lifecycle extensions allow a container to provide extra functionality to components in addition to the standard stages defined by Avalon Framework.
Avalon Framework defines a set of standard interfaces often termed as Lifecycle stages that can be used by a container to determine the components requirements during deployment and subsequent decommissioning.
These interfaces allows the developer to separate the various concerns involved when writing a component. Often termed SoC and IoC (Separation of Concerns and Inversion of Control), these concepts represent one of the primary advantages of using Avalon.
Sometimes it's useful to extend this development paradigm from the framework level into the application domain, to create customized lifecycle extensions that are called upon in addition to the standard set defined by the Avalon Framework.
Such custom lifecycle stages can further enable domain specific logic across many, perhaps even unrelated components, can reduce code duplication, and allows the developer to reuse the same development and thinking paradigm as the standard lifecycle stages.
For example, you might want to pass a specialized SecurityManager to some of your components before they are initialized, or have their internal state persistently cached during system shutdown and restored at during startup. You might want to pass user dependent decryption keys to your component, or give components the opportunity to recycle themselves before being disposed or returned to a pooled component handler.
The possibilities and number of extensions are only limited by the requirements of your particular application domain.
This document describes how to add new lifecycle extensions using Fortress and Merlin containers. This document assumes a knowledge of what an Avalon lifecycle is, and a basic understanding of the standard lifecycle interfaces Avalon Framework defines. References in this document to Service and ServiceManager can also be freely interpreted as Component and ComponentManager by the reader.
Support for lifecycle extensions in the other Avalon containers is technically possible but has not yet been discussed. Please check with the Avalon developer mailing list if you use one of these containers and would like to use lifecycle extensions.
Extending a Component's lifecycle is straightforward. An overview of the process follows:
SecurityManageable
,
Cacheable
,
Decryptable
,
Recycleable
interfaces.
Creator
and/or
Accessor
interfaces and implemets the interaction with target components supplied under the
create, destroy, access and relase operations.
LifecycleExtensionManager
implements
clause to your Component, or Component implementation,
and write any methods defined in the implemented interface.
The life of any component can be broken down to the following phases:
lookup()/select()
).
release()
).
Lifecycle extensions can be added to any of the above defined phases. This allows you to control the interception point your particular extension will be applied under.
For example, thread or user dependent extensions would be added at the access and release levels (ie. when the component is retrieved and returned to the ServiceManager) as they depend on runtime data not available until they are actually used.
More static, or global extensions would be added at the creation or destruction level, since they do not depend on any external data that change during runtime, nor are they particular to any one context of use.
A container manages extensions using an extension handler. Handlers may implement
the
Creator
and/or
Accessor
interfaces. A creator extension
will be activated during the create and destroy stages of a component lifecycle. A
accessor extension will be activated during the access and release stages.
The
Creator
interface describes the create and destroy
stages that occur between a component and a container
during service management. Lifecycle extensions supporting create
and destroy stages must implement this interface.
package org.apache.avalon.lifecycle; import org.apache.avalon.framework.context.Context; public interface Creator { /** * Create stage handler. * * @param object the object that is being created * @param context the context instance required by the create handler * implementation * @exception Exception if an error occurs */ void create( Object object, Context context ) throws Exception; /** * Destroy stage handler. * * @param object the object that is being destroyed * @param context the context instance required by the handler * implementation */ void destroy( Object object, Context context ); }
The
Accessor
interface describes the access and release
stages that occur between a service or component manager and a container
during service deployment. Lifecycle extensions supporting access
and release stages must implement this interface.
package org.apache.avalon.lifecycle; import org.apache.avalon.framework.context.Context; public interface Accessor { /** * Access stage handler. * * @param object the object that is being accessed * @param context the context instance required by the access handler * implementation * @exception Exception if an error occurs */ void access( Object object, Context context ) throws Exception; /** * Release stage handler. * * @param object the object that is being released * @param context the context instance required by the release handler * implementation */ void release( Object object, Context context ); }
If you have any particular questions, comments, etc, please send an email to the Avalon developer mailing list.