1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/classes/com/sun/tools/javac/Server.java Sat Dec 01 00:00:00 2007 +0000 1.3 @@ -0,0 +1,199 @@ 1.4 +/* 1.5 + * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Sun designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Sun in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.25 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.26 + * have any questions. 1.27 + */ 1.28 + 1.29 +package com.sun.tools.javac; 1.30 + 1.31 +import com.sun.tools.javac.main.JavacOption.Option; 1.32 +import com.sun.tools.javac.main.RecognizedOptions.GrumpyHelper; 1.33 +import com.sun.tools.javac.main.RecognizedOptions; 1.34 +import java.io.*; 1.35 +import java.net.*; 1.36 +import java.util.*; 1.37 +import java.util.concurrent.*; 1.38 +import java.util.logging.Logger; 1.39 +import javax.tools.*; 1.40 + 1.41 +/** 1.42 + * Java Compiler Server. Can be used to speed up a set of (small) 1.43 + * compilation tasks by caching jar files between compilations. 1.44 + * 1.45 + * <p><b>This is NOT part of any API supported by Sun Microsystems. 1.46 + * If you write code that depends on this, you do so at your own 1.47 + * risk. This code and its internal interfaces are subject to change 1.48 + * or deletion without notice.</b></p> 1.49 + * 1.50 + * @author Peter von der Ahé 1.51 + * @since 1.6 1.52 + */ 1.53 +class Server implements Runnable { 1.54 + private final BufferedReader in; 1.55 + private final OutputStream out; 1.56 + private final boolean isSocket; 1.57 + private static final JavaCompiler tool = ToolProvider.getSystemJavaCompiler(); 1.58 + private static Logger logger = Logger.getLogger("com.sun.tools.javac"); 1.59 + static class CwdFileManager extends ForwardingJavaFileManager<JavaFileManager> { 1.60 + String cwd; 1.61 + CwdFileManager(JavaFileManager fileManager) { 1.62 + super(fileManager); 1.63 + } 1.64 + String getAbsoluteName(String name) { 1.65 + if (new File(name).isAbsolute()) { 1.66 + return name; 1.67 + } else { 1.68 + return new File(cwd,name).getPath(); 1.69 + } 1.70 + } 1.71 +// public JavaFileObject getFileForInput(String name) 1.72 +// throws IOException 1.73 +// { 1.74 +// return super.getFileForInput(getAbsoluteName(name)); 1.75 +// } 1.76 + } 1.77 + // static CwdFileManager fm = new CwdFileManager(tool.getStandardFileManager()); 1.78 + static StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null); 1.79 + static { 1.80 + // Use the same file manager for all compilations. This will 1.81 + // cache jar files in the standard file manager. Use 1.82 + // tool.getStandardFileManager().close() to release. 1.83 + // FIXME tool.setFileManager(fm); 1.84 + logger.setLevel(java.util.logging.Level.SEVERE); 1.85 + } 1.86 + private Server(BufferedReader in, OutputStream out, boolean isSocket) { 1.87 + this.in = in; 1.88 + this.out = out; 1.89 + this.isSocket = isSocket; 1.90 + } 1.91 + private Server(BufferedReader in, OutputStream out) { 1.92 + this(in, out, false); 1.93 + } 1.94 + private Server(Socket socket) throws IOException, UnsupportedEncodingException { 1.95 + this(new BufferedReader(new InputStreamReader(socket.getInputStream(), "utf-8")), 1.96 + socket.getOutputStream(), 1.97 + true); 1.98 + } 1.99 + public void run() { 1.100 + List<String> args = new ArrayList<String>(); 1.101 + int res = -1; 1.102 + try { 1.103 + String line = null; 1.104 + try { 1.105 + line = in.readLine(); 1.106 + } catch (IOException e) { 1.107 + System.err.println(e.getLocalizedMessage()); 1.108 + System.exit(0); 1.109 + line = null; 1.110 + } 1.111 + // fm.cwd=null; 1.112 + String cwd = null; 1.113 + while (line != null) { 1.114 + if (line.startsWith("PWD:")) { 1.115 + cwd = line.substring(4); 1.116 + } else if (line.equals("END")) { 1.117 + break; 1.118 + } else if (!"-XDstdout".equals(line)) { 1.119 + args.add(line); 1.120 + } 1.121 + try { 1.122 + line = in.readLine(); 1.123 + } catch (IOException e) { 1.124 + System.err.println(e.getLocalizedMessage()); 1.125 + System.exit(0); 1.126 + line = null; 1.127 + } 1.128 + } 1.129 + Iterable<File> path = cwd == null ? null : Arrays.<File>asList(new File(cwd)); 1.130 + // try { in.close(); } catch (IOException e) {} 1.131 + long msec = System.currentTimeMillis(); 1.132 + try { 1.133 + synchronized (tool) { 1.134 + for (StandardLocation location : StandardLocation.values()) 1.135 + fm.setLocation(location, path); 1.136 + res = compile(out, fm, args); 1.137 + // FIXME res = tool.run((InputStream)null, null, out, args.toArray(new String[args.size()])); 1.138 + } 1.139 + } catch (Throwable ex) { 1.140 + logger.log(java.util.logging.Level.SEVERE, args.toString(), ex); 1.141 + PrintWriter p = new PrintWriter(out, true); 1.142 + ex.printStackTrace(p); 1.143 + p.flush(); 1.144 + } 1.145 + if (res >= 3) { 1.146 + logger.severe(String.format("problem: %s", args)); 1.147 + } else { 1.148 + logger.info(String.format("success: %s", args)); 1.149 + } 1.150 + // res = compile(args.toArray(new String[args.size()]), out); 1.151 + msec -= System.currentTimeMillis(); 1.152 + logger.info(String.format("Real time: %sms", -msec)); 1.153 + } finally { 1.154 + if (!isSocket) { 1.155 + try { in.close(); } catch (IOException e) {} 1.156 + } 1.157 + try { 1.158 + out.write(String.format("EXIT: %s%n", res).getBytes()); 1.159 + } catch (IOException ex) { 1.160 + logger.log(java.util.logging.Level.SEVERE, args.toString(), ex); 1.161 + } 1.162 + try { 1.163 + out.flush(); 1.164 + out.close(); 1.165 + } catch (IOException ex) { 1.166 + logger.log(java.util.logging.Level.SEVERE, args.toString(), ex); 1.167 + } 1.168 + logger.info(String.format("EXIT: %s", res)); 1.169 + } 1.170 + } 1.171 + public static void main(String... args) throws FileNotFoundException { 1.172 + if (args.length == 2) { 1.173 + for (;;) { 1.174 + throw new UnsupportedOperationException("TODO"); 1.175 +// BufferedReader in = new BufferedReader(new FileReader(args[0])); 1.176 +// PrintWriter out = new PrintWriter(args[1]); 1.177 +// new Server(in, out).run(); 1.178 +// System.out.flush(); 1.179 +// System.err.flush(); 1.180 + } 1.181 + } else { 1.182 + ExecutorService pool = Executors.newCachedThreadPool(); 1.183 + try 1.184 + { 1.185 + ServerSocket socket = new ServerSocket(0xcafe, -1, null); 1.186 + for (;;) { 1.187 + pool.execute(new Server(socket.accept())); 1.188 + } 1.189 + } 1.190 + catch (IOException e) { 1.191 + System.err.format("Error: %s%n", e.getLocalizedMessage()); 1.192 + pool.shutdown(); 1.193 + } 1.194 + } 1.195 + } 1.196 + 1.197 + private int compile(OutputStream out, StandardJavaFileManager fm, List<String> args) { 1.198 + // FIXME parse args and use getTask 1.199 + // System.err.println("Running " + args); 1.200 + return tool.run(null, null, out, args.toArray(new String[args.size()])); 1.201 + } 1.202 +}