link
Avalon
Avalon Central
Home PlanetProductsCentral
Service Publication

One of the most important aspects of a component management platform is the ability to provide component assembly. Assembly is achieved by wiring together the services provided by one component with the service dependencies declared by a consumer component.

This tutorial presents the creation of a simple component that publishes a service. This component will be used in the subsequent tutorial dealing with dependencies.

Resources supporting this tutorial are contained in the tutorials/dependencies/ package.

Service Interface

Services are normally exposed under a service interface. The following java source is the declaration of a RandomGenerator service interface.

RandomGenerator.java

package tutorial;

/**
 * A service that provides access to a random number.
 */
public interface RandomGenerator
{

   /**
    * Return a random integer
    * @return the random number
    */
    int getRandom();

}
Component Implementation

The following source contains the component implementation for the service above.

RandomGeneratorProvider.java

package tutorial;

import java.util.Random;

import org.apache.avalon.framework.logger.AbstractLogEnabled;
import org.apache.avalon.framework.activity.Initializable;
import org.apache.avalon.framework.activity.Disposable;

/**
 * An implementation of a random number generator.
 *
 * @avalon.component version="1.0" name="random" lifestyle="singleton"
 * @avalon.service type="tutorial.RandomGenerator" version="1.0"
 */
public class RandomGeneratorProvider extends AbstractLogEnabled 
  implements Initializable, RandomGenerator, Disposable
{

    private Random m_random = new Random();

    public void initialize()
    {
        getLogger().info( "initialization" );
    }

   /**
    * Return a random integer
    * @return the random number
    */
    public int getRandom()
    {
        getLogger().info( "processing request" );
        return m_random.nextInt();
    }

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

}

In addition to the component implementation, we need to generate an xinfo resource in which we declare the publication of the service by the component. In this example the component type is declaring one service although multiple service declarations are supported. This is generated automatically based on the following tag:

 * @avalon.service type="tutorial.RandomGenerator" version="1.0"

The type descriptor generated for this component is listed below.

RandomGeneratorProvider.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>random</name>
    <version>1.0.0</version>
    <lifestyle>singleton</lifestyle>
    <collection>hard</collection>
  </info>
  <services>
    <service type="tutorial.RandomGenerator" version="1.0.0"/>
  </services>
</type>

The following tutorial presents the use of this service by HelloComponent.