001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: *
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: /**
020: * @author Vasily Zakharov
021: * @version $Revision: 1.1.2.3 $
022: */package org.apache.harmony.rmi.common;
023:
024: import java.io.IOException;
025:
026: /**
027: * This class represents a Java Compiler executed as an external program.
028: *
029: * @author Vasily Zakharov
030: * @version $Revision: 1.1.2.3 $
031: */
032: public class ExecJavaCompiler extends JavaCompiler {
033:
034: /**
035: * Default name of a program to execute.
036: */
037: public static final String DEFAULT_COMPILER_PROGRAM = "javac"; //$NON-NLS-1$
038:
039: /**
040: * Name of the system variable specifying the Java location.
041: */
042: public static final String JAVA_HOME_VARIABLE = "JAVA_HOME"; //$NON-NLS-1$
043:
044: /**
045: * Path from {@link #JAVA_HOME_VARIABLE JAVA_HOME}
046: * to {@linkplain #JAVA_COMPILER_PATTERN Javac executable}.
047: */
048: public static final String JAVA_COMPILER_PATH = "bin"; //$NON-NLS-1$
049:
050: /**
051: * File name pattern describing Javac executable file name.
052: */
053: public static final String JAVA_COMPILER_PATTERN = "((javac)|(JAVAC))(|(\\.(exe|EXE)))"; //$NON-NLS-1$
054:
055: /**
056: * Creates uninitialized instance of this class.
057: * Note that using this constructor in most cases
058: * requires overriding {@link #run(String[])} method.
059: */
060: protected ExecJavaCompiler() {
061: }
062:
063: /**
064: * Configures this class to use the
065: * {@linkplain #DEFAULT_COMPILER_PROGRAM default program name} to compile.
066: *
067: * @param search
068: * If <code>true</code> the constructor tries to locate the Javac
069: * executable at {@link #JAVA_HOME_VARIABLE JAVA_HOME} location,
070: * this is equivalent to {@link #ExecJavaCompiler(String)
071: * ExecJavaCompiler(null)}.
072: * Otherwise, the {@linkplain #DEFAULT_COMPILER_PROGRAM}
073: * is used in hope that it would be found in system path,
074: * this is equivalent to {@link #ExecJavaCompiler(String)
075: * ExecJavaCompiler(DEFAULT_COMPILER_PROGRAM)}.
076: *
077: * @throws JavaCompilerException
078: * If error occurs during searching for executable
079: * at {@link #JAVA_HOME_VARIABLE}.
080: */
081: public ExecJavaCompiler(boolean search)
082: throws JavaCompilerException {
083: this (search ? null : DEFAULT_COMPILER_PROGRAM);
084: }
085:
086: /**
087: * Configures this class to use the specified program to compile.
088: * If <code>programName</code> is <code>null</code>, tries to locate
089: * the Javac executable at {@link #JAVA_HOME_VARIABLE JAVA_HOME} location.
090: *
091: * @param programName
092: * Name of the program to execute. Can be <code>null</code>.
093: *
094: * @throws JavaCompilerException
095: * If <code>programName</code> is <code>null</code>
096: * and error occurs during searching for Javac executable
097: * at {@link #JAVA_HOME_VARIABLE}.
098: */
099: public ExecJavaCompiler(String programName)
100: throws JavaCompilerException {
101: compilerOptions = new String[] { (programName == null) ? getFileFromVariable(
102: JAVA_HOME_VARIABLE, JAVA_COMPILER_PATH,
103: JAVA_COMPILER_PATTERN, "Java compiler").getPath() //$NON-NLS-1$
104: : programName };
105: }
106:
107: /**
108: * Runs the compilation process with the specified arguments.
109: *
110: * @param args
111: * Full arguments list.
112: *
113: * @return Java Compiler return value.
114: *
115: * @throws JavaCompilerException
116: * If execution failed for some reason.
117: */
118: protected int run(String[] args) throws JavaCompilerException {
119: try {
120: // @ToDo: Rewrite with ProcessBuilder for Java 5.0.
121: return new SubProcess(args, System.out, null, System.err)
122: .waitFor();
123: } catch (IOException e) {
124: throw new JavaCompilerException(e);
125: }
126: }
127: }
|