1 /*
   2  * Copyright (c) 2014, 2016, 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.sjavac.client;
  27 
  28 import java.io.BufferedReader;
  29 import java.io.IOException;
  30 import java.io.InputStreamReader;
  31 import java.io.OutputStreamWriter;
  32 import java.io.PrintWriter;
  33 import java.io.Reader;
  34 import java.net.InetAddress;
  35 import java.net.InetSocketAddress;
  36 import java.net.Socket;
  37 import java.util.ArrayList;
  38 import java.util.Arrays;
  39 import java.util.List;
  40 
  41 import com.sun.tools.javac.main.Main;
  42 import com.sun.tools.javac.main.Main.Result;
  43 import com.sun.tools.sjavac.Log;
  44 import com.sun.tools.sjavac.Util;
  45 import com.sun.tools.sjavac.options.OptionHelper;
  46 import com.sun.tools.sjavac.options.Options;
  47 import com.sun.tools.sjavac.server.CompilationSubResult;
  48 import com.sun.tools.sjavac.server.PortFile;
  49 import com.sun.tools.sjavac.server.Sjavac;
  50 import com.sun.tools.sjavac.server.SjavacServer;
  51 
  52 import static java.util.stream.Collectors.joining;
  53 
  54 /**
  55  * Sjavac implementation that delegates requests to a SjavacServer.
  56  *
  57  *  <p><b>This is NOT part of any supported API.
  58  *  If you write code that depends on this, you do so at your own risk.
  59  *  This code and its internal interfaces are subject to change or
  60  *  deletion without notice.</b>
  61  */
  62 public class SjavacClient implements Sjavac {
  63 
  64     // The id can perhaps be used in the future by the javac server to reuse the
  65     // JavaCompiler instance for several compiles using the same id.
  66     private final String id;
  67     private final PortFile portFile;
  68 
  69     // Default keepalive for server is 120 seconds.
  70     // I.e. it will accept 120 seconds of inactivity before quitting.
  71     private final int keepalive;
  72     private final int poolsize;
  73 
  74     // The sjavac option specifies how the server part of sjavac is spawned.
  75     // If you have the experimental sjavac in your path, you are done. If not, you have
  76     // to point to a com.sun.tools.sjavac.Main that supports --startserver
  77     // for example by setting: sjavac=java%20-jar%20...javac.jar%com.sun.tools.sjavac.Main
  78     private final String sjavacForkCmd;
  79 
  80     // Wait 2 seconds for response, before giving up on javac server.
  81     static int CONNECTION_TIMEOUT = 2000;
  82     static int MAX_CONNECT_ATTEMPTS = 3;
  83     static int WAIT_BETWEEN_CONNECT_ATTEMPTS = 2000;
  84 
  85     // Store the server conf settings here.
  86     private final String settings;
  87 
  88     public SjavacClient(Options options) {
  89         String tmpServerConf = options.getServerConf();
  90         String serverConf = (tmpServerConf!=null)? tmpServerConf : "";
  91         String tmpId = Util.extractStringOption("id", serverConf);
  92         id = (tmpId!=null) ? tmpId : "id"+(((new java.util.Random()).nextLong())&Long.MAX_VALUE);
  93         String defaultPortfile = options.getDestDir()
  94                                         .resolve("javac_server")
  95                                         .toAbsolutePath()
  96                                         .toString();
  97         String portfileName = Util.extractStringOption("portfile", serverConf, defaultPortfile);
  98         portFile = SjavacServer.getPortFile(portfileName);
  99         sjavacForkCmd = Util.extractStringOption("sjavac", serverConf, "sjavac");
 100         int poolsize = Util.extractIntOption("poolsize", serverConf);
 101         keepalive = Util.extractIntOption("keepalive", serverConf, 120);
 102 
 103         this.poolsize = poolsize > 0 ? poolsize : Runtime.getRuntime().availableProcessors();
 104         settings = (serverConf.equals("")) ? "id="+id+",portfile="+portfileName : serverConf;
 105     }
 106 
 107     /**
 108      * Hand out the server settings.
 109      * @return The server settings, possibly a default value.
 110      */
 111     public String serverSettings() {
 112         return settings;
 113     }
 114 
 115     @Override
 116     public Result compile(String[] args) {
 117         Result result = null;
 118         try (Socket socket = tryConnect()) {
 119             PrintWriter out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
 120             BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
 121 
 122             // Send args array to server
 123             out.println(args.length);
 124             for (String arg : args)
 125                 out.println(arg);
 126             out.flush();
 127 
 128             // Read server response line by line
 129             String line;
 130             while (null != (line = in.readLine())) {
 131                 if (!line.contains(":")) {
 132                     throw new AssertionError("Could not parse protocol line: >>\"" + line + "\"<<");
 133                 }
 134                 String[] typeAndContent = line.split(":", 2);
 135                 String type = typeAndContent[0];
 136                 String content = typeAndContent[1];
 137 
 138                 try {
 139                     if (Log.isDebugging()) {
 140                         // Distinguish server generated output if debugging.
 141                         content = "[sjavac-server] " + content;
 142                     }
 143                     Log.log(Log.Level.valueOf(type), content);
 144                     continue;
 145                 } catch (IllegalArgumentException e) {
 146                     // Parsing of 'type' as log level failed.
 147                 }
 148 
 149                 if (type.equals(SjavacServer.LINE_TYPE_RC)) {
 150                     result = Main.Result.valueOf(content);
 151                 }
 152             }
 153         } catch (PortFileInaccessibleException e) {
 154             Log.error("Port file inaccessible.");
 155             result = Result.ERROR;
 156         } catch (IOException ioe) {
 157             Log.error("IOException caught during compilation: " + ioe.getMessage());
 158             Log.debug(ioe);
 159             result = Result.ERROR;
 160         } catch (InterruptedException ie) {
 161             Thread.currentThread().interrupt(); // Restore interrupt
 162             Log.error("Compilation interrupted.");
 163             Log.debug(ie);
 164             result = Result.ERROR;
 165         }
 166 
 167         if (result == null) {
 168             // No LINE_TYPE_RC was found.
 169             result = Result.ERROR;
 170         }
 171 
 172         return result;
 173     }
 174 
 175     /*
 176      * Makes MAX_CONNECT_ATTEMPTS attepmts to connect to server.
 177      */
 178     private Socket tryConnect() throws IOException, InterruptedException {
 179         makeSureServerIsRunning(portFile);
 180         int attempt = 0;
 181         while (true) {
 182             Log.debug("Trying to connect. Attempt " + (++attempt) + " of " + MAX_CONNECT_ATTEMPTS);
 183             try {
 184                 return makeConnectionAttempt();
 185             } catch (IOException ex) {
 186                 Log.error("Connection attempt failed: " + ex.getMessage());
 187                 if (attempt >= MAX_CONNECT_ATTEMPTS) {
 188                     Log.error("Giving up");
 189                     throw new IOException("Could not connect to server", ex);
 190                 }
 191             }
 192             Thread.sleep(WAIT_BETWEEN_CONNECT_ATTEMPTS);
 193         }
 194     }
 195 
 196     private Socket makeConnectionAttempt() throws IOException {
 197         Socket socket = new Socket();
 198         InetAddress localhost = InetAddress.getByName(null);
 199         InetSocketAddress address = new InetSocketAddress(localhost, portFile.getPort());
 200         socket.connect(address, CONNECTION_TIMEOUT);
 201         Log.debug("Connected");
 202         return socket;
 203     }
 204 
 205     /*
 206      * Will return immediately if a server already seems to be running,
 207      * otherwise fork a new server and block until it seems to be running.
 208      */
 209     private void makeSureServerIsRunning(PortFile portFile)
 210             throws IOException, InterruptedException {
 211 
 212         if (portFile.exists()) {
 213             portFile.lock();
 214             portFile.getValues();
 215             portFile.unlock();
 216 
 217             if (portFile.containsPortInfo()) {
 218                 // Server seems to already be running
 219                 return;
 220             }
 221         }
 222 
 223         // Fork a new server and wait for it to start
 224         SjavacClient.fork(sjavacForkCmd,
 225                           portFile,
 226                           poolsize,
 227                           keepalive);
 228     }
 229 
 230     @Override
 231     public void shutdown() {
 232         // Nothing to clean up
 233     }
 234 
 235     /*
 236      * Fork a server process process and wait for server to come around
 237      */
 238     public static void fork(String sjavacCmd, PortFile portFile, int poolsize, int keepalive)
 239             throws IOException, InterruptedException {
 240         List<String> cmd = new ArrayList<>();
 241         cmd.addAll(Arrays.asList(OptionHelper.unescapeCmdArg(sjavacCmd).split(" ")));
 242         cmd.add("--startserver:"
 243               + "portfile=" + portFile.getFilename()
 244               + ",poolsize=" + poolsize
 245               + ",keepalive="+ keepalive);
 246 
 247         Process serverProcess;
 248         Log.debug("Starting server. Command: " + String.join(" ", cmd));
 249         try {
 250             // If the cmd for some reason can't be executed (file is not found,
 251             // or is not executable for instance) this will throw an
 252             // IOException and p == null.
 253             serverProcess = new ProcessBuilder(cmd)
 254                     .redirectErrorStream(true)
 255                     .start();
 256         } catch (IOException ex) {
 257             // Message is typically something like:
 258             // Cannot run program "xyz": error=2, No such file or directory
 259             Log.error("Failed to create server process: " + ex.getMessage());
 260             Log.debug(ex);
 261             throw new IOException(ex);
 262         }
 263 
 264         // serverProcess != null at this point.
 265         try {
 266             // Throws an IOException if no valid values materialize
 267             portFile.waitForValidValues();
 268         } catch (IOException ex) {
 269             // Process was started, but server failed to initialize. This could
 270             // for instance be due to the JVM not finding the server class,
 271             // or the server running in to some exception early on.
 272             Log.error("Sjavac server failed to initialize: " + ex.getMessage());
 273             Log.error("Process output:");
 274             Reader serverStdoutStderr = new InputStreamReader(serverProcess.getInputStream());
 275             try (BufferedReader br = new BufferedReader(serverStdoutStderr)) {
 276                 br.lines().forEach(Log::error);
 277             }
 278             Log.error("<End of process output>");
 279             try {
 280                 Log.error("Process exit code: " + serverProcess.exitValue());
 281             } catch (IllegalThreadStateException e) {
 282                 // Server is presumably still running.
 283             }
 284             throw new IOException("Server failed to initialize: " + ex.getMessage(), ex);
 285         }
 286     }
 287 }