This tutorial presents information about the management of the runtime context supplied to your component.
Resources supporting this tutorial are contained in the tutorials/context/avalon/ package.
In order to receive a runtime context we need to update the HelloComponent source so that it implements the Avalon Contextualizable lifecycle stage interface. Merlin will build and supply a context object containing the following four context entries:
Merlin Standard Context Entries
Key | Class | Description |
---|---|---|
urn:avalon:home | java.io.File | The working directory. |
urn:avalon:temp | java.io.File | The 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. |
In order to receive a runtime context we need to update the HelloComponent source so that it implements the Avalon Contextualization stage interface. Merlin will build and supply a context object containing the standard four context entries.
HelloComponent.java
package tutorial; import java.io.File; import org.apache.avalon.framework.logger.AbstractLogEnabled; import org.apache.avalon.framework.context.Contextualizable; import org.apache.avalon.framework.context.Context; import org.apache.avalon.framework.context.ContextException; /** * Component demonstrating access to standard context entries. * @avalon.component name="demo" */ public class HelloComponent extends AbstractLogEnabled implements Contextualizable { private File m_home = null; private File m_temp = null; private String m_name = "unknown"; private String m_partition = "unknown"; /** * Contextualization of the component by the container. * The context supplied by the container holds the * Merlin standard context entries for the home and * working directories, component name and partition. * * @avalon.context * @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 void contextualize( Context context ) throws ContextException { 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 ); getLogger().info( buffer.toString() ); } }
Build and run the tutorial.
$ maven $ merlin -execute target\classes
In the logging output we see the values provided by Merlin to the component.
[INFO ] (tutorial.hello): standard context entries name: hello home: F:\dev\avalon\merlin\platform\tutorials\context\avalon\home\tutorial\hello temp: C:\TEMP\tutorial\hello partition: /tutorial/
The next tutorial shows how Merlin provides support for creating custom context entries.