1 /* |
|
2 * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package com.sun.tools.javac; |
|
27 |
|
28 import java.io.*; |
|
29 import java.net.*; |
|
30 import java.util.*; |
|
31 import java.util.concurrent.*; |
|
32 import java.util.logging.Logger; |
|
33 import javax.tools.*; |
|
34 |
|
35 /** |
|
36 * Java Compiler Server. Can be used to speed up a set of (small) |
|
37 * compilation tasks by caching jar files between compilations. |
|
38 * |
|
39 * <p><b>This is NOT part of any supported API. |
|
40 * If you write code that depends on this, you do so at your own |
|
41 * risk. This code and its internal interfaces are subject to change |
|
42 * or deletion without notice.</b></p> |
|
43 * |
|
44 * @author Peter von der Ahé |
|
45 * @since 1.6 |
|
46 */ |
|
47 @jdk.Supported(false) |
|
48 class Server implements Runnable { |
|
49 private final BufferedReader in; |
|
50 private final OutputStream out; |
|
51 private final boolean isSocket; |
|
52 private static final JavaCompiler tool = ToolProvider.getSystemJavaCompiler(); |
|
53 private static final Logger logger = Logger.getLogger("com.sun.tools.javac"); |
|
54 static class CwdFileManager extends ForwardingJavaFileManager<JavaFileManager> { |
|
55 String cwd; |
|
56 CwdFileManager(JavaFileManager fileManager) { |
|
57 super(fileManager); |
|
58 } |
|
59 String getAbsoluteName(String name) { |
|
60 if (new File(name).isAbsolute()) { |
|
61 return name; |
|
62 } else { |
|
63 return new File(cwd,name).getPath(); |
|
64 } |
|
65 } |
|
66 // public JavaFileObject getFileForInput(String name) |
|
67 // throws IOException |
|
68 // { |
|
69 // return super.getFileForInput(getAbsoluteName(name)); |
|
70 // } |
|
71 } |
|
72 // static CwdFileManager fm = new CwdFileManager(tool.getStandardFileManager()); |
|
73 static final StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null); |
|
74 static { |
|
75 // Use the same file manager for all compilations. This will |
|
76 // cache jar files in the standard file manager. Use |
|
77 // tool.getStandardFileManager().close() to release. |
|
78 // FIXME tool.setFileManager(fm); |
|
79 logger.setLevel(java.util.logging.Level.SEVERE); |
|
80 } |
|
81 private Server(BufferedReader in, OutputStream out, boolean isSocket) { |
|
82 this.in = in; |
|
83 this.out = out; |
|
84 this.isSocket = isSocket; |
|
85 } |
|
86 private Server(BufferedReader in, OutputStream out) { |
|
87 this(in, out, false); |
|
88 } |
|
89 private Server(Socket socket) throws IOException, UnsupportedEncodingException { |
|
90 this(new BufferedReader(new InputStreamReader(socket.getInputStream(), "utf-8")), |
|
91 socket.getOutputStream(), |
|
92 true); |
|
93 } |
|
94 public void run() { |
|
95 List<String> args = new ArrayList<String>(); |
|
96 int res = -1; |
|
97 try { |
|
98 String line = null; |
|
99 try { |
|
100 line = in.readLine(); |
|
101 } catch (IOException e) { |
|
102 System.err.println(e.getLocalizedMessage()); |
|
103 System.exit(0); |
|
104 line = null; |
|
105 } |
|
106 // fm.cwd=null; |
|
107 String cwd = null; |
|
108 while (line != null) { |
|
109 if (line.startsWith("PWD:")) { |
|
110 cwd = line.substring(4); |
|
111 } else if (line.equals("END")) { |
|
112 break; |
|
113 } else if (!"-XDstdout".equals(line)) { |
|
114 args.add(line); |
|
115 } |
|
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 } |
|
124 Iterable<File> path = cwd == null ? null : Arrays.<File>asList(new File(cwd)); |
|
125 // try { in.close(); } catch (IOException e) {} |
|
126 long msec = System.currentTimeMillis(); |
|
127 try { |
|
128 synchronized (tool) { |
|
129 for (StandardLocation location : StandardLocation.values()) |
|
130 fm.setLocation(location, path); |
|
131 res = compile(out, fm, args); |
|
132 // FIXME res = tool.run((InputStream)null, null, out, args.toArray(new String[args.size()])); |
|
133 } |
|
134 } catch (Throwable ex) { |
|
135 logger.log(java.util.logging.Level.SEVERE, args.toString(), ex); |
|
136 PrintWriter p = new PrintWriter(out, true); |
|
137 ex.printStackTrace(p); |
|
138 p.flush(); |
|
139 } |
|
140 if (res >= 3) { |
|
141 logger.severe(String.format("problem: %s", args)); |
|
142 } else { |
|
143 logger.info(String.format("success: %s", args)); |
|
144 } |
|
145 // res = compile(args.toArray(new String[args.size()]), out); |
|
146 msec -= System.currentTimeMillis(); |
|
147 logger.info(String.format("Real time: %sms", -msec)); |
|
148 } finally { |
|
149 if (!isSocket) { |
|
150 try { in.close(); } catch (IOException e) {} |
|
151 } |
|
152 try { |
|
153 out.write(String.format("EXIT: %s%n", res).getBytes()); |
|
154 } catch (IOException ex) { |
|
155 logger.log(java.util.logging.Level.SEVERE, args.toString(), ex); |
|
156 } |
|
157 try { |
|
158 out.flush(); |
|
159 out.close(); |
|
160 } catch (IOException ex) { |
|
161 logger.log(java.util.logging.Level.SEVERE, args.toString(), ex); |
|
162 } |
|
163 logger.info(String.format("EXIT: %s", res)); |
|
164 } |
|
165 } |
|
166 public static void main(String... args) throws FileNotFoundException { |
|
167 if (args.length == 2) { |
|
168 for (;;) { |
|
169 throw new UnsupportedOperationException("TODO"); |
|
170 // BufferedReader in = new BufferedReader(new FileReader(args[0])); |
|
171 // PrintWriter out = new PrintWriter(args[1]); |
|
172 // new Server(in, out).run(); |
|
173 // System.out.flush(); |
|
174 // System.err.flush(); |
|
175 } |
|
176 } else { |
|
177 ExecutorService pool = Executors.newCachedThreadPool(); |
|
178 try |
|
179 { |
|
180 ServerSocket socket = new ServerSocket(0xcafe, -1, null); |
|
181 for (;;) { |
|
182 pool.execute(new Server(socket.accept())); |
|
183 } |
|
184 } |
|
185 catch (IOException e) { |
|
186 System.err.format("Error: %s%n", e.getLocalizedMessage()); |
|
187 pool.shutdown(); |
|
188 } |
|
189 } |
|
190 } |
|
191 |
|
192 private int compile(OutputStream out, StandardJavaFileManager fm, List<String> args) { |
|
193 // FIXME parse args and use getTask |
|
194 // System.err.println("Running " + args); |
|
195 return tool.run(null, null, out, args.toArray(new String[args.size()])); |
|
196 } |
|
197 } |
|