langtools/src/share/classes/com/sun/tools/sjavac/server/JavacServiceClient.java
author jjg
Mon, 14 Jul 2014 17:25:53 -0700
changeset 25605 c2d72342fe19
parent 25299 b4a7dcd657f5
child 26088 f479ca655ba1
permissions -rw-r--r--
8050408: A few new Java src files for sjavac are missing copyright notices Reviewed-by: mduigou
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
25605
c2d72342fe19 8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents: 25299
diff changeset
     1
/*
c2d72342fe19 8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents: 25299
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
c2d72342fe19 8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents: 25299
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
c2d72342fe19 8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents: 25299
diff changeset
     4
 *
c2d72342fe19 8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents: 25299
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
c2d72342fe19 8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents: 25299
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
c2d72342fe19 8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents: 25299
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
c2d72342fe19 8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents: 25299
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
c2d72342fe19 8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents: 25299
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
c2d72342fe19 8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents: 25299
diff changeset
    10
 *
c2d72342fe19 8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents: 25299
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
c2d72342fe19 8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents: 25299
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
c2d72342fe19 8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents: 25299
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
c2d72342fe19 8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents: 25299
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
c2d72342fe19 8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents: 25299
diff changeset
    15
 * accompanied this code).
c2d72342fe19 8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents: 25299
diff changeset
    16
 *
c2d72342fe19 8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents: 25299
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
c2d72342fe19 8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents: 25299
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
c2d72342fe19 8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents: 25299
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
c2d72342fe19 8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents: 25299
diff changeset
    20
 *
c2d72342fe19 8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents: 25299
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c2d72342fe19 8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents: 25299
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
c2d72342fe19 8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents: 25299
diff changeset
    23
 * questions.
c2d72342fe19 8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents: 25299
diff changeset
    24
 */
c2d72342fe19 8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents: 25299
diff changeset
    25
