Package org.jostraca.process

Source Code of org.jostraca.process.InternalGroovyController

/*
* Name:    InternalGroovyController
* Authors: Richard Rodger
*
* Copyright (c) 2008 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 groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyObject;

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

import java.io.File;
import java.io.PrintStream;


/** Processing class for executing Groovy <code>CodeWriters</code> inside the Jostraca process.
*/
public class InternalGroovyController extends GroovyController {

  public InternalGroovyController() {
    // 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();
    PrintStream outps = null;
    PrintStream errps = null;
   
    try {
      String   cw   = tmps.get( Property.main_CodeWriter );
      String   cwo  = makeCodeWriterOptions( tmps, template );
      String[] args = ArgUtil.splitQuoted( cwo );
      String   cwp  = getCodeWriterPath( tmps );

      ClassLoader parent = getClass().getClassLoader();
      GroovyClassLoader loader = new GroovyClassLoader(parent);
     
      Class groovyClass = loader.parseClass( new File( cwp ) );
      GroovyObject gtm = (GroovyObject) groovyClass.newInstance();

      // 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 ) {
        gtm.invokeMethod("setContext", new Object[]{context});
      }

      outps = new PrintStream( out );
      errps = new PrintStream( err );

      System.setOut( outps );
      System.setErr( errps );

      Object result = gtm.invokeMethod("_generate", new Object[]{args,true});
      WayPointRecorder.add( BasicWayPoint.ExecutingCodeWriter.make( template.getCodeWriterPath().getAbsolutePath() ) );

      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 );
      }

      if( null != outps ) {
        outps.flush();
      }
      if( null != errps ) {
        errps.flush();
      }

      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 );
        mUserMessageHandler.info( out );
      }

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

    }
  }


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

}
TOP

Related Classes of org.jostraca.process.InternalGroovyController

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.