Package org.jostraca.process

Source Code of org.jostraca.process.InternalJavaCompiler

/*
* 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.List;

import java.io.PrintStream;
import java.io.ByteArrayOutputStream;


/** Processing class for compiling Java classes inside the Jostraca process.
*/
public class InternalJavaCompiler extends JavaCompiler {

  public static final String COMPILER_CLASS = "com.sun.tools.javac.Main";
  public static final String COMPILE_METHOD = "compile";


  private static Class   sCompilerClass    = null;
  private static Object  sCompilerInstance = null;
  private static Method  sCompileMethod    = null;
  private static boolean sCanCompile       = false;    


  static {
    try {
      sCompilerClass    = Class.forName( COMPILER_CLASS );
      sCompilerInstance = sCompilerClass.newInstance();
      sCompileMethod    = sCompilerClass.getMethod( COMPILE_METHOD, new Class[] { (new String[] {}).getClass() } );
      sCanCompile       = true;
    }
    catch( Exception e ) {
      // this object may be used when COMPILER_CLASS is not available
      // FIX: Exception thrown when compile attempted
      sCanCompile       = false;
    }
  }



  public InternalJavaCompiler() {
    // do nothing
  }

 
  protected boolean canCompile() {
    return sCanCompile;
  }


  protected void compileAsFirst( List pTemplateList ) {
    List        tmlist  = pTemplateList;
    Template    firsttm = (Template) pTemplateList.get(0);
    PropertySet tmps    = firsttm.getMergedPropertySet();

    StringBuffer sourceFilesB = new StringBuffer( tmlist.size() * 33 );
    boolean      compile      = makeCompileList( tmlist, sourceFilesB );
    String       sourceFiles  = sourceFilesB.toString();

    // space out dots to avoid accidental replace on System DOT out search
    PrintStream sysOut = System . out;
    PrintStream sysErr = System . err;

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

        iUserMessageHandler.debug( UserText.get(UserText.TXT_compiling), COMPILER_CLASS + Standard.SPACE + argstr );

        //MessageHandlerOutputStream  out  = new MessageHandlerOutputStream( UserMessageHandler.INFO,  iUserMessageHandler );
        //MessageHandlerOutputStream  err  = new MessageHandlerOutputStream( UserMessageHandler.ERROR, iUserMessageHandler );

        ByteArrayOutputStream outbaos = new ByteArrayOutputStream();   
        System.setOut( new PrintStream( outbaos ) );

        ByteArrayOutputStream errbaos = new ByteArrayOutputStream();   
        System.setErr( new PrintStream( errbaos ) );

        int compileResult = ( (Integer) sCompileMethod.invoke( sCompilerInstance, new Object[] { args } ) ).intValue();

        if( 0 != 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 ) ) {
            iUserMessageHandler.info( outstr );
          }

          String errstr = errbaos.toString();
          if( null != errstr && !"".equals( errstr ) ) {
            iUserMessageHandler.error( errstr );
          }
        }
      }
      catch( Exception e ) {
        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 ); }
      }
    }
  }

}




TOP

Related Classes of org.jostraca.process.InternalJavaCompiler

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.