langtools/src/jdk.compiler/share/classes/com/sun/tools/sjavac/server/SjavacServer.java
author erikj
Fri, 22 Aug 2014 12:25:01 +0200
changeset 26263 5ce1fe94b270
parent 26107 a4a156a33c94
child 26271 97603426a4ce
permissions -rw-r--r--
8055767: Sjavac is leaking servers Reviewed-by: alundblad, jfranck
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
/*
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
     2
 * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
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.File;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    28
import java.io.FileNotFoundException;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    29
import java.io.IOException;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    30
import java.io.PrintStream;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    31
import java.io.PrintWriter;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    32
import java.net.InetAddress;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    33
import java.net.InetSocketAddress;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    34
import java.net.ServerSocket;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    35
import java.net.Socket;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    36
import java.net.SocketException;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    37
import java.util.ArrayList;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    38
import java.util.HashMap;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    39
import java.util.Map;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    40
import java.util.Random;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    41
import java.util.concurrent.atomic.AtomicBoolean;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    42
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    43
import com.sun.tools.sjavac.ProblemException;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    44
import com.sun.tools.sjavac.Util;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    45
import com.sun.tools.sjavac.comp.SjavacImpl;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    46
import com.sun.tools.sjavac.comp.PooledSjavac;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    47
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
 * 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
    50
 *
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    51
 *  <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
    52
 *  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
    53
 *  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
    54
 *  deletion without notice.</b>
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
public class SjavacServer implements Terminable {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    57
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    58
    // 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
    59
    public final static String CMD_COMPILE = "compile";
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    60
    public final static String CMD_SYS_INFO = "sys-info";
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    61
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    62
    final private String portfilename;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    63
    final private String logfile;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    64
    final private String stdouterrfile;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    65
    final private int poolsize;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    66
    final private int keepalive;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    67
    final private PrintStream err;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    68
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    69
    // 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
    70
    // 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
    71
    // 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
    72
    // an old server.
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    73
    private final long myCookie;
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
    // 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
    76
    private long totalBuildTime;
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 javac server specific log file.
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    79
    PrintWriter theLog;
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
    // The sjavac implementation to delegate requests to
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    82
    Sjavac sjavac;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    83
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    84
    private ServerSocket serverSocket;
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
    private PortFile portFile;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    87
    private PortFileMonitor portFileMonitor;
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
    // Set to false break accept loop
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    90
    final AtomicBoolean keepAcceptingRequests = new AtomicBoolean();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    91
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    92
    // 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
    93
    // 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
    94
    private static Map<String, PortFile> allPortFiles;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    95
    private static Map<String, Long> maxServerMemory;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    96
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    97
    public SjavacServer(String settings, PrintStream err) throws FileNotFoundException {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    98
        // Extract options. TODO: Change to proper constructor args
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    99
        portfilename = Util.extractStringOption("portfile", settings);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   100
        logfile = Util.extractStringOption("logfile", settings);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   101
        stdouterrfile = Util.extractStringOption("stdouterrfile", settings);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   102
        keepalive = Util.extractIntOption("keepalive", settings, 120);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   103
        poolsize = Util.extractIntOption("poolsize", settings,
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   104
                                         Runtime.getRuntime().availableProcessors());
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   105
        this.err = err;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   106
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   107
        myCookie = new Random().nextLong();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   108
        theLog = new PrintWriter(logfile);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   109
    }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   110
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   111
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   112
    /**
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   113
     * 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
   114
     */
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   115
    public static synchronized PortFile getPortFile(String filename) throws FileNotFoundException {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   116
        if (allPortFiles == null) {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   117
            allPortFiles = new HashMap<>();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   118
        }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   119
        PortFile pf = allPortFiles.get(filename);
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
        // Port file known. Does it still exist?
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   122
        if (pf != null) {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   123
            try {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   124
                if (!pf.exists())
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   125
                    pf = null;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   126
            } catch (IOException ioex) {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   127
                ioex.printStackTrace();
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
        }
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
        if (pf == null) {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   132
            pf = new PortFile(filename);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   133
            allPortFiles.put(filename, pf);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   134
        }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   135
        return pf;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   136
    }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   137
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
     * Get the cookie used for this server.
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
    long getCookie() {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   142
        return myCookie;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   143
    }
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
    /**
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   146
     * Get the port used for this server.
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
    int getPort() {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   149
        return serverSocket.getLocalPort();
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
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   152
    /**
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   153
     * 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
   154
     */
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   155
    public void addBuildTime(long inc) {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   156
        totalBuildTime += inc;
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
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   159
    /**
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   160
     * Log this message.
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
    public void log(String msg) {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   163
        if (theLog != null) {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   164
            theLog.println(msg);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   165
        } else {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   166
            System.err.println(msg);
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
    /**
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   171
     * Make sure the log is flushed.
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   172
     */
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   173
    public void flushLog() {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   174
        if (theLog != null) {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   175
            theLog.flush();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   176
        }
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
     * 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
   181
     * 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
   182
     */
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   183
    public int startServer() throws IOException {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   184
        long serverStart = System.currentTimeMillis();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   185
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   186
        // 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
   187
        portFile = getPortFile(portfilename);
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
        synchronized (portFile) {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   190
            portFile.lock();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   191
            portFile.getValues();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   192
            if (portFile.containsPortInfo()) {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   193
                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
   194
                portFile.unlock();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   195
                return -1;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   196
            }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   197
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
            // socket -->| IdleReset |-->| Pooled |-->| Impl |--> javac
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   200
            //           '-----------'   '--------'   '------'
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   201
            sjavac = new SjavacImpl();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   202
            sjavac = new PooledSjavac(sjavac, poolsize);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   203
            sjavac = new IdleResetSjavac(sjavac,
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   204
                                         this,
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   205
                                         keepalive * 1000);
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
            serverSocket = new ServerSocket();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   208
            InetAddress localhost = InetAddress.getByName(null);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   209
            serverSocket.bind(new InetSocketAddress(localhost, 0));
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
            // 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
   212
            // to publish the port / cookie information
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   213
            portFile.setValues(getPort(), getCookie());
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   214
            portFile.unlock();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   215
        }
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
        portFileMonitor = new PortFileMonitor(portFile, this);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   218
        portFileMonitor.start();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   219
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   220
        log("Sjavac server started. Accepting connections...");
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   221
        log("    port: " + getPort());
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   222
        log("    time: " + new java.util.Date());
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   223
        log("    poolsize: " + poolsize);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   224
        flushLog();
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
        keepAcceptingRequests.set(true);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   227
        do {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   228
            try {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   229
                Socket socket = serverSocket.accept();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   230
                new Thread(new RequestHandler(socket, sjavac)).start();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   231
            } catch (SocketException se) {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   232
                // Caused by serverSocket.close() and indicates shutdown
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   233
            }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   234
        } while (keepAcceptingRequests.get());
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
        log("Shutting down.");
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   237
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   238
        // 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
   239
        // 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
   240
        // 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
   241
        // IOException on the client side.
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   242
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   243
        long realTime = System.currentTimeMillis() - serverStart;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   244
        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
   245
        flushLog();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   246
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   247
        // Shut down
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   248
        sjavac.shutdown();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   249
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   250
        return 0;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   251
    }
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
    /**
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   254
     * Fork a background process. Returns the command line used that can be printed if something failed.
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   255
     */
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   256
    public static String fork(String sjavac, String portfile, String logfile, int poolsize, int keepalive,
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   257
            final PrintStream err, String stdouterrfile, boolean background)
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   258
            throws IOException, ProblemException {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   259
        if (stdouterrfile != null && stdouterrfile.trim().equals("")) {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   260
            stdouterrfile = null;
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
        final String startserver = "--startserver:portfile=" + portfile + ",logfile=" + logfile + ",stdouterrfile=" + stdouterrfile + ",poolsize=" + poolsize + ",keepalive="+ keepalive;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   263
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   264
        if (background) {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   265
            sjavac += "%20" + startserver;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   266
            sjavac = sjavac.replaceAll("%20", " ");
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   267
            sjavac = sjavac.replaceAll("%2C", ",");
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   268
            // If the java/sh/cmd launcher fails the failure will be captured by stdouterr because of the redirection here.
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   269
            String[] cmd = {"/bin/sh", "-c", sjavac + " >> " + stdouterrfile + " 2>&1"};
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   270
            if (!(new File("/bin/sh")).canExecute()) {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   271
                ArrayList<String> wincmd = new ArrayList<>();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   272
                wincmd.add("cmd");
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   273
                wincmd.add("/c");
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   274
                wincmd.add("start");
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   275
                wincmd.add("cmd");
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   276
                wincmd.add("/c");
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   277
                wincmd.add(sjavac + " >> " + stdouterrfile + " 2>&1");
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   278
                cmd = wincmd.toArray(new String[wincmd.size()]);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   279
            }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   280
            Process pp = null;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   281
            try {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   282
                pp = Runtime.getRuntime().exec(cmd);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   283
            } catch (Exception e) {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   284
                e.printStackTrace(err);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   285
                e.printStackTrace(new PrintWriter(stdouterrfile));
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
            StringBuilder rs = new StringBuilder();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   288
            for (String s : cmd) {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   289
                rs.append(s + " ");
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   290
            }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   291
            return rs.toString();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   292
        }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   293
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   294
        // Do not spawn a background server, instead run it within the same JVM.
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   295
        Thread t = new Thread() {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   296
            @Override
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   297
            public void run() {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   298
                try {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   299
                    SjavacServer server = new SjavacServer(startserver, err);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   300
                    server.startServer();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   301
                } catch (Throwable t) {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   302
                    t.printStackTrace(err);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   303
                }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   304
            }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   305
        };
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   306
        t.setDaemon(true);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   307
        t.start();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   308
        return "";
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   309
    }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   310
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   311
    @Override
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   312
    public void shutdown(String quitMsg) {
26263
5ce1fe94b270 8055767: Sjavac is leaking servers
erikj
parents: 26107
diff changeset
   313
        if (!keepAcceptingRequests.compareAndSet(true, false)) {
26098
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   314
            // Already stopped, no need to shut down again
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   315
            return;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   316
        }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   317
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   318
        log("Quitting: " + quitMsg);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   319
        flushLog();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   320
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   321
        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
   322
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   323
        // 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
   324
        // failed connection attempts
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   325
        try {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   326
            portFile.delete();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   327
        } catch (IOException e) {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   328
            e.printStackTrace(theLog);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   329
        }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   330
        try {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   331
            serverSocket.close();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   332
        } catch (IOException e) {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   333
            e.printStackTrace(theLog);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   334
        }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   335
    }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   336
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   337
    public static void cleanup(String... args) {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   338
        String settings = Util.findServerSettings(args);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   339
        if (settings == null) return;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   340
        String portfile = Util.extractStringOption("portfile", settings);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   341
        String background = Util.extractStringOption("background", settings);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   342
        if (background != null && background.equals("false")) {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   343
            // If the server runs within this jvm, then delete the portfile,
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   344
            // since this jvm is about to exit soon.
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   345
            File f = new File(portfile);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   346
            f.delete();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   347
        }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   348
    }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   349
}