langtools/src/jdk.compiler/share/classes/com/sun/tools/sjavac/server/SjavacServer.java
author alundblad
Tue, 25 Aug 2015 15:14:41 +0200
changeset 32335 7df616378cf3
parent 27383 716ed9a6d607
child 32542 f4e4f4c4f9f4
permissions -rw-r--r--
8048318: Refactor sjavac as a thin client Summary: Moved compilation logic from client to server. Reviewed-by: jlahoda
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
26098
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
     1
/*
32335
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 27383
diff changeset
     2
 * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
26098
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
     4
 *
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    10
 *
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    15
 * accompanied this code).
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    16
 *
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    20
 *
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    23
 * questions.
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    24
 */
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    25
package com.sun.tools.sjavac.server;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    26
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    27
import java.io.FileNotFoundException;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    28
import java.io.IOException;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    29
import java.io.PrintStream;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    30
import java.io.PrintWriter;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    31
import java.net.InetAddress;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    32
import java.net.InetSocketAddress;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    33
import java.net.ServerSocket;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    34
import java.net.Socket;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    35
import java.net.SocketException;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    36
import java.util.HashMap;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    37
import java.util.Map;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    38
import java.util.Random;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    39
import java.util.concurrent.atomic.AtomicBoolean;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    40
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    41
import com.sun.tools.sjavac.Util;
32335
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 27383
diff changeset
    42
import com.sun.tools.sjavac.client.PortFileInaccessibleException;
27383
716ed9a6d607 8042088: Sjavac spawns external processes in a unnecessarily complex and platform dependent way
alundblad
parents: 26271
diff changeset
    43
