Context Artifact

Overview

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.

Standard Context Entries

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.

Standard Context Example

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() );
    }

Context Casting

IN PREP