Wed, 27 Apr 2016 01:34:52 +0800
Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17
1 /*
2 * Copyright (c) 2012, 2013, 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 */
26 package com.sun.tools.sjavac.server;
28 import java.io.BufferedReader;
29 import java.io.File;
30 import java.io.IOException;
31 import java.io.InputStreamReader;
32 import java.io.OutputStreamWriter;
33 import java.io.PrintWriter;
34 import java.io.StringWriter;
35 import java.net.Socket;
36 import java.net.URI;
37 import java.net.URISyntaxException;
38 import java.util.ArrayList;
39 import java.util.Arrays;
40 import java.util.HashSet;
41 import java.util.List;
42 import java.util.Set;
43 import java.util.Map;
44 import java.util.concurrent.Future;
45 import javax.tools.JavaFileManager;
46 import javax.tools.JavaFileObject;
47 import javax.tools.StandardJavaFileManager;
49 import com.sun.tools.javac.util.Context;
50 import com.sun.tools.javac.util.Log;
51 import com.sun.tools.javac.util.BaseFileManager;
52 import com.sun.tools.javac.util.StringUtils;
53 import com.sun.tools.sjavac.comp.Dependencies;
54 import com.sun.tools.sjavac.comp.JavaCompilerWithDeps;
55 import com.sun.tools.sjavac.comp.SmartFileManager;
56 import com.sun.tools.sjavac.comp.ResolveWithDeps;
58 /**
59 * The compiler thread maintains a JavaCompiler instance and
60 * can receive a request from the client, perform the compilation
61 * requested and report back the results.
62 *
63 * * <p><b>This is NOT part of any supported API.
64 * If you write code that depends on this, you do so at your own
65 * risk. This code and its internal interfaces are subject to change
66 * or deletion without notice.</b></p>
67 */
68 public class CompilerThread implements Runnable {
69 private JavacServer javacServer;
70 private CompilerPool compilerPool;
71 private List<Future<?>> subTasks;
73 // Communicating over this socket.
74 private Socket socket;
76 // The necessary classes to do a compilation.
77 private com.sun.tools.javac.api.JavacTool compiler;
78 private StandardJavaFileManager fileManager;
79 private BaseFileManager fileManagerBase;
80 private SmartFileManager smartFileManager;
81 private Context context;
83 // If true, then this thread is serving a request.
84 private boolean inUse = false;
86 CompilerThread(CompilerPool cp) {
87 compilerPool = cp;
88 javacServer = cp.getJavacServer();
89 }
91 /**
92 * Execute a minor task, for example generating bytecodes and writing them to disk,
93 * that belong to a major compiler thread task.
94 */
95 public synchronized void executeSubtask(Runnable r) {
96 subTasks.add(compilerPool.executeSubtask(this, r));
97 }
99 /**
100 * Count the number of active sub tasks.
101 */
102 public synchronized int numActiveSubTasks() {
103 int c = 0;
104 for (Future<?> f : subTasks) {
105 if (!f.isDone() && !f.isCancelled()) {
106 c++;
107 }
108 }
109 return c;
110 }
112 /**
113 * Use this socket for the upcoming request.
114 */
115 public void setSocket(Socket s) {
116 socket = s;
117 }
119 /**
120 * Prepare the compiler thread for use. It is not yet started.
121 * It will be started by the executor service.
122 */
123 public synchronized void use() {
124 assert(!inUse);
125 inUse = true;
126 compiler = com.sun.tools.javac.api.JavacTool.create();
127 fileManager = compiler.getStandardFileManager(null, null, null);
128 fileManagerBase = (BaseFileManager)fileManager;
129 smartFileManager = new SmartFileManager(fileManager);
130 context = new Context();
131 context.put(JavaFileManager.class, smartFileManager);
132 ResolveWithDeps.preRegister(context);
133 JavaCompilerWithDeps.preRegister(context, this);
134 subTasks = new ArrayList<Future<?>>();
135 }
137 /**
138 * Prepare the compiler thread for idleness.
139 */
140 public synchronized void unuse() {
141 assert(inUse);
142 inUse = false;
143 compiler = null;
144 fileManager = null;
145 fileManagerBase = null;
146 smartFileManager = null;
147 context = null;
148 subTasks = null;
149 }
151 /**
152 * Expect this key on the next line read from the reader.
153 */
154 private static boolean expect(BufferedReader in, String key) throws IOException {
155 String s = in.readLine();
156 if (s != null && s.equals(key)) {
157 return true;
158 }
159 return false;
160 }
162 // The request identifier, for example GENERATE_NEWBYTECODE
163 String id = "";
165 public String currentRequestId() {
166 return id;
167 }
169 PrintWriter stdout;
170 PrintWriter stderr;
171 int forcedExitCode = 0;
173 public void logError(String msg) {
174 stderr.println(msg);
175 forcedExitCode = -1;
176 }
178 /**
179 * Invoked by the executor service.
180 */
181 public void run() {
182 // Unique nr that identifies this request.
183 int thisRequest = compilerPool.startRequest();
184 long start = System.currentTimeMillis();
185 int numClasses = 0;
186 StringBuilder compiledPkgs = new StringBuilder();
187 use();
189 PrintWriter out = null;
190 try {
191 javacServer.log("<"+thisRequest+"> Connect from "+socket.getRemoteSocketAddress()+" activethreads="+compilerPool.numActiveRequests());
192 BufferedReader in = new BufferedReader(new InputStreamReader(
193 socket.getInputStream()));
194 out = new PrintWriter(new OutputStreamWriter(
195 socket.getOutputStream()));
196 if (!expect(in, JavacServer.PROTOCOL_COOKIE_VERSION)) {
197 javacServer.log("<"+thisRequest+"> Bad protocol from ip "+socket.getRemoteSocketAddress());
198 return;
199 }
201 String cookie = in.readLine();
202 if (cookie == null || !cookie.equals(""+javacServer.getCookie())) {
203 javacServer.log("<"+thisRequest+"> Bad cookie from ip "+socket.getRemoteSocketAddress());
204 return;
205 }
206 if (!expect(in, JavacServer.PROTOCOL_CWD)) {
207 return;
208 }
209 String cwd = in.readLine();
210 if (cwd == null)
211 return;
212 if (!expect(in, JavacServer.PROTOCOL_ID)) {
213 return;
214 }
215 id = in.readLine();
216 if (id == null)
217 return;
218 if (!expect(in, JavacServer.PROTOCOL_ARGS)) {
219 return;
220 }
221 ArrayList<String> the_options = new ArrayList<String>();
222 ArrayList<File> the_classes = new ArrayList<File>();
223 Iterable<File> path = Arrays.<File> asList(new File(cwd));
225 for (;;) {
226 String l = in.readLine();
227 if (l == null)
228 return;
229 if (l.equals(JavacServer.PROTOCOL_SOURCES_TO_COMPILE))
230 break;
231 if (l.startsWith("--server:"))
232 continue;
233 if (!l.startsWith("-") && l.endsWith(".java")) {
234 the_classes.add(new File(l));
235 numClasses++;
236 } else {
237 the_options.add(l);
238 }
239 continue;
240 }
242 // Load sources to compile
243 Set<URI> sourcesToCompile = new HashSet<URI>();
244 for (;;) {
245 String l = in.readLine();
246 if (l == null)
247 return;
248 if (l.equals(JavacServer.PROTOCOL_VISIBLE_SOURCES))
249 break;
250 try {
251 sourcesToCompile.add(new URI(l));
252 numClasses++;
253 } catch (URISyntaxException e) {
254 return;
255 }
256 }
257 // Load visible sources
258 Set<URI> visibleSources = new HashSet<URI>();
259 boolean fix_drive_letter_case =
260 StringUtils.toLowerCase(System.getProperty("os.name")).startsWith("windows");
261 for (;;) {
262 String l = in.readLine();
263 if (l == null)
264 return;
265 if (l.equals(JavacServer.PROTOCOL_END))
266 break;
267 try {
268 URI u = new URI(l);
269 if (fix_drive_letter_case) {
270 // Make sure the driver letter is lower case.
271 String s = u.toString();
272 if (s.startsWith("file:/") &&
273 Character.isUpperCase(s.charAt(6))) {
274 u = new URI("file:/"+Character.toLowerCase(s.charAt(6))+s.substring(7));
275 }
276 }
277 visibleSources.add(u);
278 } catch (URISyntaxException e) {
279 return;
280 }
281 }
283 // A completed request has been received.
285 // Now setup the actual compilation....
286 // First deal with explicit source files on cmdline and in at file.
287 com.sun.tools.javac.util.ListBuffer<JavaFileObject> compilationUnits =
288 new com.sun.tools.javac.util.ListBuffer<JavaFileObject>();
289 for (JavaFileObject i : fileManager.getJavaFileObjectsFromFiles(the_classes)) {
290 compilationUnits.append(i);
291 }
292 // Now deal with sources supplied as source_to_compile.
293 com.sun.tools.javac.util.ListBuffer<File> sourcesToCompileFiles =
294 new com.sun.tools.javac.util.ListBuffer<File>();
295 for (URI u : sourcesToCompile) {
296 sourcesToCompileFiles.append(new File(u));
297 }
298 for (JavaFileObject i : fileManager.getJavaFileObjectsFromFiles(sourcesToCompileFiles)) {
299 compilationUnits.append(i);
300 }
301 // Log the options to be used.
302 StringBuilder options = new StringBuilder();
303 for (String s : the_options) {
304 options.append(">").append(s).append("< ");
305 }
306 javacServer.log(id+" <"+thisRequest+"> options "+options.toString());
308 forcedExitCode = 0;
309 // Create a new logger.
310 StringWriter stdoutLog = new StringWriter();
311 StringWriter stderrLog = new StringWriter();
312 stdout = new PrintWriter(stdoutLog);
313 stderr = new PrintWriter(stderrLog);
314 com.sun.tools.javac.main.Main.Result rc = com.sun.tools.javac.main.Main.Result.OK;
315 try {
316 if (compilationUnits.size() > 0) {
317 // Bind the new logger to the existing context.
318 context.put(Log.outKey, stderr);
319 Log.instance(context).setWriter(Log.WriterKind.NOTICE, stdout);
320 Log.instance(context).setWriter(Log.WriterKind.WARNING, stderr);
321 Log.instance(context).setWriter(Log.WriterKind.ERROR, stderr);
322 // Process the options.
323 com.sun.tools.javac.api.JavacTool.processOptions(context, smartFileManager, the_options);
324 fileManagerBase.setContext(context);
325 smartFileManager.setVisibleSources(visibleSources);
326 smartFileManager.cleanArtifacts();
327 smartFileManager.setLog(stdout);
328 Dependencies.instance(context).reset();
330 com.sun.tools.javac.main.Main ccompiler = new com.sun.tools.javac.main.Main("javacTask", stderr);
331 String[] aa = the_options.toArray(new String[0]);
333 // Do the compilation!
334 rc = ccompiler.compile(aa, context, compilationUnits.toList(), null);
336 while (numActiveSubTasks()>0) {
337 try { Thread.sleep(1000); } catch (InterruptedException e) { }
338 }
340 smartFileManager.flush();
341 }
342 } catch (Exception e) {
343 stderr.println(e.getMessage());
344 forcedExitCode = -1;
345 }
347 // Send the response..
348 out.println(JavacServer.PROTOCOL_STDOUT);
349 out.print(stdoutLog);
350 out.println(JavacServer.PROTOCOL_STDERR);
351 out.print(stderrLog);
352 // The compilation is complete! And errors will have already been printed on out!
353 out.println(JavacServer.PROTOCOL_PACKAGE_ARTIFACTS);
354 Map<String,Set<URI>> pa = smartFileManager.getPackageArtifacts();
355 for (String aPkgName : pa.keySet()) {
356 out.println("+"+aPkgName);
357 Set<URI> as = pa.get(aPkgName);
358 for (URI a : as) {
359 out.println(" "+a.toString());
360 }
361 }
362 Dependencies deps = Dependencies.instance(context);
363 out.println(JavacServer.PROTOCOL_PACKAGE_DEPENDENCIES);
364 Map<String,Set<String>> pd = deps.getDependencies();
365 for (String aPkgName : pd.keySet()) {
366 out.println("+"+aPkgName);
367 Set<String> ds = pd.get(aPkgName);
368 // Everything depends on java.lang
369 if (!ds.contains(":java.lang")) ds.add(":java.lang");
370 for (String d : ds) {
371 out.println(" "+d);
372 }
373 }
374 out.println(JavacServer.PROTOCOL_PACKAGE_PUBLIC_APIS);
375 Map<String,String> pp = deps.getPubapis();
376 for (String aPkgName : pp.keySet()) {
377 out.println("+"+aPkgName);
378 String ps = pp.get(aPkgName);
379 // getPubapis added a space to each line!
380 out.println(ps);
381 compiledPkgs.append(aPkgName+" ");
382 }
383 out.println(JavacServer.PROTOCOL_SYSINFO);
384 out.println("num_cores=" + Runtime.getRuntime().availableProcessors());
385 out.println("max_memory=" + Runtime.getRuntime().maxMemory());
386 out.println(JavacServer.PROTOCOL_RETURN_CODE);
388 // Errors from sjavac that affect compilation status!
389 int rcv = rc.exitCode;
390 if (rcv == 0 && forcedExitCode != 0) {
391 rcv = forcedExitCode;
392 }
393 out.println("" + rcv);
394 out.println(JavacServer.PROTOCOL_END);
395 out.flush();
396 } catch (IOException e) {
397 e.printStackTrace();
398 } finally {
399 try {
400 if (out != null) out.close();
401 if (!socket.isClosed()) {
402 socket.close();
403 }
404 socket = null;
405 } catch (Exception e) {
406 javacServer.log("ERROR "+e);
407 e.printStackTrace();
408 }
409 compilerPool.stopRequest();
410 long duration = System.currentTimeMillis()-start;
411 javacServer.addBuildTime(duration);
412 float classpersec = ((float)numClasses)*(((float)1000.0)/((float)duration));
413 javacServer.log(id+" <"+thisRequest+"> "+compiledPkgs+" duration " + duration+ " ms num_classes="+numClasses+
414 " classpersec="+classpersec+" subtasks="+subTasks.size());
415 javacServer.flushLog();
416 unuse();
417 compilerPool.returnCompilerThread(this);
418 }
419 }
420 }