25299
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    26
package com.sun.tools.sjavac.server;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    27
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    28
import java.io.BufferedReader;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    29
import java.io.File;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    30
import java.io.FileReader;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    31
import java.io.IOException;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    32
import java.io.InputStreamReader;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    33
import java.io.PrintStream;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    34
import java.io.PrintWriter;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    35
import java.io.StringWriter;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    36
import java.net.InetAddress;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    37
import java.net.InetSocketAddress;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    38
import java.net.Socket;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    39
import java.net.SocketAddress;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    40
import java.net.URI;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    41
import java.util.Collections;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    42
import java.util.HashMap;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    43
import java.util.HashSet;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    44
import java.util.List;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    45
import java.util.Map;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    46
import java.util.Set;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    47
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    48
import com.sun.tools.sjavac.Util;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    49
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    50
import static com.sun.tools.sjavac.server.CompilationResult.ERROR_BUT_TRY_AGAIN;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    51
import static com.sun.tools.sjavac.server.CompilationResult.ERROR_FATAL;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    52
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    53
public class JavacServiceClient implements JavacService {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    54
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    55
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    56
    // The id can perhaps be used in the future by the javac server to reuse the
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    57
    // JavaCompiler instance for several compiles using the same id.
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    58
    private final String id;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    59
    private final String portfile;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    60
    private final String logfile;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    61
    private final String stdouterrfile;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    62
    private final boolean background;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    63
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    64
    // Default keepalive for server is 120 seconds.
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    65
    // I.e. it will accept 120 seconds of inactivity before quitting.
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    66
    private final int keepalive;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    67
    private final int poolsize;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    68
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    69
    // The sjavac option specifies how the server part of sjavac is spawned.
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    70
    // If you have the experimental sjavac in your path, you are done. If not, you have
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    71
    // to point to a com.sun.tools.sjavac.Main that supports --startserver
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    72
    // for example by setting: sjavac=java%20-jar%20...javac.jar%com.sun.tools.sjavac.Main
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    73
    private final String sjavac;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    74
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    75
    public JavacServiceClient(String settings) {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    76
        id = Util.extractStringOption("id", settings);
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    77
        portfile = Util.extractStringOption("portfile", settings);
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    78
        logfile = Util.extractStringOption("logfile", settings, portfile + ".javaclog");
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    79
        stdouterrfile = Util.extractStringOption("stdouterrfile", settings, portfile + ".stdouterr");
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    80
        background = Util.extractBooleanOption("background", settings, true);
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    81
        sjavac = Util.extractStringOption("sjavac", settings, "sjavac");
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    82
        int poolsize = Util.extractIntOption("poolsize", settings);
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    83
        keepalive = Util.extractIntOption("keepalive", settings, 120);
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    84
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    85
        this.poolsize = poolsize > 0 ? poolsize : Runtime.getRuntime().availableProcessors();
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    86
    }
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    87
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    88
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    89
    /**
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    90
     * Make a request to the server only to get the maximum possible heap size to use for compilations.
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    91
     *
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    92
     * @param port_file The port file used to synchronize creation of this server.
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    93
     * @param id The identify of the compilation.
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    94
     * @param out Standard out information.
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    95
     * @param err Standard err information.
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    96
     * @return The maximum heap size in bytes.
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    97
     */
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    98
    @Override
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    99
    public SysInfo getSysInfo() {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   100
        try {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   101
            CompilationResult cr = useServer(new String[0],
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   102
                                             Collections.<URI>emptySet(),
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   103
                                             Collections.<URI>emptySet(),
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   104
                                             Collections.<URI, Set<String>>emptyMap());
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   105
            return cr.sysinfo;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   106
        } catch (Exception e) {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   107
            return new SysInfo(-1, -1);
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   108
        }
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   109
    }
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   110
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   111
    @Override
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   112
    public CompilationResult compile(String protocolId,
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   113
                                     String invocationId,
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   114
                                     String[] args,
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   115
                                     List<File> explicitSources,
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   116
                                     Set<URI> sourcesToCompile,
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   117
                                     Set<URI> visibleSources) {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   118
        // Delegate to useServer, which delegates to compileHelper
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   119
        return useServer(args, sourcesToCompile, visibleSources, null);
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   120
    }
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   121
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   122
    /**
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   123
     * Connect and compile using the javac server settings and the args. When using more advanced features, the sources_to_compile and visible_sources are
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   124
     * supplied to the server and meta data is returned in package_artifacts, package_dependencies and package_pubapis.
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   125
     */
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   126
    public CompilationResult compileHelper(String id,
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   127
                                           String[] args,
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   128
                                           Set<URI> sourcesToCompile,
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   129
                                           Set<URI> visibleSources) {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   130
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   131
        CompilationResult rc = new CompilationResult(-3);
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   132
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   133
        try {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   134
            PortFile portFile = JavacServer.getPortFile(this.portfile);
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   135
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   136
            int port = portFile.containsPortInfo() ? portFile.getPort() : 0;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   137
            if (port == 0) {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   138
                return new CompilationResult(ERROR_BUT_TRY_AGAIN);
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   139
            }
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   140
            long cookie = portFile.getCookie();
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   141
            // Acquire the localhost/127.0.0.1 address.
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   142
            InetAddress addr = InetAddress.getByName(null);
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   143
            SocketAddress sockaddr = new InetSocketAddress(addr, port);
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   144
            Socket sock = new Socket();
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   145
            int timeoutMs = JavacServer.CONNECTION_TIMEOUT * 1000;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   146
            try {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   147
                sock.connect(sockaddr, timeoutMs);
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   148
            } catch (java.net.ConnectException e) {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   149
                rc.setReturnCode(ERROR_BUT_TRY_AGAIN);
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   150
                rc.stderr = "Could not connect to javac server found in portfile: " + portFile.getFilename() + " " + e;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   151
                return rc;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   152
            }
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   153
            if (!sock.isConnected()) {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   154
                rc.setReturnCode(ERROR_BUT_TRY_AGAIN);
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   155
                rc.stderr = "Could not connect to javac server found in portfile: " + portFile.getFilename();
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   156
                return rc;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   157
            }
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   158
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   159
            //
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   160
            // Send arguments
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   161
            //
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   162
            BufferedReader in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   163
            PrintWriter sockout = new PrintWriter(sock.getOutputStream());
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   164
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   165
            sockout.println(JavacServer.PROTOCOL_COOKIE_VERSION);
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   166
            sockout.println("" + cookie);
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   167
            sockout.println(JavacServer.PROTOCOL_CWD);
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   168
            sockout.println(System.getProperty("user.dir"));
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   169
            sockout.println(JavacServer.PROTOCOL_ID);
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   170
            sockout.println(id);
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   171
            sockout.println(JavacServer.PROTOCOL_ARGS);
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   172
            for (String s : args) {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   173
                StringBuffer buf = new StringBuffer();
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   174
                String[] paths = s.split(File.pathSeparator);
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   175
                int c = 0;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   176
                for (String path : paths) {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   177
                    File f = new File(path);
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   178
                    if (f.isFile() || f.isDirectory()) {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   179
                        buf.append(f.getAbsolutePath());
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   180
                        c++;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   181
                        if (c < paths.length) {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   182
                            buf.append(File.pathSeparator);
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   183
                        }
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   184
                    } else {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   185
                        buf = new StringBuffer(s);
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   186
                        break;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   187
                    }
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   188
                }
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   189
                sockout.println(buf.toString());
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   190
            }
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   191
            sockout.println(JavacServer.PROTOCOL_SOURCES_TO_COMPILE);
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   192
            for (URI uri : sourcesToCompile) {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   193
                sockout.println(uri.toString());
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   194
            }
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   195
            sockout.println(JavacServer.PROTOCOL_VISIBLE_SOURCES);
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   196
            for (URI uri : visibleSources) {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   197
                sockout.println(uri.toString());
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   198
            }
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   199
            sockout.println(JavacServer.PROTOCOL_END);
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   200
            sockout.flush();
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   201
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   202
            //
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   203
            // Receive result
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   204
            //
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   205
            StringBuffer stdout = new StringBuffer();
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   206
            StringBuffer stderr = new StringBuffer();
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   207
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   208
            if (!JavacServiceClient.expect(in, JavacServer.PROTOCOL_STDOUT)) {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   209
                return new CompilationResult(ERROR_FATAL);
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   210
            }
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   211
            // Load stdout
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   212
            for (;;) {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   213
                String l = in.readLine();
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   214
                if (l == null) {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   215
                    return new CompilationResult(ERROR_FATAL);
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   216
                }
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   217
                if (l.equals(JavacServer.PROTOCOL_STDERR)) {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   218
                    break;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   219
                }
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   220
                stdout.append(l);
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   221
                stdout.append('\n');
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   222
            }
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   223
            // Load stderr
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   224
            for (;;) {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   225
                String l = in.readLine();
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   226
                if (l == null) {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   227
                    return new CompilationResult(ERROR_FATAL);
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   228
                }
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   229
                if (l.equals(JavacServer.PROTOCOL_PACKAGE_ARTIFACTS)) {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   230
                    break;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   231
                }
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   232
                stderr.append(l);
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   233
                stderr.append('\n');
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   234
            }
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   235
            // Load the package artifacts
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   236
            Set<URI> lastUriSet = null;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   237
            for (;;) {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   238
                String l = in.readLine();
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   239
                if (l == null) {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   240
                    return new CompilationResult(ERROR_FATAL);
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   241
                }
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   242
                if (l.equals(JavacServer.PROTOCOL_PACKAGE_DEPENDENCIES)) {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   243
                    break;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   244
                }
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   245
                if (l.length() > 1 && l.charAt(0) == '+') {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   246
                    String pkg = l.substring(1);
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   247
                    lastUriSet = new HashSet<>();
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   248
                    rc.packageArtifacts.put(pkg, lastUriSet);
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   249
                } else if (l.length() > 1 && lastUriSet != null) {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   250
                    lastUriSet.add(new URI(l.substring(1)));
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   251
                }
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   252
            }
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   253
            // Load package dependencies
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   254
            Set<String> lastPackageSet = null;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   255
            for (;;) {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   256
                String l = in.readLine();
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   257
                if (l == null) {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   258
                    return new CompilationResult(ERROR_FATAL);
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   259
                }
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   260
                if (l.equals(JavacServer.PROTOCOL_PACKAGE_PUBLIC_APIS)) {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   261
                    break;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   262
                }
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   263
                if (l.length() > 1 && l.charAt(0) == '+') {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   264
                    String pkg = l.substring(1);
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   265
                    lastPackageSet = new HashSet<>();
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   266
                    rc.packageDependencies.put(pkg, lastPackageSet);
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   267
                } else if (l.length() > 1 && lastPackageSet != null) {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   268
                    lastPackageSet.add(l.substring(1));
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   269
                }
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   270
            }
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   271
            // Load package pubapis
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   272
            Map<String, StringBuffer> tmp = new HashMap<>();
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   273
            StringBuffer lastPublicApi = null;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   274
            for (;;) {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   275
                String l = in.readLine();
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   276
                if (l == null) {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   277
                    return new CompilationResult(ERROR_FATAL);
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   278
                }
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   279
                if (l.equals(JavacServer.PROTOCOL_SYSINFO)) {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   280
                    break;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   281
                }
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   282
                if (l.length() > 1 && l.charAt(0) == '+') {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   283
                    String pkg = l.substring(1);
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   284
                    lastPublicApi = new StringBuffer();
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   285
                    tmp.put(pkg, lastPublicApi);
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   286
                } else if (l.length() > 1 && lastPublicApi != null) {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   287
                    lastPublicApi.append(l.substring(1));
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   288
                    lastPublicApi.append("\n");
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   289
                }
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   290
            }
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   291
            for (String p : tmp.keySet()) {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   292
                //assert (packagePublicApis.get(p) == null);
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   293
                String api = tmp.get(p).toString();
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   294
                rc.packagePubapis.put(p, api);
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   295
            }
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   296
            // Now reading the max memory possible.
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   297
            for (;;) {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   298
                String l = in.readLine();
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   299
                if (l == null) {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   300
                    return new CompilationResult(ERROR_FATAL);
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   301
                }
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   302
                if (l.equals(JavacServer.PROTOCOL_RETURN_CODE)) {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   303
                    break;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   304
                }
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   305
                if (l.startsWith("num_cores=")) {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   306
                    rc.sysinfo.numCores = Integer.parseInt(l.substring(10));
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   307
                }
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   308
                if (l.startsWith("max_memory=")) {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   309
                    rc.sysinfo.maxMemory = Long.parseLong(l.substring(11));
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   310
                }
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   311
            }
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   312
            String l = in.readLine();
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   313
            if (l == null) {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   314
                rc.setReturnCode(ERROR_FATAL);
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   315
                rc.stderr = "No return value from the server!";
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   316
                return rc;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   317
            }
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   318
            rc.setReturnCode(Integer.parseInt(l));
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   319
            rc.stdout = stdout.toString();
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   320
            rc.stderr = stderr.toString();
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   321
        } catch (Exception e) {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   322
            StringWriter sw = new StringWriter();
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   323
            e.printStackTrace(new PrintWriter(sw));
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   324
            rc.stderr = sw.toString();
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   325
        }
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   326
        return rc;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   327
    }
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   328
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   329
    /**
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   330
     * Dispatch a compilation request to a javac server.
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   331
     *
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   332
     * @param args are the command line args to javac and is allowed to contain source files, @file and other command line options to javac.
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   333
     *
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   334
     * The generated classes, h files and other artifacts from the javac invocation are stored by the javac server to disk.
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   335
     *
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   336
     * @param sources_to_compile The sources to compile.
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   337
     *
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   338
     * @param visibleSources If visible sources has a non zero size, then visible_sources are the only files in the file system that the javac server can see!
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   339
     * (Sources to compile are always visible.) The visible sources are those supplied by the (filtered) -sourcepath
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   340
     *
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   341
     * @param visibleClasses If visible classes for a specific root/jar has a non zero size, then visible_classes are the only class files that the javac server
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   342
     * can see, in that root/jar. It maps from a classpath root or a jar file to the set of visible classes for that root/jar.
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   343
     *
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   344
     * The server return meta data about the build in the following parameters.
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   345
     * @param package_artifacts, map from package name to set of created artifacts for that package.
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   346
     * @param package_dependencies, map from package name to set of packages that it depends upon.
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   347
     * @param package_pubapis, map from package name to unique string identifying its pub api.
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   348
     */
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   349
    public CompilationResult useServer(String[] args,
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   350
                                       Set<URI> sourcesToCompile,
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   351
                                       Set<URI> visibleSources,
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   352
                                       Map<URI, Set<String>> visibleClasses) {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   353
        try {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   354
            if (portfile == null) {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   355
                CompilationResult cr = new CompilationResult(CompilationResult.ERROR_FATAL);
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   356
                cr.stderr = "No portfile was specified!";
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   357
                return cr;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   358
            }
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   359
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   360
            int attempts = 0;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   361
            CompilationResult rc;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   362
            do {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   363
                PortFile port_file = JavacServer.getPortFile(portfile);
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   364
                synchronized (port_file) {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   365
                    port_file.lock();
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   366
                    port_file.getValues();
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   367
                    port_file.unlock();
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   368
                }
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   369
                if (!port_file.containsPortInfo()) {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   370
                    String cmd = JavacServer.fork(sjavac, port_file.getFilename(), logfile, poolsize, keepalive, System.err, stdouterrfile, background);
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   371
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   372
                    if (background && !port_file.waitForValidValues()) {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   373
                        // Ouch the server did not start! Lets print its stdouterrfile and the command used.
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   374
                        StringWriter sw = new StringWriter();
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   375
                        JavacServiceClient.printFailedAttempt(cmd, stdouterrfile, new PrintWriter(sw));
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   376
                        // And give up.
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   377
                        CompilationResult cr = new CompilationResult(ERROR_FATAL);
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   378
                        cr.stderr = sw.toString();
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   379
                        return cr;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   380
                    }
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   381
                }
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   382
                rc = compileHelper(id, args, sourcesToCompile, visibleSources);
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   383
                // Try again until we manage to connect. Any error after that
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   384
                // will cause the compilation to fail.
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   385
                if (rc.returnCode == CompilationResult.ERROR_BUT_TRY_AGAIN) {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   386
                    // We could not connect to the server. Try again.
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   387
                    attempts++;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   388
                    try {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   389
                        Thread.sleep(JavacServer.WAIT_BETWEEN_CONNECT_ATTEMPTS * 1000);
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   390
                    } catch (InterruptedException e) {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   391
                    }
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   392
                }
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   393
            } while (rc.returnCode == ERROR_BUT_TRY_AGAIN && attempts < JavacServer.MAX_NUM_CONNECT_ATTEMPTS);
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   394
            return rc;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   395
        } catch (Exception e) {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   396
            StringWriter sw = new StringWriter();
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   397
            e.printStackTrace(new PrintWriter(sw));
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   398
            CompilationResult cr = new CompilationResult(ERROR_FATAL);
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   399
            cr.stderr = sw.toString();
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   400
            return cr;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   401
        }
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   402
    }
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   403
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   404
    public static void printFailedAttempt(String cmd, String f, PrintWriter err) {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   405
        err.println("---- Failed to start javac server with this command -----");
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   406
        err.println(cmd);
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   407
        try {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   408
            BufferedReader in = new BufferedReader(new FileReader(f));
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   409
            err.println("---- stdout/stderr output from attempt to start javac server -----");
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   410
            for (;;) {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   411
                String l = in.readLine();
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   412
                if (l == null) {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   413
                    break;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   414
                }
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   415
                err.println(l);
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   416
            }
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   417
            err.println("------------------------------------------------------------------");
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   418
        } catch (Exception e) {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   419
            err.println("The stdout/stderr output in file " + f + " does not exist and the server did not start.");
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   420
        }
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   421
    }
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   422
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   423
    /**
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   424
     * Expect this key on the next line read from the reader.
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   425
     */
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   426
    public static boolean expect(BufferedReader in, String key) throws IOException {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   427
        String s = in.readLine();
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   428
        if (s != null && s.equals(key)) {
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   429
            return true;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   430
        }
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   431
        return false;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   432
    }
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   433
}