import com.sun.tools.sjavac.comp.PooledSjavac;
26098
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    44
import com.sun.tools.sjavac.comp.SjavacImpl;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    45
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    46
/**
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    47
 * The JavacServer class contains methods both to setup a server that responds to requests and methods to connect to this server.
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    48
 *
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    49
 *  <p><b>This is NOT part of any supported API.
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    50
 *  If you write code that depends on this, you do so at your own risk.
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    51
 *  This code and its internal interfaces are subject to change or
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    52
 *  deletion without notice.</b>
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    53
 */
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    54
public class SjavacServer implements Terminable {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    55
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    56
    // Used in protocol to indicate which method to invoke
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    57
    public final static String CMD_COMPILE = "compile";
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    58
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    59
    final private String portfilename;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    60
    final private String logfile;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    61
    final private String stdouterrfile;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    62
    final private int poolsize;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    63
    final private int keepalive;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    64
    final private PrintStream err;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    65
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    66
    // The secret cookie shared between server and client through the port file.
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    67
    // Used to prevent clients from believing that they are communicating with
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    68
    // an old server when a new server has started and reused the same port as
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    69
    // an old server.
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    70
    private final long myCookie;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    71
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    72
    // Accumulated build time, not counting idle time, used for logging purposes
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    73
    private long totalBuildTime;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    74
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    75
    // The javac server specific log file.
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    76
    PrintWriter theLog;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    77
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    78
    // The sjavac implementation to delegate requests to
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    79
    Sjavac sjavac;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    80
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    81
    private ServerSocket serverSocket;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    82
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    83
    private PortFile portFile;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    84
    private PortFileMonitor portFileMonitor;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    85
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    86
    // Set to false break accept loop
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    87
    final AtomicBoolean keepAcceptingRequests = new AtomicBoolean();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    88
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    89
    // For the client, all port files fetched, one per started javac server.
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    90
    // Though usually only one javac server is started by a client.
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    91
    private static Map<String, PortFile> allPortFiles;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    92
    private static Map<String, Long> maxServerMemory;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    93
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    94
    public SjavacServer(String settings, PrintStream err) throws FileNotFoundException {
27383
716ed9a6d607 8042088: Sjavac spawns external processes in a unnecessarily complex and platform dependent way
alundblad
parents: 26271
diff changeset
    95
        this(Util.extractStringOption("portfile", settings),
716ed9a6d607 8042088: Sjavac spawns external processes in a unnecessarily complex and platform dependent way
alundblad
parents: 26271
diff changeset
    96
             Util.extractStringOption("logfile", settings),
716ed9a6d607 8042088: Sjavac spawns external processes in a unnecessarily complex and platform dependent way
alundblad
parents: 26271
diff changeset
    97
             Util.extractStringOption("stdouterrfile", settings),
716ed9a6d607 8042088: Sjavac spawns external processes in a unnecessarily complex and platform dependent way
alundblad
parents: 26271
diff changeset
    98
             Util.extractIntOption("poolsize", settings, Runtime.getRuntime().availableProcessors()),
716ed9a6d607 8042088: Sjavac spawns external processes in a unnecessarily complex and platform dependent way
alundblad
parents: 26271
diff changeset
    99
             Util.extractIntOption("keepalive", settings, 120),
716ed9a6d607 8042088: Sjavac spawns external processes in a unnecessarily complex and platform dependent way
alundblad
parents: 26271
diff changeset
   100
             err);
716ed9a6d607 8042088: Sjavac spawns external processes in a unnecessarily complex and platform dependent way
alundblad
parents: 26271
diff changeset
   101
    }
716ed9a6d607 8042088: Sjavac spawns external processes in a unnecessarily complex and platform dependent way
alundblad
parents: 26271
diff changeset
   102
716ed9a6d607 8042088: Sjavac spawns external processes in a unnecessarily complex and platform dependent way
alundblad
parents: 26271
diff changeset
   103
    public SjavacServer(String portfilename,
716ed9a6d607 8042088: Sjavac spawns external processes in a unnecessarily complex and platform dependent way
alundblad
parents: 26271
diff changeset
   104
                        String logfile,
716ed9a6d607 8042088: Sjavac spawns external processes in a unnecessarily complex and platform dependent way
alundblad
parents: 26271
diff changeset
   105
                        String stdouterrfile,
716ed9a6d607 8042088: Sjavac spawns external processes in a unnecessarily complex and platform dependent way
alundblad
parents: 26271
diff changeset
   106
                        int poolsize,
716ed9a6d607 8042088: Sjavac spawns external processes in a unnecessarily complex and platform dependent way
alundblad
parents: 26271
diff changeset
   107
                        int keepalive,
716ed9a6d607 8042088: Sjavac spawns external processes in a unnecessarily complex and platform dependent way
alundblad
parents: 26271
diff changeset
   108
                        PrintStream err)
716ed9a6d607 8042088: Sjavac spawns external processes in a unnecessarily complex and platform dependent way
alundblad
parents: 26271
diff changeset
   109
                                throws FileNotFoundException {
716ed9a6d607 8042088: Sjavac spawns external processes in a unnecessarily complex and platform dependent way
alundblad
parents: 26271
diff changeset
   110
        this.portfilename = portfilename;
716ed9a6d607 8042088: Sjavac spawns external processes in a unnecessarily complex and platform dependent way
alundblad
parents: 26271
diff changeset
   111
        this.logfile = logfile;
716ed9a6d607 8042088: Sjavac spawns external processes in a unnecessarily complex and platform dependent way
alundblad
parents: 26271
diff changeset
   112
        this.stdouterrfile = stdouterrfile;
716ed9a6d607 8042088: Sjavac spawns external processes in a unnecessarily complex and platform dependent way
alundblad
parents: 26271
diff changeset
   113
        this.poolsize = poolsize;
716ed9a6d607 8042088: Sjavac spawns external processes in a unnecessarily complex and platform dependent way
alundblad
parents: 26271
diff changeset
   114
        this.keepalive = keepalive;
26098
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   115
        this.err = err;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   116
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   117
        myCookie = new Random().nextLong();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   118
        theLog = new PrintWriter(logfile);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   119
    }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   120
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   121
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   122
    /**
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   123
     * Acquire the port file. Synchronized since several threads inside an smart javac wrapper client acquires the same port file at the same time.
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   124
     */
32335
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 27383
diff changeset
   125
    public static synchronized PortFile getPortFile(String filename) throws PortFileInaccessibleException {
26098
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   126
        if (allPortFiles == null) {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   127
            allPortFiles = new HashMap<>();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   128
        }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   129
        PortFile pf = allPortFiles.get(filename);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   130
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   131
        // Port file known. Does it still exist?
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   132
        if (pf != null) {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   133
            try {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   134
                if (!pf.exists())
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   135
                    pf = null;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   136
            } catch (IOException ioex) {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   137
                ioex.printStackTrace();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   138
            }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   139
        }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   140
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   141
        if (pf == null) {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   142
            pf = new PortFile(filename);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   143
            allPortFiles.put(filename, pf);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   144
        }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   145
        return pf;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   146
    }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   147
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   148
    /**
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   149
     * Get the cookie used for this server.
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   150
     */
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   151
    long getCookie() {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   152
        return myCookie;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   153
    }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   154
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   155
    /**
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   156
     * Get the port used for this server.
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   157
     */
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   158
    int getPort() {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   159
        return serverSocket.getLocalPort();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   160
    }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   161
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   162
    /**
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   163
     * Sum up the total build time for this javac server.
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   164
     */
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   165
    public void addBuildTime(long inc) {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   166
        totalBuildTime += inc;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   167
    }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   168
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   169
    /**
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   170
     * Log this message.
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   171
     */
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   172
    public void log(String msg) {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   173
        if (theLog != null) {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   174
            theLog.println(msg);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   175
        } else {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   176
            System.err.println(msg);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   177
        }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   178
    }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   179
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   180
    /**
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   181
     * Make sure the log is flushed.
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   182
     */
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   183
    public void flushLog() {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   184
        if (theLog != null) {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   185
            theLog.flush();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   186
        }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   187
    }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   188
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   189
    /**
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   190
     * Start a server using a settings string. Typically: "--startserver:portfile=/tmp/myserver,poolsize=3" and the string "portfile=/tmp/myserver,poolsize=3"
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   191
     * is sent as the settings parameter. Returns 0 on success, -1 on failure.
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   192
     */
27383
716ed9a6d607 8042088: Sjavac spawns external processes in a unnecessarily complex and platform dependent way
alundblad
parents: 26271
diff changeset
   193
    public int startServer() throws IOException, InterruptedException {
26098
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   194
        long serverStart = System.currentTimeMillis();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   195
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   196
        // The port file is locked and the server port and cookie is written into it.
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   197
        portFile = getPortFile(portfilename);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   198
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   199
        synchronized (portFile) {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   200
            portFile.lock();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   201
            portFile.getValues();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   202
            if (portFile.containsPortInfo()) {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   203
                err.println("Javac server not started because portfile exists!");
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   204
                portFile.unlock();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   205
                return -1;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   206
            }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   207
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   208
            //           .-----------.   .--------.   .------.
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   209
            // socket -->| IdleReset |-->| Pooled |-->| Impl |--> javac
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   210
            //           '-----------'   '--------'   '------'
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   211
            sjavac = new SjavacImpl();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   212
            sjavac = new PooledSjavac(sjavac, poolsize);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   213
            sjavac = new IdleResetSjavac(sjavac,
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   214
                                         this,
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   215
                                         keepalive * 1000);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   216
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   217
            serverSocket = new ServerSocket();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   218
            InetAddress localhost = InetAddress.getByName(null);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   219
            serverSocket.bind(new InetSocketAddress(localhost, 0));
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   220
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   221
            // At this point the server accepts connections, so it is  now safe
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   222
            // to publish the port / cookie information
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   223
            portFile.setValues(getPort(), getCookie());
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   224
            portFile.unlock();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   225
        }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   226
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   227
        portFileMonitor = new PortFileMonitor(portFile, this);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   228
        portFileMonitor.start();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   229
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   230
        log("Sjavac server started. Accepting connections...");
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   231
        log("    port: " + getPort());
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   232
        log("    time: " + new java.util.Date());
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   233
        log("    poolsize: " + poolsize);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   234
        flushLog();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   235
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   236
        keepAcceptingRequests.set(true);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   237
        do {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   238
            try {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   239
                Socket socket = serverSocket.accept();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   240
                new Thread(new RequestHandler(socket, sjavac)).start();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   241
            } catch (SocketException se) {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   242
                // Caused by serverSocket.close() and indicates shutdown
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   243
            }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   244
        } while (keepAcceptingRequests.get());
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   245
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   246
        log("Shutting down.");
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   247
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   248
        // No more connections accepted. If any client managed to connect after
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   249
        // the accept() was interrupted but before the server socket is closed
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   250
        // here, any attempt to read or write to the socket will result in an
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   251
        // IOException on the client side.
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   252
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   253
        long realTime = System.currentTimeMillis() - serverStart;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   254
        log("Total wall clock time " + realTime + "ms build time " + totalBuildTime + "ms");
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   255
        flushLog();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   256
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   257
        // Shut down
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   258
        sjavac.shutdown();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   259
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   260
        return 0;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   261
    }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   262
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   263
    @Override
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   264
    public void shutdown(String quitMsg) {
26263
5ce1fe94b270 8055767: Sjavac is leaking servers
erikj
parents: 26107
diff changeset
   265
        if (!keepAcceptingRequests.compareAndSet(true, false)) {
26098
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   266
            // Already stopped, no need to shut down again
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   267
            return;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   268
        }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   269
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   270
        log("Quitting: " + quitMsg);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   271
        flushLog();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   272
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   273
        portFileMonitor.shutdown(); // No longer any need to monitor port file
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   274
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   275
        // Unpublish port before shutting down socket to minimize the number of
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   276
        // failed connection attempts
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   277
        try {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   278
            portFile.delete();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   279
        } catch (IOException e) {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   280
            e.printStackTrace(theLog);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   281
        }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   282
        try {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   283
            serverSocket.close();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   284
        } catch (IOException e) {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   285
            e.printStackTrace(theLog);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   286
        }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   287
    }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   288
}