/*
* Name: InternalJavaCompiler
* 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.Property;
import org.jostraca.UserText;
import org.jostraca.util.Standard;
import org.jostraca.util.ArgUtil;
import org.jostraca.util.PropertySet;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.io.PrintStream;
import java.io.ByteArrayOutputStream;
import javax.management.RuntimeErrorException;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
/** Processing class for compiling Java classes inside the Jostraca process.
*/
public class InternalJavaCompiler extends JavaCompiler {
private static javax.tools.JavaCompiler sJavaCompiler = null;
static {
try {
sJavaCompiler = ToolProvider.getSystemJavaCompiler();
}
catch( Exception e ) {
// leave mJavaCompiler null
}
}
public InternalJavaCompiler() {
// do nothing
}
protected boolean canCompile() {
return null != sJavaCompiler;
}
protected void compileAsFirst( List pTemplateList ) {
List tmlist = pTemplateList;
Template firsttm = (Template) pTemplateList.get(0);
PropertySet tmps = firsttm.getMergedPropertySet();
List sourceFilesList = new ArrayList();
StringBuffer sourceFilesB = new StringBuffer( tmlist.size() * 33 );
boolean compile = makeCompileList( tmlist, sourceFilesB, sourceFilesList );
String sourceFiles = sourceFilesB.toString();
// space out dots to avoid accidental replace on System DOT out search
PrintStream sysOut = System . out;
PrintStream sysErr = System . err;
StandardJavaFileManager sjfm = null;
if( compile ) {
try {
String compilerOpts = tmps.get( Property.main_ExternalCompilerOptions );
if( !JavaUtil.hasClassPathArg(compilerOpts) ) {
compilerOpts
= JavaUtil.ARG_classpath + Standard.SPACE
+ JavaUtil.makeClassPath( tmps )
+ Standard.SPACE
+ compilerOpts
;
}
String argstr = compilerOpts + Standard.SPACE + sourceFiles;
String[] args = ArgUtil.splitQuoted( argstr );
mUserMessageHandler.debug( UserText.get(UserText.TXT_compiling), "javax.tools.JavaCompiler" + Standard.SPACE + argstr );
ByteArrayOutputStream outbaos = new ByteArrayOutputStream();
System.setOut( new PrintStream( outbaos ) );
ByteArrayOutputStream errbaos = new ByteArrayOutputStream();
System.setErr( new PrintStream( errbaos ) );
sjfm = sJavaCompiler.getStandardFileManager(null, null, null);
Iterable javafiles = sjfm.getJavaFileObjectsFromStrings( sourceFilesList );
String[] options = ArgUtil.splitQuoted( compilerOpts );
boolean compileResult = sJavaCompiler.getTask(null, null, null, Arrays.asList(options), null, javafiles).call();
if( !compileResult ) {
throw ProcessException.CODE_compile_errors( argstr + Standard.NEWLINE
+ outbaos.toString() + Standard.NEWLINE
+ errbaos.toString() + Standard.NEWLINE );
}
else {
String outstr = outbaos.toString();
if( null != outstr && !"".equals( outstr ) ) {
mUserMessageHandler.info( outstr );
}
String errstr = errbaos.toString();
if( null != errstr && !"".equals( errstr ) ) {
mUserMessageHandler.error( errstr );
}
}
}
catch( Exception e ) {
e.printStackTrace();
if( e instanceof ProcessException ) {
throw (ProcessException) e;
}
else {
throw ProcessException.CODE_compile_failed( new String[] { "sourcefiles", sourceFiles }, e );
}
}
finally {
if( null != sysOut ) { System.setOut( sysOut ); }
if( null != sysErr ) { System.setErr( sysErr ); }
if( null != sjfm ) {
try {
sjfm.close();
}
catch( Exception e ) {
throw new RuntimeException(e);
}
}
}
}
}
}