001: /*
002: * Copyright 2005-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.javac;
027:
028: import com.sun.tools.javac.main.JavacOption.Option;
029: import com.sun.tools.javac.main.RecognizedOptions.GrumpyHelper;
030: import com.sun.tools.javac.main.RecognizedOptions;
031: import com.sun.tools.javac.util.Version;
032: import java.io.*;
033: import java.net.*;
034: import java.util.*;
035: import java.util.concurrent.*;
036: import java.util.logging.Logger;
037: import javax.tools.*;
038:
039: /**
040: * Java Compiler Server. Can be used to speed up a set of (small)
041: * compilation tasks by caching jar files between compilations.
042: *
043: * <p><b>This is NOT part of any API supported by Sun Microsystems.
044: * If you write code that depends on this, you do so at your own
045: * risk. This code and its internal interfaces are subject to change
046: * or deletion without notice.</b></p>
047: *
048: * @author Peter von der Ahé
049: * @since 1.6
050: */
051: @Version("@(#)Server.java 1.14 07/05/05")
052: class Server implements Runnable {
053: private final BufferedReader in;
054: private final OutputStream out;
055: private final boolean isSocket;
056: private static final JavaCompiler tool = ToolProvider
057: .getSystemJavaCompiler();
058: private static Logger logger = Logger
059: .getLogger("com.sun.tools.javac");
060:
061: static class CwdFileManager extends
062: ForwardingJavaFileManager<JavaFileManager> {
063: String cwd;
064:
065: CwdFileManager(JavaFileManager fileManager) {
066: super (fileManager);
067: }
068:
069: String getAbsoluteName(String name) {
070: if (new File(name).isAbsolute()) {
071: return name;
072: } else {
073: return new File(cwd, name).getPath();
074: }
075: }
076: // public JavaFileObject getFileForInput(String name)
077: // throws IOException
078: // {
079: // return super.getFileForInput(getAbsoluteName(name));
080: // }
081: }
082:
083: // static CwdFileManager fm = new CwdFileManager(tool.getStandardFileManager());
084: static StandardJavaFileManager fm = tool.getStandardFileManager(
085: null, null, null);
086: static {
087: // Use the same file manager for all compilations. This will
088: // cache jar files in the standard file manager. Use
089: // tool.getStandardFileManager().close() to release.
090: // FIXME tool.setFileManager(fm);
091: logger.setLevel(java.util.logging.Level.SEVERE);
092: }
093:
094: private Server(BufferedReader in, OutputStream out, boolean isSocket) {
095: this .in = in;
096: this .out = out;
097: this .isSocket = isSocket;
098: }
099:
100: private Server(BufferedReader in, OutputStream out) {
101: this (in, out, false);
102: }
103:
104: private Server(Socket socket) throws IOException,
105: UnsupportedEncodingException {
106: this (new BufferedReader(new InputStreamReader(socket
107: .getInputStream(), "utf-8")), socket.getOutputStream(),
108: true);
109: }
110:
111: public void run() {
112: List<String> args = new ArrayList<String>();
113: int res = -1;
114: try {
115: String line = null;
116: try {
117: line = in.readLine();
118: } catch (IOException e) {
119: System.err.println(e.getLocalizedMessage());
120: System.exit(0);
121: line = null;
122: }
123: // fm.cwd=null;
124: String cwd = null;
125: while (line != null) {
126: if (line.startsWith("PWD:")) {
127: cwd = line.substring(4);
128: } else if (line.equals("END")) {
129: break;
130: } else if (!"-XDstdout".equals(line)) {
131: args.add(line);
132: }
133: try {
134: line = in.readLine();
135: } catch (IOException e) {
136: System.err.println(e.getLocalizedMessage());
137: System.exit(0);
138: line = null;
139: }
140: }
141: Iterable<File> path = cwd == null ? null : Arrays
142: .<File> asList(new File(cwd));
143: // try { in.close(); } catch (IOException e) {}
144: long msec = System.currentTimeMillis();
145: try {
146: synchronized (tool) {
147: for (StandardLocation location : StandardLocation
148: .values())
149: fm.setLocation(location, path);
150: res = compile(out, fm, args);
151: // FIXME res = tool.run((InputStream)null, null, out, args.toArray(new String[args.size()]));
152: }
153: } catch (Throwable ex) {
154: logger.log(java.util.logging.Level.SEVERE, args
155: .toString(), ex);
156: PrintWriter p = new PrintWriter(out, true);
157: ex.printStackTrace(p);
158: p.flush();
159: }
160: if (res >= 3) {
161: logger.severe(String.format("problem: %s", args));
162: } else {
163: logger.info(String.format("success: %s", args));
164: }
165: // res = compile(args.toArray(new String[args.size()]), out);
166: msec -= System.currentTimeMillis();
167: logger.info(String.format("Real time: %sms", -msec));
168: } finally {
169: if (!isSocket) {
170: try {
171: in.close();
172: } catch (IOException e) {
173: }
174: }
175: try {
176: out.write(String.format("EXIT: %s%n", res).getBytes());
177: } catch (IOException ex) {
178: logger.log(java.util.logging.Level.SEVERE, args
179: .toString(), ex);
180: }
181: try {
182: out.flush();
183: out.close();
184: } catch (IOException ex) {
185: logger.log(java.util.logging.Level.SEVERE, args
186: .toString(), ex);
187: }
188: logger.info(String.format("EXIT: %s", res));
189: }
190: }
191:
192: public static void main(String... args)
193: throws FileNotFoundException {
194: if (args.length == 2) {
195: for (;;) {
196: throw new UnsupportedOperationException("TODO");
197: // BufferedReader in = new BufferedReader(new FileReader(args[0]));
198: // PrintWriter out = new PrintWriter(args[1]);
199: // new Server(in, out).run();
200: // System.out.flush();
201: // System.err.flush();
202: }
203: } else {
204: ExecutorService pool = Executors.newCachedThreadPool();
205: try {
206: ServerSocket socket = new ServerSocket(0xcafe, -1, null);
207: for (;;) {
208: pool.execute(new Server(socket.accept()));
209: }
210: } catch (IOException e) {
211: System.err.format("Error: %s%n", e
212: .getLocalizedMessage());
213: pool.shutdown();
214: }
215: }
216: }
217:
218: private int compile(OutputStream out, StandardJavaFileManager fm,
219: List<String> args) {
220: // FIXME parse args and use getTask
221: // System.err.println("Running " + args);
222: return tool.run(null, null, out, args.toArray(new String[args
223: .size()]));
224: }
225: }
|