Package org.jostraca.process

Source Code of org.jostraca.process.InternalJythonController

/*
* Name:    InternalJavaController
* Authors: Richard Rodger
*
* Copyright (c) 2004-2005 Richard Rodger
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/


// package
package org.jostraca.process;


// import
import org.jostraca.Template;
import org.jostraca.Property;

import org.jostraca.util.TextUtil;
import org.jostraca.util.ArgUtil;
import org.jostraca.util.StandardException;
import org.jostraca.util.PropertySet;
import org.jostraca.util.UserMessageHandler;
import org.jostraca.util.MessageHandlerOutputStream;
import org.jostraca.util.RecordingUserMessageHandler;
import org.jostraca.util.BasicWayPoint;
import org.jostraca.util.WayPointRecorder;

import org.python.core.PyStringMap;
import org.python.core.PySystemState;

import org.python.util.PythonInterpreter;

import java.util.Properties;
import java.util.List;

import java.io.PrintStream;


/** Processing class for executing Jython <code>CodeWriters</code> inside the Jostraca process.
*/
public class InternalJythonController extends JythonController {

  public InternalJythonController() {
    // do nothing
  }


  protected void processImpl( Template pTemplate ) {
    Template    template   = pTemplate;
    PropertySet tmps       = template.getMergedPropertySet();

    boolean     successful = false;
    PrintStream sysOut     = null;
    PrintStream sysErr     = null;
    RecordingUserMessageHandler rumh = new RecordingUserMessageHandler();

    try {
      String   cw   = tmps.get( Property.main_CodeWriter );
      String   cwo  = makeCodeWriterOptions( tmps, template );
      String[] args = ArgUtil.splitQuoted( cwo );
      String   cwp  = getCodeWriterPath( tmps );

      Properties jythonProps = new Properties();
      jythonProps.put( "python.home", tmps.get("jython.home") );
      PythonInterpreter.initialize( System.getProperties(), jythonProps, args );

      PyStringMap       dict  = new PyStringMap();
      PySystemState     pysys = new PySystemState();
      PythonInterpreter pi    = new PythonInterpreter( dict, pysys );


      // DO NOT use CommandLineUserMessageHandler here - causes infinite recursion
      MessageHandlerOutputStream  out  = new MessageHandlerOutputStream( UserMessageHandler.INFO,  rumh );
      MessageHandlerOutputStream  err  = new MessageHandlerOutputStream( UserMessageHandler.ERROR, rumh );

      // space out dots to avoid accidental replace on System DOT out search
      sysOut = System . out;
      sysErr = System . err;
   
      // set context if not null
      Object context = template.getContext();
      if( null != context ) {
        pi.set( "context", context );
      }

      System.setOut( new PrintStream( out ) );
      System.setErr( new PrintStream( err ) );

      pi.execfile( cwp );
     
      String jythonexec
        = "cw = "+cw+"()\n"
        + (null!=context?"cw._setContext( context )\n":"")
        + "cw.main( sys.argv )\n"
        + "result = cw._getResult()\n"
        ;
      pi.exec( jythonexec );
      WayPointRecorder.add( BasicWayPoint.ExecutingCodeWriter.make( template.getCodeWriterPath().getAbsolutePath() ) );

      Object result = pi.get("result");

      if( result instanceof Integer ) {
        successful = ( 0 == ((Integer)result).intValue() );
      }
      else {
        successful = true;
      }

      template.setResult( result );
    }

    // FIX: need a util to handle general exceptions and just rethrow StandardExceptions
    catch( StandardException se ) {
      throw se;
    }
    catch( Exception e ) {
      throw new ProcessException( e );
    }
    finally {
      if( null != sysOut ) {
        System.setOut( sysOut );
      }
      if( null != sysErr ) {
        System.setErr( sysErr );
      }

      String out = rumh.toString( UserMessageHandler.INFO, false );
      if( !TextUtil.isEmptyOrNull( out ) ) {
        // REVIEW: need to do this a better way so that it is automatic
        parseOutResult( out, template );
        iUserMessageHandler.info( out );
      }

      String err = rumh.toString( UserMessageHandler.ERROR, false );
      if( !TextUtil.isEmptyOrNull( err ) ) {
        iUserMessageHandler.info( err );
      }

    }
  }


  protected void completeImpl( List pTemplateList ) {
    // do nothing
  }





}




TOP

Related Classes of org.jostraca.process.InternalJythonController

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.