001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.tools.util;
019:
020: import java.io.File;
021: import java.io.FileWriter;
022: import java.io.IOException;
023: import java.io.PrintWriter;
024: import java.util.ArrayList;
025: import java.util.Arrays;
026: import java.util.List;
027:
028: public class Compiler {
029: public boolean compileFiles(String[] files, File outputDir) {
030: List<String> list = new ArrayList<String>();
031: list.add("javac");
032:
033: if (outputDir != null) {
034: list.add("-d");
035: list.add(outputDir.getAbsolutePath().replace(
036: File.pathSeparatorChar, '/'));
037: }
038:
039: String javaClasspath = System.getProperty("java.class.path");
040: boolean classpathSetted = javaClasspath != null ? true : false;
041: if (!classpathSetted) {
042: list.add("-extdirs");
043: list.add(getClass().getClassLoader().getResource(".")
044: .getFile()
045: + "../lib/");
046: } else {
047: list.add("-classpath");
048: list.add(javaClasspath);
049: }
050:
051: int idx = list.size();
052: list.addAll(Arrays.asList(files));
053:
054: return internalCompile(list.toArray(new String[list.size()]),
055: idx);
056: }
057:
058: public boolean internalCompile(String[] args, int sourceFileIndex) {
059: Process p = null;
060: String cmdArray[] = null;
061:
062: try {
063: if (isLongCommandLines(args) && sourceFileIndex >= 0) {
064: PrintWriter out = null;
065: File tmpFile = File
066: .createTempFile("cxf-compiler", null);
067: tmpFile.deleteOnExit();
068: out = new PrintWriter(new FileWriter(tmpFile));
069: for (int i = sourceFileIndex; i < args.length; i++) {
070: if (args[i].indexOf(" ") > -1) {
071: args[i] = args[i].replace(File.separatorChar,
072: '/');
073: //
074: // javac gives an error if you use forward slashes
075: // with package-info.java. Refer to:
076: // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6198196
077: //
078: if (args[i].indexOf("package-info.java") > -1
079: && System.getProperty("os.name")
080: .toLowerCase().indexOf(
081: "windows") > -1) {
082: out.println("\""
083: + args[i].replaceAll("/",
084: "\\\\\\\\") + "\"");
085: } else {
086: out.println("\"" + args[i] + "\"");
087: }
088: } else {
089: out.println(args[i]);
090: }
091: }
092: out.flush();
093: out.close();
094: cmdArray = new String[sourceFileIndex + 1];
095: System.arraycopy(args, 0, cmdArray, 0, sourceFileIndex);
096: cmdArray[sourceFileIndex] = "@" + tmpFile;
097: } else {
098: cmdArray = new String[args.length];
099: System.arraycopy(args, 0, cmdArray, 0, args.length);
100: }
101:
102: if (System.getProperty("os.name").toLowerCase().indexOf(
103: "windows") > -1) {
104: for (int i = 0; i < cmdArray.length; i++) {
105: if (cmdArray[i].indexOf("package-info") == -1) {
106: cmdArray[i] = cmdArray[i].replace('\\', '/');
107: }
108: }
109: }
110:
111: p = Runtime.getRuntime().exec(cmdArray);
112:
113: if (p.getErrorStream() != null) {
114: StreamPrinter errorStreamPrinter = new StreamPrinter(p
115: .getErrorStream(), "", System.out);
116: errorStreamPrinter.run();
117: }
118:
119: if (p.getInputStream() != null) {
120: StreamPrinter infoStreamPrinter = new StreamPrinter(p
121: .getInputStream(), "[INFO]", System.out);
122: infoStreamPrinter.run();
123: }
124:
125: if (p != null) {
126: return p.waitFor() == 0 ? true : false;
127: }
128: } catch (SecurityException e) {
129: System.err
130: .println("[ERROR] SecurityException during exec() of compiler \""
131: + args[0] + "\".");
132: } catch (InterruptedException e) {
133: // ignore
134:
135: } catch (IOException e) {
136: System.err
137: .print("[ERROR] IOException during exec() of compiler \""
138: + args[0] + "\"");
139: System.err
140: .println(". Check your path environment variable.");
141: }
142:
143: return false;
144: }
145:
146: private boolean isLongCommandLines(String args[]) {
147: StringBuffer strBuffer = new StringBuffer();
148: for (int i = 0; i < args.length; i++) {
149: strBuffer.append(args[i]);
150: }
151: return strBuffer.toString().length() > 4096 ? true : false;
152: }
153: }
|