A component may be supplied with a context object either by constructor or through an implementation of the Contextualizable lifecycle artifact delivery interface. A context object is typically used to provide standard environment information to a component. It may also be used a mechanisms to deliver domain specific resources to a component.
The avalon platform defines a small number of standard context entries that are managed automatically by the container.
Key | Class | Description |
---|---|---|
urn:avalon:home | java.io.File | The working directory. |
urn:avalon:temp | java.io.File | A temporary directory that will be destroyed at the end of the session. |
urn:avalon:name | java.lang.String | The name assigned to the component. |
urn:avalon:partition | java.lang.String | The assigned partition name. |
Both standard and custom context entry dependencies may be declared using the @avalon.entry source markup tag. The following code fragment is an example of a constructor declaring a set of standard context entry dependencies.
/** * Creation of a new HelloComponent instance using a * container supplied logging channel and context. * The context supplied by the container holds the * standard context entries for the home and * working directories, component name and partition. * * @avalon.entry key="urn:avalon:name" * @avalon.entry key="urn:avalon:partition" * @avalon.entry key="urn:avalon:home" type="java.io.File" * @avalon.entry key="urn:avalon:temp" type="java.io.File" */ public HelloComponent( Logger logger, Context context ) throws ContextException { m_logger = logger; m_home = (File) context.get( "urn:avalon:home" ); m_temp = (File) context.get( "urn:avalon:temp" ); m_name = (String) context.get( "urn:avalon:name" ); m_partition = (String) context.get( "urn:avalon:partition" ); StringBuffer buffer = new StringBuffer( "standard context entries" ); buffer.append( "\n name: " + m_name ); buffer.append( "\n home: " + m_home ); buffer.append( "\n temp: " + m_temp ); buffer.append( "\n partition: " + m_partition ); m_logger.info( buffer.toString() ); }
While the context interface provides the mechanisms to access any object type via key, it is sometimes convenient to declare a domain specific interface and custom context implementation. This enables client code to take advantage of convenience accessors, resulting in code that is not cluttered with casting and context entry key references.
The dependency of a component on a custom context type is declared using the @avalon.context tag type attribute.
/** * Creation of a new DefaultWidget instance using a * custom object that is castable to a domain specific * interface. * * @avalon.context type="net.dpml.WidgetContext" * @avalon.entry key="urn:avalon:home" type="java.io.File" */ public DefaultWidget( WidgetContext context ) throws ContextException { File common = context.getCommonDirectory(); m_common = common; }
In the above example the WidgetContext interfact is unknown to the container - as such an explicit deployment directives is required to declare a the context implementation class to be used by the container during context argument establishment.
<component name="widget" class="net.dpml.DefaultWidget"> <context class="net.dpml.DefaultWidgetContext"/> </component>