001: /*
002: * Portions Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.tools.internal.ws.util;
027:
028: import java.io.OutputStream;
029: import java.io.PrintWriter;
030: import java.lang.reflect.Constructor;
031: import java.lang.reflect.InvocationTargetException;
032: import java.lang.reflect.Method;
033:
034: import com.sun.xml.internal.ws.util.localization.Localizable;
035:
036: /**
037: * A helper class to invoke javac.
038: *
039: * @author WS Development Team
040: */
041: public class JavaCompilerHelper extends ToolBase {
042:
043: public JavaCompilerHelper(OutputStream out) {
044: super (out, " ");
045: this .out = out;
046: }
047:
048: public boolean compile(String[] args) {
049: return internalCompile(args);
050: }
051:
052: protected String getResourceBundleName() {
053: return "com.sun.tools.internal.ws.resources.javacompiler";
054: }
055:
056: protected boolean internalCompile(String[] args) {
057: ClassLoader cl = Thread.currentThread().getContextClassLoader();
058: Class comSunToolsJavacMainClass = null;
059: try {
060: /* try to use the new compiler */
061: comSunToolsJavacMainClass = cl
062: .loadClass("com.sun.tools.javac.Main");
063: try {
064: Method compileMethod = comSunToolsJavacMainClass
065: .getMethod("compile", compile141MethodSignature);
066: try {
067: Object result = compileMethod
068: .invoke(null, new Object[] { args,
069: new PrintWriter(out) });
070: if (!(result instanceof Integer)) {
071: return false;
072: }
073: return ((Integer) result).intValue() == 0;
074: } catch (IllegalAccessException e3) {
075: return false;
076: } catch (IllegalArgumentException e3) {
077: return false;
078: } catch (InvocationTargetException e3) {
079: return false;
080: }
081: } catch (NoSuchMethodException e2) {
082: //tryout 1.3.1 signature
083: return internalCompilePre141(args);
084: //onError(getMessage("javacompiler.nosuchmethod.error", "getMethod(\"compile\", compile141MethodSignature)"));
085: //return false;
086: }
087: } catch (ClassNotFoundException e) {
088: onError(getMessage("javacompiler.classpath.error",
089: "com.sun.tools.javac.Main"));
090: return false;
091: } catch (SecurityException e) {
092: return false;
093: }
094: }
095:
096: protected boolean internalCompilePre141(String[] args) {
097: ClassLoader cl = Thread.currentThread().getContextClassLoader();
098: try {
099: Class sunToolsJavacMainClass = cl
100: .loadClass("sun.tools.javac.Main");
101: try {
102: Constructor constructor = sunToolsJavacMainClass
103: .getConstructor(constructorSignature);
104: try {
105: Object javacMain = constructor
106: .newInstance(new Object[] { out, "javac" });
107: Method compileMethod = sunToolsJavacMainClass
108: .getMethod("compile",
109: compileMethodSignature);
110: Object result = compileMethod.invoke(javacMain,
111: new Object[] { args });
112: if (!(result instanceof Boolean)) {
113: return false;
114: }
115: return ((Boolean) result).booleanValue();
116: } catch (InstantiationException e4) {
117: return false;
118: } catch (IllegalAccessException e4) {
119: return false;
120: } catch (IllegalArgumentException e4) {
121: return false;
122: } catch (InvocationTargetException e4) {
123: return false;
124: }
125:
126: } catch (NoSuchMethodException e3) {
127: onError(getMessage("javacompiler.nosuchmethod.error",
128: "getMethod(\"compile\", compileMethodSignature)"));
129: return false;
130: }
131: } catch (ClassNotFoundException e2) {
132: return false;
133: }
134: }
135:
136: protected String getGenericErrorMessage() {
137: return "javacompiler.error";
138: }
139:
140: protected void run() {
141: }
142:
143: protected boolean parseArguments(String[] args) {
144: return false;
145: }
146:
147: public void onError(Localizable msg) {
148: report(getMessage("javacompiler.error", localizer.localize(msg)));
149: }
150:
151: protected OutputStream out;
152:
153: protected static final Class[] compile141MethodSignature;
154: protected static final Class[] constructorSignature;
155: protected static final Class[] compileMethodSignature;
156:
157: static {
158: compile141MethodSignature = new Class[2];
159: compile141MethodSignature[0] = (new String[0]).getClass();
160: compile141MethodSignature[1] = PrintWriter.class;
161: //jdk version < 1.4.1 signature
162: constructorSignature = new Class[2];
163: constructorSignature[0] = OutputStream.class;
164: constructorSignature[1] = String.class;
165: compileMethodSignature = new Class[1];
166: compileMethodSignature[0] = compile141MethodSignature[0]; // String[]
167:
168: }
169: }
|