CVS: plexus-container-new/src/java/org/apache/plexus/embed Embedder.java,NONE,1.1

[email protected] Sat, 24 May 2003 17:25:09 -0500
Newsgroups gmane.comp.java.plexus.devel
Message-ID <[email protected]>
Update of /cvsroot/plexus/plexus-container-new/src/java/org/apache/plexus/embed
In directory eng.werken.com:/tmp/cvs-serv19628

Added Files:
	Embedder.java 
Log Message:
o start of tiny embedder if pete wants to look at it. This one just runs straight through and expects a load-on-start component.

--- NEW FILE: Embedder.java ---
package org.apache.plexus.embed;

import org.apache.plexus.DefaultPlexusContainer;
import org.apache.plexus.PlexusContainer;
import org.apache.avalon.framework.configuration.Configuration;

import java.io.InputStreamReader;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

/**
 *
 * 
 * @author <a href="mailto:[email protected]">Jason van Zyl</a>
 *
 * @version $Id: Embedder.java,v 1.1 2003/05/24 22:25:04 jvanzyl Exp $
 */
public class Embedder
{
    /** Plexus Container. */
    private PlexusContainer container;
    /** Configuration resource or file. */
    private String configuration;

    public void Embedder()
        throws Exception
    {
        container = new DefaultPlexusContainer();
    }

    public void setConfiguration( String configuration )
    {
        this.configuration = configuration;
    }

    public void addContextValue( Object key, Object value )
    {
        container.addContextValue( key, value );
    }

    public void run()
        throws Exception
    {
        container.setConfigurationResource( new InputStreamReader( findConfigurationInputStream()  ) );
        container.initialize();
        container.start();
        container.dispose();
    }

    private InputStream findConfigurationInputStream()
    {
        InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream( configuration );

        if ( is == null )
        {
            try
            {
                is = new FileInputStream( configuration );
            }
            catch ( FileNotFoundException e )
            {
                // do nothing.
            }
        }

        if ( is == null )
        {
            throw new IllegalStateException( "The specified configuration resource cannot be found: " + configuration );
        }

        return is;
    }
}