Using Merlin

Adding configuration support to the component

To be supplied with a configuration, our component must implement the Avalon Framework Configurable interface. The following code is the HelloComponent extended to include implementation of Configurable, and updates to the inialize method to log the source of the configuration based on runtime information.

Resources supporting this tutorial are contained in the tutorials/configuration/block/ directory.

/**
 * A configurable component.
 *
 * @avalon.component version="1.0" name="simple"
 */
public class HelloComponent extends AbstractLogEnabled 
  implements Configurable, Initializable
{
    private String m_source = "undefined";

   /**
    * Configuration of the component by the container.  The 
    * implementation get a child element named 'source' and 
    * assigns the value of the element to a local variable.
    *
    * @param config the component configuration
    * @exception ConfigurationException if a configuration error occurs
    */
    public void configure( Configuration config ) throws ConfigurationException
    {
        getLogger().info( "configuration stage" );
        m_source = config.getChild( "source" ).getValue( "unknown" );
    }

   /**
    * Initialization of the component by the container.
    * @exception Exception if an initialization error occurs
    */
    public void initialize() throws Exception
    {
        getLogger().info( "initialization stage" );
        final String message = 
          "source: " + m_source;
        getLogger().info( message );
    }
}

Declaring a Configuration

A number of different approaches to controlling the configuration that is supplied to a component are presented in the next turorial .