Package org.jostraca.process

Source Code of org.jostraca.process.ExternalJavaCompiler

/*
* Name:    ExternalJavaCompiler
* Authors: Richard Rodger
*
* Copyright (c) 2004 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.Property;
import org.jostraca.Template;
import org.jostraca.UserText;

import org.jostraca.util.PropertySet;
import org.jostraca.util.Standard;
import org.jostraca.util.ExecutableCommand;

import java.util.List;

import java.io.File;


/** Processing class for external java compiler executable.
*/
public class ExternalJavaCompiler extends JavaCompiler {


  public ExternalJavaCompiler() {
    // do nothing
  }



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

    if( compile ) {

      String compiler     = tmps.get( Property.main_ExternalCompiler );
      String compilerOpts = tmps.get( Property.main_ExternalCompilerOptions );

      if( !JavaUtil.hasClassPathArg(compilerOpts) ) {
        compilerOpts
          = JavaUtil.ARG_classpath + Standard.SPACE
          + JavaUtil.makeClassPath( tmps )
          + Standard.SPACE
          + compilerOpts
          ;
      }

      ExecutableCommand cmd = new ExecutableCommand( compiler, compilerOpts, sourceFiles );
      String fullcmd = cmd.getFullCmd();

      cmd.setUserMessageHandler( iUserMessageHandler );
      cmd.setActivityDescription( UserText.get( UserText.TXT_compiling ) );

      boolean successful = cmd.execute
        ( new ExecutableCommand.AlternativeExecutableCommands
          (
           // try jikes if javac not found and vice versa
           new ExecutableCommand( JavaUtil.COMPILER_javac.equals( compiler ) ? JavaUtil.COMPILER_jikes : JavaUtil.COMPILER_javac,
                                  compilerOpts, sourceFiles )

           // try to use JAVA_HOME
           ,new ExecutableCommand( "JAVA_HOME/"+JavaUtil.COMPILER_javac, compilerOpts, sourceFiles ) {
               public boolean execute( ExecutableCommand pParent ) {
                 ExecutableCommand jhc = new ExecutableCommand( "cmd", "/c echo %JAVA_HOME%" );
                 if( jhc.execute( new ExecutableCommand.AlternativeExecutableCommands
                   ( new ExecutableCommand( "sh", "-c \"echo $JAVA_HOME\"" ) ) ) ) {
                   String javaHome = jhc.getOutResult().trim();
                   if( !Standard.EMPTY.equals( javaHome ) ) {
                     iCmdPrefix = javaHome + File.separator + "bin" + File.separator + JavaUtil.COMPILER_javac;
                     return execute();
                   }
                   else {
                     return false;
                   }
                 }
                 else {
                   return false;
                 }
               }
             }
           ) );

      if( !successful ) {
        if( cmd.hasErrResult() ) {
          throw ProcessException.CODE_compile_errors( cmd.getExecutedCmd() + Standard.NEWLINE
                                                       + cmd.getOutResult() + Standard.NEWLINE
                                                       + cmd.getErrResult() + Standard.NEWLINE
                                                       );
        }
        else if( cmd.wasNotFound() ) {
          throw ProcessException.CODE_compiler_not_found( compiler );
        }
        else {
          throw ProcessException.CODE_compile_failed( cmd.getExecutedCmd() + Standard.NEWLINE
                                                       + cmd.getOutResult() + Standard.NEWLINE
                                                       + cmd.getErrResult() + Standard.NEWLINE
                                                       );
        }
      }
    }
  }

}




TOP

Related Classes of org.jostraca.process.ExternalJavaCompiler

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.