link
Avalon
Service Manager Artifact
Home PlanetProductsCentral
Service Manager Artifact
Overview

A ServiceManager is used as a mechanisms to provide dependent services to a component. A component implementation declares dependencies using the @avalon.dependency tag. Dependencies may be declared relative to the component constructor or through the optional Serviceable delivery interface.

Example of a constructor based declaration of service dependencies.

   /**
    * Creation of a new instance.  The component receives
    * a service manager via constructor that is pre-populated
    * with services based on the avalon.dependency declarations.
    *
    * @param manager the service manager
    *
    * @avalon.dependency type="tutorial.RandomGenerator" key="random"
    * @avalon.dependency type="tutorial.Identifiable"
    */
    public DefaultWidget( ServiceManager manager )
      throws ServiceException
    {
        m_random = (RandomGenerator) manager.lookup( "random" );
        m_identifiable = (Identifiable) manager.lookup( 
          Identifiable.class.getName() );
    }
Lookup and Release Semantics

Services are aquired by passing the dependency key under the lookup operation. If no key is declared, the component can aquire the service using the value of the type attribute. When a component no longer requires a service it can relase the service, thereby notifying the container that the service instance is no longer required. While release of a service is optional, component authors should be aware that the component implementation underlying the service may be pooled. As such, release of the service may have a direct impact on memory consumption and overall performance.

Example of an aquisition and release cycle.

   /**
    * Creation of a new instance during which a service is aquired,
    * used, and released.
    *
    * @param manager the service manager
    *
    * @avalon.dependency type="tutorial.RandomGenerator" key="random"
    */
    public DefaultWidget( ServiceManager manager )
      throws ServiceException
    {
        RandomGenerator random = 
          (RandomGenerator) manager.lookup( "random" );
        doSomething( random.getValue() );
        manager.release( random );
    }