Using Merlin

Managing Dependencies

A component type can declare dependencies on services provided by other components. Merlin will ensure that dependencies are resolved prior to creation of the dependent component. Dependencies are declared in the component xinfo resource and supplied by Merlin to the component using the Avalon Serviceable interface.

Resource supporting this tutorial are contained in the turorials/dependencies package.

Adding a Serviceable method implementation

The following code fragment is the implementation of the Serviceable interface under the HelloComponent.java source.

Note the use of the @avalon.dependency tag.

HelloComponent.java

package tutorial;

import org.apache.avalon.framework.logger.AbstractLogEnabled;
import org.apache.avalon.framework.service.Serviceable;
import org.apache.avalon.framework.service.ServiceManager;
import org.apache.avalon.framework.service.ServiceException;
import org.apache.avalon.framework.activity.Initializable;
import org.apache.avalon.framework.activity.Disposable;

/**
 * The HelloComponent is dependent on a RandomGenerator service.
 * @avalon.component version="1.0" name="simple" lifestyle="singleton"
 */
public class HelloComponent extends AbstractLogEnabled 
  implements Initializable, Serviceable, Disposable
{
    RandomGenerator m_random = null;

   /**
    * Servicing of the component by the container during 
    * which service dependencies declared under the component
    * can be resolved using the supplied service manager.
    *
    * @param manager the service manager
    * @avalon.dependency type="tutorial.RandomGenerator:1.0"
    *    key="random"
    */
    public void service( ServiceManager manager )
      throws ServiceException
    {
        m_random = (RandomGenerator) manager.lookup( "random" );
    }

    public void initialize()
    {
        getLogger().info( "initialization" );
        getLogger().info( "received random value: " + m_random.getRandom() );
    }

    public void dispose()
    {
        getLogger().info( "disposal" );
    }
}

Declaring the service dependency

In this example we are automatically generating the xinfo descriptors using the builtin meta-info generation goal based on the presence of dependency tag.

* @avalon.dependency type="tutorial.RandomGenerator:1.0"
*    key="random"

The generated xinfo for the hello component is shown below.

HelloComponent.xinfo

<?xml version="1.0"?>
<!DOCTYPE type
      PUBLIC "-//AVALON/Type DTD Version 1.0//EN"
             "http://avalon.apache.org/dtds/meta/type_1_1.dtd" >
<type>
  <info>
    <name>hello</name>
    <version>1.0</version>
  </info>
  <dependencies>
    <dependency key="random" type="tutorial.RandomGenerator"/>
  </dependencies>
</type>

Executing the demo

Build and run the tutorial.

$ maven
$ merlin target\classes -execute
        

In the logging output we see that Merlin has automatically locating the RandomGeneratorProvider and associated it as the source for the RandomGenerator service. The HelloComponent implementation has logged the result of accessing and using the service.

[INFO   ] (tutorial.random-provider): initialization
[INFO   ] (tutorial.hello): initialization
[INFO   ] (tutorial.random-provider): processing request
[INFO   ] (tutorial.hello): received random value: 1234694919
[INFO   ] (tutorial.hello): disposal
[INFO   ] (tutorial.random-provider): disposal

Advanced Features

This completes the introduction to some of the basic features of the merlin platform. More tutorials will be added so please check for new releases and updates. In the meantime you may want to take a look at some of Merlin's more advanced features .