CVS: plexus-container/src/java/org/apache/plexus/service/repository/instance AbstractMultipleInstanceManager.java,NONE,1.1 ClassicSingletonInstanceManager.java,NONE,1.1 KeepAliveSingletonInstanceManager.java,NONE,1.1

Jason van Zyl <[email protected]> Tue, 5 Aug 2003 15:45:31 -0500
Newsgroups gmane.comp.java.plexus.devel
Message-ID <[email protected]>
Update of /cvsroot/plexus/plexus-container/src/java/org/apache/plexus/service/repository/instance
In directory hogshead.codehaus.org:/tmp/cvs-serv29227/src/java/org/apache/plexus/service/repository/instance

Added Files:
	AbstractMultipleInstanceManager.java 
	ClassicSingletonInstanceManager.java 
	KeepAliveSingletonInstanceManager.java 
Log Message:
o Adding some new files.


--- NEW FILE: AbstractMultipleInstanceManager.java ---
package org.apache.plexus.service.repository.instance;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.apache.plexus.service.repository.ComponentHousing;

/**
  * 
  * <p>Created on 20/07/2003</p>
  *
  * @author <a href="mailto:[email protected]">Bert van Brakel</a>
  * @version $Revision: 1.1 $
  */
public abstract class AbstractMultipleInstanceManager extends AbstractInstanceManager
{
	
    /** Component housings keyed by component. Used so we can dispose of
    	* components. Need a threadsafe map as we could be putting and setting at the same
    	* time. Were are putting and getting in equals amounts so a synchronized map is best*/
    private Map housings;
    /**
     * 
     */
    public AbstractMultipleInstanceManager()
    {
        super();
		housings = Collections.synchronizedMap(new HashMap());
    }

    protected ComponentHousing getHousing(Object component)
    {
        return (ComponentHousing) housings.get(component);
    }
    protected ComponentHousing removeHousing(Object component)
    {
        return (ComponentHousing) housings.remove(component);
    }
    protected void putHousing(Object component, ComponentHousing housing)
    {
        housings.put(component, housing);
    }

}

--- NEW FILE: ClassicSingletonInstanceManager.java ---
package org.apache.plexus.service.repository.instance;

import org.apache.plexus.service.repository.ComponentHousing;

/**
 * This ensures only a single instance of a a component exists. Once no
 * more connections for this component exists it is disposed.
 * 
 * @author <a href="mailto:[email protected]">Jason van Zyl</a>
 * @author <a href="mailto:[email protected]">Bert van Brakel</a>
 *
 * @version $Id: ClassicSingletonInstanceManager.java,v 1.1 2003/08/05 20:45:28 jvanzyl Exp $
 */
public class ClassicSingletonInstanceManager
	extends AbstractInstanceManager
{
	private ComponentHousing singleton;

	/** Number of clients using this component */
	private int connections = 0;
	
    /**
     * 
     */
    public ClassicSingletonInstanceManager()
    {
        super();
    }

	/**
	 * @see org.apache.plexus.service.repository.instance.InstanceManager#release(java.lang.Object)
	 */
	public void release(Object component)
	{
		//Only accept it if it is the same instance. 
		if( singleton.getComponent() == component)
		{
			connections--;
			if(connections == 0)
			{
				endComponentLifecycle( singleton );
			}
			singleton = null;			
		}
		else
		{
			getLogger().warn("Component returned which is not the same instance. Ignored. component=" + component);
		}
	}

	/**
	 * @see org.apache.plexus.service.repository.instance.InstanceManager#dispose()
	 */
	public void dispose()
	{
		//wait for all the clients to return all the components
		//Do we do this in a seperate thread? or block the current thread??
		//TODO
		if( singleton!=null)
		{
			endComponentLifecycle(singleton);
		} 
	}

	/**
	 * @see org.apache.plexus.service.repository.instance.InstanceManager#getComponent()
	 */
	public Object getComponent() throws Exception
	{
		if( singleton == null)
		{
			singleton = newHousingInstance();
		}
		connections++;
		return singleton.getComponent();
	}

	/**
	 * @see org.apache.plexus.service.repository.instance.InstanceManager#getConnections()
	 */
	public int getConnections()
	{
		return connections;
	}

}
--- NEW FILE: KeepAliveSingletonInstanceManager.java ---
package org.apache.plexus.service.repository.instance;

import org.apache.plexus.service.repository.ComponentHousing;

/**
 * This ensures a component is only used as a singleton, and is only shutdown when
 * the container shuts down.
 * 
 * @author <a href="mailto:[email protected]">Bert van Brakel</a>
 *
 * @version $Id: KeepAliveSingletonInstanceManager.java,v 1.1 2003/08/05 20:45:28 jvanzyl Exp $
 */
public class KeepAliveSingletonInstanceManager
	extends AbstractInstanceManager
{
	private ComponentHousing singleton;

	/** Number of clients using this component */
	private int connections = 0;
	
    /**
     * 
     */
    public KeepAliveSingletonInstanceManager()
    {
        super();
    }

	/**
	 * @see org.apache.plexus.service.repository.instance.InstanceManager#release(java.lang.Object)
	 */
	public void release(Object component)
	{
		//Only accept it if it is the same instance. 
		if( singleton.getComponent() == component)
		{
			connections--;
		}
		else
		{
			getLogger().warn("Component returned which is not the same instance. Ignored. component=" + component);
		}
	}

	/**
	 * @see org.apache.plexus.service.repository.instance.InstanceManager#dispose()
	 */
	public void dispose()
	{
		//wait for all the clients to return all the components
		//Do we do this in a seperate thread? or block the current thread??
		//TODO
		if( singleton!=null)
		endComponentLifecycle(singleton);
	}

	/**
	 * @see org.apache.plexus.service.repository.instance.InstanceManager#getComponent()
	 */
	public Object getComponent() throws Exception
	{
		if( singleton == null)
		{
			singleton = newHousingInstance();
		}
		connections++;
		return singleton.getComponent();
	}

	/**
	 * @see org.apache.plexus.service.repository.instance.InstanceManager#getConnections()
	 */
	public int getConnections()
	{
		return connections;
	}

}