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.2 $
022: */package org.apache.harmony.rmi.common;
023:
024: import java.lang.reflect.InvocationTargetException;
025:
026: /**
027: * This class represents an Eclipse Java Compiler
028: * executed with a simple Java call.
029: *
030: * @author Vasily Zakharov
031: * @version $Revision: 1.1.2.2 $
032: */
033: public class EclipseJavaCompiler extends MethodJavaCompiler {
034:
035: /**
036: * Eclipse Compiler class name.
037: */
038: public static final String ECLIPSE_COMPILER_CLASS_NAME = "org.eclipse.jdt.internal.compiler.batch.Main"; //$NON-NLS-1$
039:
040: /**
041: * Eclipse Compiler method name.
042: */
043: protected final static String ECLIPSE_COMPILER_METHOD_NAME = "compile"; //$NON-NLS-1$
044:
045: /**
046: * Eclipse Compiler method signature.
047: */
048: protected static final Class[] ECLIPSE_COMPILER_METHOD_SIGNATURE = new Class[] { String.class };
049:
050: /**
051: * Name of the environment variable specifying the Eclipse location.
052: */
053: public static final String ECLIPSE_HOME_VARIABLE = "ECLIPSE_HOME"; //$NON-NLS-1$
054:
055: /**
056: * Path from {@link #ECLIPSE_HOME_VARIABLE ECLIPSE_HOME}
057: * to {@linkplain #ECLIPSE_JAR_PATTERN Eclipse Compiler JAR}.
058: */
059: public static final String ECLIPSE_JAR_PATH = "plugins"; //$NON-NLS-1$
060:
061: /**
062: * File name pattern describing Eclipse Compiler JAR file name.
063: */
064: public static final String ECLIPSE_JAR_PATTERN = "^org.eclipse.jdt.core_.+\\.jar$"; //$NON-NLS-1$
065:
066: /**
067: * Creates instance of this class, equivalent to
068: * {@link #EclipseJavaCompiler(ClassLoader) EclipseJavaCompiler(null)}.
069: *
070: * @throws JavaCompilerException
071: * If error occurs while finding or loading
072: * the Eclipse Compiler class or method.
073: */
074: public EclipseJavaCompiler() throws JavaCompilerException {
075: this (null);
076: }
077:
078: /**
079: * Creates instance of this class by trying to load the
080: * {@linkplain #ECLIPSE_COMPILER_CLASS_NAME Eclipse Compiler class}
081: * with the specified class loader.
082: *
083: * If the specified class loader is <code>null</code>, tries to load
084: * with the default class loader, and if failed, tries to load
085: * from {@link #ECLIPSE_HOME_VARIABLE ECLIPSE_HOME} location.
086: *
087: * @param classLoader
088: * Class loader to use to load the compiler class.
089: * Can be <code>null</code>.
090: *
091: * @throws JavaCompilerException
092: * If error occurs while finding or loading
093: * the Eclipse Compiler class or method.
094: */
095: public EclipseJavaCompiler(ClassLoader classLoader)
096: throws JavaCompilerException {
097: Class compilerClass = null;
098:
099: try {
100: compilerClass = getClass(ECLIPSE_COMPILER_CLASS_NAME,
101: classLoader);
102: } catch (JavaCompilerException e) {
103: if (classLoader != null) {
104: throw e;
105: }
106: }
107:
108: if (compilerClass == null) {
109: compilerClass = getClass(
110: ECLIPSE_COMPILER_CLASS_NAME,
111: getClassLoaderFromJarFile(getFileFromVariable(
112: ECLIPSE_HOME_VARIABLE, ECLIPSE_JAR_PATH,
113: ECLIPSE_JAR_PATTERN, "Eclipse Compiler JAR"))); //$NON-NLS-1$
114: }
115:
116: compilerMethod = getMethod(compilerClass,
117: ECLIPSE_COMPILER_METHOD_NAME,
118: ECLIPSE_COMPILER_METHOD_SIGNATURE);
119: }
120:
121: /**
122: * {@inheritDoc}
123: */
124: public int compile(String commandString)
125: throws JavaCompilerException {
126: try {
127: Object ret = compilerMethod.invoke(null,
128: new Object[] { commandString });
129: return (((Boolean) ret).booleanValue() ? 0 : -1);
130: } catch (IllegalAccessException e) {
131: throw new JavaCompilerException(e);
132: } catch (InvocationTargetException e) {
133: throw new JavaCompilerException(e.getCause());
134: }
135: }
136:
137: /**
138: * {@inheritDoc}
139: */
140: protected int run(String[] args) throws JavaCompilerException {
141: StringBuffer buffer = new StringBuffer();
142:
143: for (int i = 0; i < args.length; i++) {
144: if (i > 0) {
145: buffer.append(' ');
146: }
147: buffer.append(args[i]);
148: }
149: return compile(buffer.toString());
150: }
151: }
|