Package org.jostraca.process

Source Code of org.jostraca.process.JavaScriptController

/*
* Name:    JavaScriptController
* Authors: Richard Rodger
*
* Copyright (c) 2006 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.PropertySet;
import org.jostraca.util.StandardException;
import org.jostraca.util.ArgUtil;
import org.jostraca.util.TextUtil;
import org.jostraca.util.MessageHandlerOutputStream;
import org.jostraca.util.UserMessageHandler;
import org.jostraca.util.RecordingUserMessageHandler;

import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Function;

import java.util.List;

import java.io.PrintStream;


/** Processing class for executing <code>CodeWriters</code> inside the Java process.
*/
public class JavaScriptController extends JavaController {

  public JavaScriptController() {
    // 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();
    Object                      result     = null;

    try {
      String   cwo  = makeCodeWriterOptions( tmps, template );
      String[] args = ArgUtil.splitQuoted( cwo );

      // 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();

      boolean throwWriterExceptions = tmps.isYes( Property.main_CodeWriter_throwExceptions );

      String script = template.getCodeWriterSource();

      Context cx = Context.enter();

      // DO NOT put System.out in MessageHandler as this will cause infinite loop
      System.setOut( new PrintStream( out ) );
      System.setErr( new PrintStream( err ) );

      Scriptable scope = cx.initStandardObjects();
      cx.evaluateString(scope, script, "CodeWriter Line Number: ", 1, null);     

      Object codewriterObj = scope.get( "codewriter", scope );
      if( null == codewriterObj || !(codewriterObj instanceof Scriptable) ) {
        throw new RuntimeException( "no codewriter in script" );
      }
      Scriptable codewriter = (Scriptable) codewriterObj;

      Object _setContextObj = codewriter.get( "_setContext", codewriter );
      if( null == _setContextObj || !(_setContextObj instanceof Function) ) {
        throw new RuntimeException( "no codewriter._setContext in script" );
      }

      Function _setContext = (Function) _setContextObj;
      _setContext.call(cx,scope,codewriter,new Object[]{context});

      Object _generateObj = codewriter.get( "_generate", codewriter );
      if( null == _generateObj || !(_generateObj instanceof Function) ) {
        throw new RuntimeException( "no codewriter._generate in script" );
      }

      Function _generate = (Function) _generateObj;
      result = _generate.call(cx,scope,codewriter,new Object[]{args,new Boolean(throwWriterExceptions)});

      template.setResult( result );
    }

    catch( StandardException se ) {
      if( null != sysOut ) {
        System.setOut( sysOut );
      }
      if( null != sysErr ) {
        System.setErr( sysErr );
      }
      throw se;
    }
    catch( Exception e ) {
      if( null != sysOut ) {
        System.setOut( sysOut );
      }
      if( null != sysErr ) {
        System.setErr( sysErr );
      }
      throw new ProcessException( e );
    }
    finally {
      if( null != sysOut ) {
        System.setOut( sysOut );
      }
      if( null != sysErr ) {
        System.setErr( sysErr );
      }

      Context.exit();

      String out = rumh.toString( UserMessageHandler.INFO, false );
      if( !TextUtil.isEmptyOrNull( out ) ) {
        parseOutResult( out, template );
        iUserMessageHandler.info( out );
      }

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


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


}




TOP

Related Classes of org.jostraca.process.JavaScriptController

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.