CVS: plexus-components/python-factory/src/main/org/apache/plexus/service/repository/factory/python PythonComponentFactory.java,NONE,1.1 PythonServiceFactory.java,1.3,NONE

[email protected] Thu, 1 May 2003 14:50:43 -0500
Newsgroups gmane.comp.java.plexus.devel
Message-ID <[email protected]>
Update of /cvsroot/plexus/plexus-components/python-factory/src/main/org/apache/plexus/service/repository/factory/python
In directory eng.werken.com:/tmp/cvs-serv18947/src/main/org/apache/plexus/service/repository/factory/python

Added Files:
	PythonComponentFactory.java 
Removed Files:
	PythonServiceFactory.java 
Log Message:
o updating for plexus 0.4.


--- NEW FILE: PythonComponentFactory.java ---
package org.apache.plexus.service.repository.factory.python;

import org.apache.avalon.framework.activity.Initializable;
import org.apache.avalon.framework.configuration.Configurable;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.framework.context.Context;
import org.apache.avalon.framework.context.ContextException;
import org.apache.avalon.framework.context.Contextualizable;
import org.apache.plexus.logging.AbstractLogEnabled;
import org.apache.plexus.service.repository.factory.ComponentFactory;
import org.python.core.Py;
import org.python.core.PySystemState;
import org.python.util.PythonInterpreter;

import java.io.File;
import java.io.InputStream;
import java.util.Properties;

/**
 */
public class PythonComponentFactory
    extends AbstractLogEnabled
    implements Contextualizable, Configurable, Initializable, ComponentFactory
{
    /** */
    private String componentScript = "org/apache/plexus/service/repository/factory/python/conf.py";
    /** */
    private String pythonHome;
    /** */
    private String pythonPath;
    /** */
    private String pythonCachedir;
    /** */
    private ClassLoader classLoader;
    /** */
    //private PythonInterpreter interp;
    /** */
    private PySystemState pythonSystemState;
    /** */
    private Properties pythonSystemStateProperties;

    // ----------------------------------------------------------------------
    // Lifecylce Management
    // ----------------------------------------------------------------------

    /** @see Contextualizable#contextualize */
    public void contextualize( Context context )
        throws ContextException
    {
        classLoader = (ClassLoader) context.get( "common.classloader" );
    }


    public void configure( Configuration configuration )
        throws ConfigurationException
    {
        pythonHome = configuration.getChild( "python-home" ).getValue();
        pythonPath = configuration.getChild( "python-path" ).getValue();
        pythonCachedir = configuration.getChild( "python-cachedir" ).getValue();
    }

    public void initialize()
        throws Exception
    {
        pythonSystemStateProperties = new Properties();
        pythonSystemStateProperties.setProperty( "python.home", pythonHome );
        pythonSystemStateProperties.setProperty( "python.path", pythonPath );
        pythonSystemStateProperties.setProperty( "python.cachedir", pythonCachedir );

        // This is just the way jython is, don't look at me. You cannot
        // create an instance of the system state without initializing first.
        PySystemState.initialize( pythonSystemStateProperties, null, null );

        pythonSystemState = new PySystemState();
        pythonSystemState.initialize( pythonSystemStateProperties, null, null );
        pythonSystemState.setClassLoader( classLoader );

        Py.setSystemState( pythonSystemState );
    }

    public Object newInstance( String name, ClassLoader classLoader )
        throws ClassNotFoundException, InstantiationException, IllegalAccessException
    {
        Object component = null;

        try
        {
            PythonInterpreter interp = new PythonInterpreter( null, pythonSystemState );

            // Execute the component script.
            InputStream is = classLoader.getResourceAsStream( componentScript );
            interp.execfile( is );

            // Execute the component script.
            interp.execfile( new File( pythonPath, name + ".py" ).getPath() );

            try
            {
                // We create an instance of the screen class from the
                // python script
                interp.exec( "scr = " + name + "()" );
            }
            catch ( Throwable e )
            {
                throw new Exception( "Cannot create " + name );
            }

            // Here we convert the python sceen instance to a java instance.
            component = interp.get( "scr", Object.class );
        }
        catch ( Exception e )
        {
            // We log the error here because this code is not widely tested
            // yet. After we tested the code on a range of platforms this
            // won't be usefull anymore.
            getLogger().error( "PYTHON SCRIPT SCREEN LOADER ERROR:", e );
            throw new InstantiationException( e.getMessage() );
        }

        return component;
    }
}

--- PythonServiceFactory.java DELETED ---