ohrstrom@1504: /* ohrstrom@1504: * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. ohrstrom@1504: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ohrstrom@1504: * ohrstrom@1504: * This code is free software; you can redistribute it and/or modify it ohrstrom@1504: * under the terms of the GNU General Public License version 2 only, as ohrstrom@1504: * published by the Free Software Foundation. Oracle designates this ohrstrom@1504: * particular file as subject to the "Classpath" exception as provided ohrstrom@1504: * by Oracle in the LICENSE file that accompanied this code. ohrstrom@1504: * ohrstrom@1504: * This code is distributed in the hope that it will be useful, but WITHOUT ohrstrom@1504: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ohrstrom@1504: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ohrstrom@1504: * version 2 for more details (a copy is included in the LICENSE file that ohrstrom@1504: * accompanied this code). ohrstrom@1504: * ohrstrom@1504: * You should have received a copy of the GNU General Public License version ohrstrom@1504: * 2 along with this work; if not, write to the Free Software Foundation, ohrstrom@1504: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ohrstrom@1504: * ohrstrom@1504: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohrstrom@1504: * or visit www.oracle.com if you need additional information or have any ohrstrom@1504: * questions. ohrstrom@1504: */ ohrstrom@1504: ohrstrom@1504: package com.sun.tools.sjavac.server; ohrstrom@1504: ohrstrom@1504: import java.io.BufferedReader; ohrstrom@1504: import java.io.File; ohrstrom@1504: import java.io.IOException; ohrstrom@1504: import java.io.InputStreamReader; ohrstrom@1504: import java.io.OutputStreamWriter; ohrstrom@1504: import java.io.PrintWriter; ohrstrom@1504: import java.io.StringWriter; ohrstrom@1504: import java.net.Socket; ohrstrom@1504: import java.net.URI; ohrstrom@1504: import java.net.URISyntaxException; ohrstrom@1504: import java.util.ArrayList; ohrstrom@1504: import java.util.Arrays; ohrstrom@1504: import java.util.HashSet; ohrstrom@1504: import java.util.List; ohrstrom@1504: import java.util.Set; ohrstrom@1504: import java.util.Map; ohrstrom@1504: import java.util.concurrent.Future; ohrstrom@1504: import javax.tools.JavaFileManager; ohrstrom@1504: import javax.tools.JavaFileObject; ohrstrom@1504: import javax.tools.StandardJavaFileManager; ohrstrom@1504: ohrstrom@1504: import com.sun.tools.javac.util.Context; ohrstrom@1504: import com.sun.tools.javac.util.Log; ohrstrom@1504: import com.sun.tools.javac.util.BaseFileManager; ohrstrom@1504: import com.sun.tools.sjavac.comp.Dependencies; ohrstrom@1504: import com.sun.tools.sjavac.comp.JavaCompilerWithDeps; ohrstrom@1504: import com.sun.tools.sjavac.comp.SmartFileManager; ohrstrom@1504: import com.sun.tools.sjavac.comp.ResolveWithDeps; ohrstrom@1504: ohrstrom@1504: /** ohrstrom@1504: * The compiler thread maintains a JavaCompiler instance and ohrstrom@1504: * can receive a request from the client, perform the compilation ohrstrom@1504: * requested and report back the results. ohrstrom@1504: * ohrstrom@1504: * *

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

ohrstrom@1504: */ ohrstrom@1504: public class CompilerThread implements Runnable { ohrstrom@1504: private JavacServer javacServer; ohrstrom@1504: private CompilerPool compilerPool; ohrstrom@1504: private List> subTasks; ohrstrom@1504: ohrstrom@1504: // Communicating over this socket. ohrstrom@1504: private Socket socket; ohrstrom@1504: ohrstrom@1504: // The necessary classes to do a compilation. ohrstrom@1504: private com.sun.tools.javac.api.JavacTool compiler; ohrstrom@1504: private StandardJavaFileManager fileManager; ohrstrom@1504: private BaseFileManager fileManagerBase; ohrstrom@1504: private SmartFileManager smartFileManager; ohrstrom@1504: private Context context; ohrstrom@1504: ohrstrom@1504: // If true, then this thread is serving a request. ohrstrom@1504: private boolean inUse = false; ohrstrom@1504: ohrstrom@1504: CompilerThread(CompilerPool cp) { ohrstrom@1504: compilerPool = cp; ohrstrom@1504: javacServer = cp.getJavacServer(); ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: /** ohrstrom@1504: * Execute a minor task, for example generating bytecodes and writing them to disk, ohrstrom@1504: * that belong to a major compiler thread task. ohrstrom@1504: */ ohrstrom@1504: public synchronized void executeSubtask(Runnable r) { ohrstrom@1504: subTasks.add(compilerPool.executeSubtask(this, r)); ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: /** ohrstrom@1504: * Count the number of active sub tasks. ohrstrom@1504: */ ohrstrom@1504: public synchronized int numActiveSubTasks() { ohrstrom@1504: int c = 0; ohrstrom@1504: for (Future f : subTasks) { ohrstrom@1504: if (!f.isDone() && !f.isCancelled()) { ohrstrom@1504: c++; ohrstrom@1504: } ohrstrom@1504: } ohrstrom@1504: return c; ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: /** ohrstrom@1504: * Use this socket for the upcoming request. ohrstrom@1504: */ ohrstrom@1504: public void setSocket(Socket s) { ohrstrom@1504: socket = s; ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: /** ohrstrom@1504: * Prepare the compiler thread for use. It is not yet started. ohrstrom@1504: * It will be started by the executor service. ohrstrom@1504: */ ohrstrom@1504: public synchronized void use() { ohrstrom@1504: assert(!inUse); ohrstrom@1504: inUse = true; ohrstrom@1504: compiler = com.sun.tools.javac.api.JavacTool.create(); ohrstrom@1504: fileManager = compiler.getStandardFileManager(null, null, null); ohrstrom@1504: fileManagerBase = (BaseFileManager)fileManager; ohrstrom@1504: smartFileManager = new SmartFileManager(fileManager); ohrstrom@1504: context = new Context(); ohrstrom@1504: context.put(JavaFileManager.class, smartFileManager); ohrstrom@1504: ResolveWithDeps.preRegister(context); ohrstrom@1504: JavaCompilerWithDeps.preRegister(context, this); ohrstrom@1504: subTasks = new ArrayList>(); ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: /** ohrstrom@1504: * Prepare the compiler thread for idleness. ohrstrom@1504: */ ohrstrom@1504: public synchronized void unuse() { ohrstrom@1504: assert(inUse); ohrstrom@1504: inUse = false; ohrstrom@1504: compiler = null; ohrstrom@1504: fileManager = null; ohrstrom@1504: fileManagerBase = null; ohrstrom@1504: smartFileManager = null; ohrstrom@1504: context = null; ohrstrom@1504: subTasks = null; ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: /** ohrstrom@1504: * Expect this key on the next line read from the reader. ohrstrom@1504: */ ohrstrom@1504: private static boolean expect(BufferedReader in, String key) throws IOException { ohrstrom@1504: String s = in.readLine(); ohrstrom@1504: if (s != null && s.equals(key)) { ohrstrom@1504: return true; ohrstrom@1504: } ohrstrom@1504: return false; ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: // The request identifier, for example GENERATE_NEWBYTECODE ohrstrom@1504: String id = ""; ohrstrom@1504: ohrstrom@1504: public String currentRequestId() { ohrstrom@1504: return id; ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: PrintWriter stdout; ohrstrom@1504: PrintWriter stderr; ohrstrom@1504: int forcedExitCode = 0; ohrstrom@1504: ohrstrom@1504: public void logError(String msg) { ohrstrom@1504: stderr.println(msg); ohrstrom@1504: forcedExitCode = -1; ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: /** ohrstrom@1504: * Invoked by the executor service. ohrstrom@1504: */ ohrstrom@1504: public void run() { ohrstrom@1504: // Unique nr that identifies this request. ohrstrom@1504: int thisRequest = compilerPool.startRequest(); ohrstrom@1504: long start = System.currentTimeMillis(); ohrstrom@1504: int numClasses = 0; ohrstrom@1504: StringBuilder compiledPkgs = new StringBuilder(); ohrstrom@1504: use(); ohrstrom@1504: ohrstrom@1504: PrintWriter out = null; ohrstrom@1504: try { ohrstrom@1504: javacServer.log("<"+thisRequest+"> Connect from "+socket.getRemoteSocketAddress()+" activethreads="+compilerPool.numActiveRequests()); ohrstrom@1504: BufferedReader in = new BufferedReader(new InputStreamReader( ohrstrom@1504: socket.getInputStream())); ohrstrom@1504: out = new PrintWriter(new OutputStreamWriter( ohrstrom@1504: socket.getOutputStream())); ohrstrom@1504: if (!expect(in, JavacServer.PROTOCOL_COOKIE_VERSION)) { ohrstrom@1504: javacServer.log("<"+thisRequest+"> Bad protocol from ip "+socket.getRemoteSocketAddress()); ohrstrom@1504: return; ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: String cookie = in.readLine(); ohrstrom@1504: if (cookie == null || !cookie.equals(""+javacServer.getCookie())) { ohrstrom@1504: javacServer.log("<"+thisRequest+"> Bad cookie from ip "+socket.getRemoteSocketAddress()); ohrstrom@1504: return; ohrstrom@1504: } ohrstrom@1504: if (!expect(in, JavacServer.PROTOCOL_CWD)) { ohrstrom@1504: return; ohrstrom@1504: } ohrstrom@1504: String cwd = in.readLine(); ohrstrom@1504: if (cwd == null) ohrstrom@1504: return; ohrstrom@1504: if (!expect(in, JavacServer.PROTOCOL_ID)) { ohrstrom@1504: return; ohrstrom@1504: } ohrstrom@1504: id = in.readLine(); ohrstrom@1504: if (id == null) ohrstrom@1504: return; ohrstrom@1504: if (!expect(in, JavacServer.PROTOCOL_ARGS)) { ohrstrom@1504: return; ohrstrom@1504: } ohrstrom@1504: ArrayList the_options = new ArrayList(); ohrstrom@1504: ArrayList the_classes = new ArrayList(); ohrstrom@1504: Iterable path = Arrays. asList(new File(cwd)); ohrstrom@1504: ohrstrom@1504: for (;;) { ohrstrom@1504: String l = in.readLine(); ohrstrom@1504: if (l == null) ohrstrom@1504: return; ohrstrom@1504: if (l.equals(JavacServer.PROTOCOL_SOURCES_TO_COMPILE)) ohrstrom@1504: break; ohrstrom@1504: if (l.startsWith("--server:")) ohrstrom@1504: continue; ohrstrom@1504: if (!l.startsWith("-") && l.endsWith(".java")) { ohrstrom@1504: the_classes.add(new File(l)); ohrstrom@1504: numClasses++; ohrstrom@1504: } else { ohrstrom@1504: the_options.add(l); ohrstrom@1504: } ohrstrom@1504: continue; ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: // Load sources to compile ohrstrom@1504: Set sourcesToCompile = new HashSet(); ohrstrom@1504: for (;;) { ohrstrom@1504: String l = in.readLine(); ohrstrom@1504: if (l == null) ohrstrom@1504: return; ohrstrom@1504: if (l.equals(JavacServer.PROTOCOL_VISIBLE_SOURCES)) ohrstrom@1504: break; ohrstrom@1504: try { ohrstrom@1504: sourcesToCompile.add(new URI(l)); ohrstrom@1504: numClasses++; ohrstrom@1504: } catch (URISyntaxException e) { ohrstrom@1504: return; ohrstrom@1504: } ohrstrom@1504: } ohrstrom@1504: // Load visible sources ohrstrom@1504: Set visibleSources = new HashSet(); ohrstrom@1504: boolean fix_drive_letter_case = System.getProperty("os.name").toLowerCase().equals("windows"); ohrstrom@1504: for (;;) { ohrstrom@1504: String l = in.readLine(); ohrstrom@1504: if (l == null) ohrstrom@1504: return; ohrstrom@1504: if (l.equals(JavacServer.PROTOCOL_END)) ohrstrom@1504: break; ohrstrom@1504: try { ohrstrom@1504: URI u = new URI(l); ohrstrom@1504: if (fix_drive_letter_case) { ohrstrom@1504: // Make sure the driver letter is lower case. ohrstrom@1504: String s = u.toString(); ohrstrom@1504: if (s.startsWith("file:/") && ohrstrom@1504: Character.isUpperCase(s.charAt(6))) { ohrstrom@1504: u = new URI("file:/"+Character.toLowerCase(s.charAt(6))+s.substring(7)); ohrstrom@1504: } ohrstrom@1504: } ohrstrom@1504: visibleSources.add(u); ohrstrom@1504: } catch (URISyntaxException e) { ohrstrom@1504: return; ohrstrom@1504: } ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: // A completed request has been received. ohrstrom@1504: ohrstrom@1504: // Now setup the actual compilation.... ohrstrom@1504: // First deal with explicit source files on cmdline and in at file. ohrstrom@1504: com.sun.tools.javac.util.ListBuffer compilationUnits = ohrstrom@1504: new com.sun.tools.javac.util.ListBuffer(); ohrstrom@1504: for (JavaFileObject i : fileManager.getJavaFileObjectsFromFiles(the_classes)) { ohrstrom@1504: compilationUnits.append(i); ohrstrom@1504: } ohrstrom@1504: // Now deal with sources supplied as source_to_compile. ohrstrom@1504: com.sun.tools.javac.util.ListBuffer sourcesToCompileFiles = ohrstrom@1504: new com.sun.tools.javac.util.ListBuffer(); ohrstrom@1504: for (URI u : sourcesToCompile) { ohrstrom@1504: sourcesToCompileFiles.append(new File(u)); ohrstrom@1504: } ohrstrom@1504: for (JavaFileObject i : fileManager.getJavaFileObjectsFromFiles(sourcesToCompileFiles)) { ohrstrom@1504: compilationUnits.append(i); ohrstrom@1504: } ohrstrom@1504: // Log the options to be used. ohrstrom@1504: StringBuilder options = new StringBuilder(); ohrstrom@1504: for (String s : the_options) { ohrstrom@1504: options.append(">").append(s).append("< "); ohrstrom@1504: } ohrstrom@1504: javacServer.log(id+" <"+thisRequest+"> options "+options.toString()); ohrstrom@1504: ohrstrom@1504: forcedExitCode = 0; ohrstrom@1504: // Create a new logger. ohrstrom@1504: StringWriter stdoutLog = new StringWriter(); ohrstrom@1504: StringWriter stderrLog = new StringWriter(); ohrstrom@1504: stdout = new PrintWriter(stdoutLog); ohrstrom@1504: stderr = new PrintWriter(stderrLog); ohrstrom@1504: com.sun.tools.javac.main.Main.Result rc = com.sun.tools.javac.main.Main.Result.OK; ohrstrom@1504: try { ohrstrom@1504: if (compilationUnits.size() > 0) { ohrstrom@1504: // Bind the new logger to the existing context. ohrstrom@1504: context.put(Log.outKey, stderr); ohrstrom@1504: Log.instance(context).setWriter(Log.WriterKind.NOTICE, stdout); ohrstrom@1504: Log.instance(context).setWriter(Log.WriterKind.WARNING, stderr); ohrstrom@1504: Log.instance(context).setWriter(Log.WriterKind.ERROR, stderr); ohrstrom@1504: // Process the options. ohrstrom@1504: com.sun.tools.javac.api.JavacTool.processOptions(context, smartFileManager, the_options); ohrstrom@1504: fileManagerBase.setContext(context); ohrstrom@1504: smartFileManager.setVisibleSources(visibleSources); ohrstrom@1504: smartFileManager.cleanArtifacts(); ohrstrom@1504: smartFileManager.setLog(stdout); ohrstrom@1504: Dependencies.instance(context).reset(); ohrstrom@1504: ohrstrom@1504: com.sun.tools.javac.main.Main ccompiler = new com.sun.tools.javac.main.Main("javacTask", stderr); ohrstrom@1504: String[] aa = the_options.toArray(new String[0]); ohrstrom@1504: ohrstrom@1504: // Do the compilation! ohrstrom@1504: rc = ccompiler.compile(aa, context, compilationUnits.toList(), null); ohrstrom@1504: ohrstrom@1504: while (numActiveSubTasks()>0) { ohrstrom@1504: try { Thread.sleep(1000); } catch (InterruptedException e) { } ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: smartFileManager.flush(); ohrstrom@1504: } ohrstrom@1504: } catch (Exception e) { ohrstrom@1504: stderr.println(e.getMessage()); ohrstrom@1504: forcedExitCode = -1; ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: // Send the response.. ohrstrom@1504: out.println(JavacServer.PROTOCOL_STDOUT); ohrstrom@1504: out.print(stdoutLog); ohrstrom@1504: out.println(JavacServer.PROTOCOL_STDERR); ohrstrom@1504: out.print(stderrLog); ohrstrom@1504: // The compilation is complete! And errors will have already been printed on out! ohrstrom@1504: out.println(JavacServer.PROTOCOL_PACKAGE_ARTIFACTS); ohrstrom@1504: Map> pa = smartFileManager.getPackageArtifacts(); ohrstrom@1504: for (String aPkgName : pa.keySet()) { ohrstrom@1504: out.println("+"+aPkgName); ohrstrom@1504: Set as = pa.get(aPkgName); ohrstrom@1504: for (URI a : as) { ohrstrom@1504: out.println(" "+a.toString()); ohrstrom@1504: } ohrstrom@1504: } ohrstrom@1504: Dependencies deps = Dependencies.instance(context); ohrstrom@1504: out.println(JavacServer.PROTOCOL_PACKAGE_DEPENDENCIES); ohrstrom@1504: Map> pd = deps.getDependencies(); ohrstrom@1504: for (String aPkgName : pd.keySet()) { ohrstrom@1504: out.println("+"+aPkgName); ohrstrom@1504: Set ds = pd.get(aPkgName); ohrstrom@1504: // Everything depends on java.lang ohrstrom@1504: if (!ds.contains(":java.lang")) ds.add(":java.lang"); ohrstrom@1504: for (String d : ds) { ohrstrom@1504: out.println(" "+d); ohrstrom@1504: } ohrstrom@1504: } ohrstrom@1504: out.println(JavacServer.PROTOCOL_PACKAGE_PUBLIC_APIS); ohrstrom@1504: Map pp = deps.getPubapis(); ohrstrom@1504: for (String aPkgName : pp.keySet()) { ohrstrom@1504: out.println("+"+aPkgName); ohrstrom@1504: String ps = pp.get(aPkgName); ohrstrom@1504: // getPubapis added a space to each line! ohrstrom@1504: out.println(ps); ohrstrom@1504: compiledPkgs.append(aPkgName+" "); ohrstrom@1504: } ohrstrom@1504: out.println(JavacServer.PROTOCOL_SYSINFO); ohrstrom@1504: out.println("num_cores=" + Runtime.getRuntime().availableProcessors()); ohrstrom@1504: out.println("max_memory=" + Runtime.getRuntime().maxMemory()); ohrstrom@1504: out.println(JavacServer.PROTOCOL_RETURN_CODE); ohrstrom@1504: ohrstrom@1504: // Errors from sjavac that affect compilation status! ohrstrom@1504: int rcv = rc.exitCode; ohrstrom@1504: if (rcv == 0 && forcedExitCode != 0) { ohrstrom@1504: rcv = forcedExitCode; ohrstrom@1504: } ohrstrom@1504: out.println("" + rcv); ohrstrom@1504: out.println(JavacServer.PROTOCOL_END); ohrstrom@1504: out.flush(); ohrstrom@1504: } catch (IOException e) { ohrstrom@1504: e.printStackTrace(); ohrstrom@1504: } finally { ohrstrom@1504: try { ohrstrom@1504: if (out != null) out.close(); ohrstrom@1504: if (!socket.isClosed()) { ohrstrom@1504: socket.close(); ohrstrom@1504: } ohrstrom@1504: socket = null; ohrstrom@1504: } catch (Exception e) { ohrstrom@1504: javacServer.log("ERROR "+e); ohrstrom@1504: e.printStackTrace(); ohrstrom@1504: } ohrstrom@1504: compilerPool.stopRequest(); ohrstrom@1504: long duration = System.currentTimeMillis()-start; ohrstrom@1504: javacServer.addBuildTime(duration); ohrstrom@1504: float classpersec = ((float)numClasses)*(((float)1000.0)/((float)duration)); ohrstrom@1504: javacServer.log(id+" <"+thisRequest+"> "+compiledPkgs+" duration " + duration+ " ms num_classes="+numClasses+ ohrstrom@1504: " classpersec="+classpersec+" subtasks="+subTasks.size()); ohrstrom@1504: javacServer.flushLog(); ohrstrom@1504: unuse(); ohrstrom@1504: compilerPool.returnCompilerThread(this); ohrstrom@1504: } ohrstrom@1504: } ohrstrom@1504: } ohrstrom@1504: