/*
* Name: InternalJavaController
* Authors: Richard Rodger
*
* Copyright (c) 2004-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.UserText;
import org.jostraca.Property;
import org.jostraca.util.PropertySet;
import org.jostraca.util.Standard;
import org.jostraca.util.StandardException;
import org.jostraca.util.TextUtil;
import org.jostraca.util.ArgUtil;
import org.jostraca.util.ClassUtil;
import org.jostraca.util.MessageHandlerOutputStream;
import org.jostraca.util.UserMessageHandler;
import org.jostraca.util.RecordingUserMessageHandler;
import org.jostraca.util.BasicWayPoint;
import org.jostraca.util.WayPointRecorder;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.io.PrintStream;
import java.io.File;
/** Processing class for executing <code>CodeWriters</code> inside the Java process.
*/
public class InternalJavaController extends JavaController {
public InternalJavaController() {
// 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 = makeCodeWriter( tmps );
String cwo = makeCodeWriterOptions( tmps, template );
String[] args = ArgUtil.splitQuoted( cwo );
String jcwp = getCodeWriterPath( tmps );
jcwp = TextUtil.replace( jcwp, ".java", ".class" );
File jcwpf = new File( jcwp );
String cwp = jcwpf.getAbsolutePath();
iUserMessageHandler.debug( UserText.get(UserText.TXT_generating), "class-file:"+cwp+Standard.SPACE+cwo );
// NOTE: has to be _generate to prevent exit
Class cwClass = ClassUtil.makeClassFromFile( cwp );
Object cwInstance = cwClass.newInstance();
Method generateMethod = cwClass.getMethod( "_generate", new Class[] { (new String[] {}).getClass(), Boolean.TYPE } );
// 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 ) {
Method setContextMethod = cwClass.getMethod( "_setContext", new Class[] { Object.class } );
setContextMethod.invoke( cwInstance, new Object[] { context } );
}
boolean throwWriterExceptions = tmps.isYes( Property.main_CodeWriter_throwExceptions );
// DO NOT put System.out in MessageHandler as this will case infinite loop
System.setOut( new PrintStream( out ) );
System.setErr( new PrintStream( err ) );
Object result = generateMethod.invoke( cwInstance, new Object[] { args, new Boolean(throwWriterExceptions) } );
WayPointRecorder.add( BasicWayPoint.ExecutingCodeWriter.make( template.getCodeWriterPath().getAbsolutePath() ) );
if( result instanceof Integer ) {
successful = ( 0 == ((Integer)result).intValue() );
}
else {
successful = true;
}
template.setResult( result );
}
catch( InvocationTargetException ite ) {
if( null != sysOut ) {
System.setOut( sysOut );
}
if( null != sysErr ) {
System.setErr( sysErr );
}
Throwable cause = ite.getCause();
if( null == cause ) {
throw new ProcessException( ite );
}
else {
throw new ProcessException( cause );
}
}
// FIX: need a util to handle general exceptions and just rethrow StandardExceptions
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 );
}
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.error( err );
}
}
}
protected void completeImpl( List pTemplateList ) {
// do nothing
}
}