duke@1: /* ohair@554: * Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved. duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@1: * duke@1: * This code is free software; you can redistribute it and/or modify it duke@1: * under the terms of the GNU General Public License version 2 only, as ohair@554: * published by the Free Software Foundation. Oracle designates this duke@1: * particular file as subject to the "Classpath" exception as provided ohair@554: * by Oracle in the LICENSE file that accompanied this code. duke@1: * duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@1: * version 2 for more details (a copy is included in the LICENSE file that duke@1: * accompanied this code). duke@1: * duke@1: * You should have received a copy of the GNU General Public License version duke@1: * 2 along with this work; if not, write to the Free Software Foundation, duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@1: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. duke@1: */ duke@1: duke@1: package com.sun.tools.javac; duke@1: duke@1: import java.io.*; duke@1: import java.net.*; duke@1: import java.util.*; duke@1: import java.util.concurrent.*; duke@1: import java.util.logging.Logger; duke@1: import javax.tools.*; duke@1: duke@1: /** duke@1: * Java Compiler Server. Can be used to speed up a set of (small) duke@1: * compilation tasks by caching jar files between compilations. duke@1: * jjg@581: *

This is NOT part of any supported API. duke@1: * If you write code that depends on this, you do so at your own duke@1: * risk. This code and its internal interfaces are subject to change duke@1: * or deletion without notice.

duke@1: * duke@1: * @author Peter von der Ahé duke@1: * @since 1.6 duke@1: */ duke@1: class Server implements Runnable { duke@1: private final BufferedReader in; duke@1: private final OutputStream out; duke@1: private final boolean isSocket; duke@1: private static final JavaCompiler tool = ToolProvider.getSystemJavaCompiler(); duke@1: private static Logger logger = Logger.getLogger("com.sun.tools.javac"); duke@1: static class CwdFileManager extends ForwardingJavaFileManager { duke@1: String cwd; duke@1: CwdFileManager(JavaFileManager fileManager) { duke@1: super(fileManager); duke@1: } duke@1: String getAbsoluteName(String name) { duke@1: if (new File(name).isAbsolute()) { duke@1: return name; duke@1: } else { duke@1: return new File(cwd,name).getPath(); duke@1: } duke@1: } duke@1: // public JavaFileObject getFileForInput(String name) duke@1: // throws IOException duke@1: // { duke@1: // return super.getFileForInput(getAbsoluteName(name)); duke@1: // } duke@1: } duke@1: // static CwdFileManager fm = new CwdFileManager(tool.getStandardFileManager()); duke@1: static StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null); duke@1: static { duke@1: // Use the same file manager for all compilations. This will duke@1: // cache jar files in the standard file manager. Use duke@1: // tool.getStandardFileManager().close() to release. duke@1: // FIXME tool.setFileManager(fm); duke@1: logger.setLevel(java.util.logging.Level.SEVERE); duke@1: } duke@1: private Server(BufferedReader in, OutputStream out, boolean isSocket) { duke@1: this.in = in; duke@1: this.out = out; duke@1: this.isSocket = isSocket; duke@1: } duke@1: private Server(BufferedReader in, OutputStream out) { duke@1: this(in, out, false); duke@1: } duke@1: private Server(Socket socket) throws IOException, UnsupportedEncodingException { duke@1: this(new BufferedReader(new InputStreamReader(socket.getInputStream(), "utf-8")), duke@1: socket.getOutputStream(), duke@1: true); duke@1: } duke@1: public void run() { duke@1: List args = new ArrayList(); duke@1: int res = -1; duke@1: try { duke@1: String line = null; duke@1: try { duke@1: line = in.readLine(); duke@1: } catch (IOException e) { duke@1: System.err.println(e.getLocalizedMessage()); duke@1: System.exit(0); duke@1: line = null; duke@1: } duke@1: // fm.cwd=null; duke@1: String cwd = null; duke@1: while (line != null) { duke@1: if (line.startsWith("PWD:")) { duke@1: cwd = line.substring(4); duke@1: } else if (line.equals("END")) { duke@1: break; duke@1: } else if (!"-XDstdout".equals(line)) { duke@1: args.add(line); duke@1: } duke@1: try { duke@1: line = in.readLine(); duke@1: } catch (IOException e) { duke@1: System.err.println(e.getLocalizedMessage()); duke@1: System.exit(0); duke@1: line = null; duke@1: } duke@1: } duke@1: Iterable path = cwd == null ? null : Arrays.asList(new File(cwd)); duke@1: // try { in.close(); } catch (IOException e) {} duke@1: long msec = System.currentTimeMillis(); duke@1: try { duke@1: synchronized (tool) { duke@1: for (StandardLocation location : StandardLocation.values()) duke@1: fm.setLocation(location, path); duke@1: res = compile(out, fm, args); duke@1: // FIXME res = tool.run((InputStream)null, null, out, args.toArray(new String[args.size()])); duke@1: } duke@1: } catch (Throwable ex) { duke@1: logger.log(java.util.logging.Level.SEVERE, args.toString(), ex); duke@1: PrintWriter p = new PrintWriter(out, true); duke@1: ex.printStackTrace(p); duke@1: p.flush(); duke@1: } duke@1: if (res >= 3) { duke@1: logger.severe(String.format("problem: %s", args)); duke@1: } else { duke@1: logger.info(String.format("success: %s", args)); duke@1: } duke@1: // res = compile(args.toArray(new String[args.size()]), out); duke@1: msec -= System.currentTimeMillis(); duke@1: logger.info(String.format("Real time: %sms", -msec)); duke@1: } finally { duke@1: if (!isSocket) { duke@1: try { in.close(); } catch (IOException e) {} duke@1: } duke@1: try { duke@1: out.write(String.format("EXIT: %s%n", res).getBytes()); duke@1: } catch (IOException ex) { duke@1: logger.log(java.util.logging.Level.SEVERE, args.toString(), ex); duke@1: } duke@1: try { duke@1: out.flush(); duke@1: out.close(); duke@1: } catch (IOException ex) { duke@1: logger.log(java.util.logging.Level.SEVERE, args.toString(), ex); duke@1: } duke@1: logger.info(String.format("EXIT: %s", res)); duke@1: } duke@1: } duke@1: public static void main(String... args) throws FileNotFoundException { duke@1: if (args.length == 2) { duke@1: for (;;) { duke@1: throw new UnsupportedOperationException("TODO"); duke@1: // BufferedReader in = new BufferedReader(new FileReader(args[0])); duke@1: // PrintWriter out = new PrintWriter(args[1]); duke@1: // new Server(in, out).run(); duke@1: // System.out.flush(); duke@1: // System.err.flush(); duke@1: } duke@1: } else { duke@1: ExecutorService pool = Executors.newCachedThreadPool(); duke@1: try duke@1: { duke@1: ServerSocket socket = new ServerSocket(0xcafe, -1, null); duke@1: for (;;) { duke@1: pool.execute(new Server(socket.accept())); duke@1: } duke@1: } duke@1: catch (IOException e) { duke@1: System.err.format("Error: %s%n", e.getLocalizedMessage()); duke@1: pool.shutdown(); duke@1: } duke@1: } duke@1: } duke@1: duke@1: private int compile(OutputStream out, StandardJavaFileManager fm, List args) { duke@1: // FIXME parse args and use getTask duke@1: // System.err.println("Running " + args); duke@1: return tool.run(null, null, out, args.toArray(new String[args.size()])); duke@1: } duke@1: }