langtools/src/jdk.compiler/share/classes/com/sun/tools/sjavac/server/SjavacServer.java
author vasya
Mon, 14 Dec 2015 20:18:19 +0100
changeset 34752 9c262a013456
parent 32542 f4e4f4c4f9f4
child 34998 416dfba03d33
permissions -rw-r--r--
8145342: Some copyright notices are inconsistently and ill formatted Summary: Adjusted formatting of copyright notices. Reviewed-by: darcy, alundblad
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
 */
34752
9c262a013456 8145342: Some copyright notices are inconsistently and ill formatted
vasya
parents: 32542
diff changeset
    25
26098
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    26
package com.sun.tools.sjavac.server;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    27
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.HashMap;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    38
import java.util.Map;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    39
import java.util.Random;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    40
import java.util.concurrent.atomic.AtomicBoolean;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    41
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    42
import com.sun.tools.sjavac.Util;
32335
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 27383
diff changeset
    43
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
    44
import com.sun.tools.sjavac.comp.PooledSjavac;
26098
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
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
 * 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
    49
 *
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    50
 *  <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
    51
 *  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
    52
 *  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
    53
 *  deletion without notice.</b>
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    54
 */
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    55
public class SjavacServer implements Terminable {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    56
32542
f4e4f4c4f9f4 8129114: Sjavac should stream back compiler output to the client as soon as it becomes available
alundblad
parents: 32335
diff changeset
    57
    // Used in protocol to tell the content of each line
f4e4f4c4f9f4 8129114: Sjavac should stream back compiler output to the client as soon as it becomes available
alundblad
parents: 32335
diff changeset
    58
    public final static String LINE_TYPE_RC = "RC";
f4e4f4c4f9f4 8129114: Sjavac should stream back compiler output to the client as soon as it becomes available
alundblad
parents: 32335
diff changeset
    59
    public final static String LINE_TYPE_STDOUT = "STDOUT";
f4e4f4c4f9f4 8129114: Sjavac should stream back compiler output to the client as soon as it becomes available
alundblad
parents: 32335
diff changeset
    60
    public final static String LINE_TYPE_STDERR = "STDERR";
26098
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 {
27383
716ed9a6d607 8042088: Sjavac spawns external processes in a unnecessarily complex and platform dependent way
alundblad
parents: 26271
diff changeset
    98
        this(Util.extractStringOption("portfile", settings),
716ed9a6d607 8042088: Sjavac spawns external processes in a unnecessarily complex and platform dependent way
alundblad
parents: 26271
diff changeset
    99
             Util.extractStringOption("logfile", settings),
716ed9a6d607 8042088: Sjavac spawns external processes in a unnecessarily complex and platform dependent way
alundblad
parents: 26271
diff changeset
   100
             Util.extractStringOption("stdouterrfile", settings),
716ed9a6d607 8042088: Sjavac spawns external processes in a unnecessarily complex and platform dependent way
alundblad
parents: 26271
diff changeset
   101
             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
   102
             Util.extractIntOption("keepalive", settings, 120),
716ed9a6d607 8042088: Sjavac spawns external processes in a unnecessarily complex and platform dependent way
alundblad
parents: 26271
diff changeset
   103
             err);
716ed9a6d607 8042088: Sjavac spawns external processes in a unnecessarily complex and platform dependent way
alundblad
parents: 26271
diff changeset
   104
    }
716ed9a6d607 8042088: Sjavac spawns external processes in a unnecessarily complex and platform dependent way
alundblad
parents: 26271
diff changeset
   105
716ed9a6d607 8042088: Sjavac spawns external processes in a unnecessarily complex and platform dependent way
alundblad
parents: 26271
diff changeset
   106
    public SjavacServer(String portfilename,
716ed9a6d607 8042088: Sjavac spawns external processes in a unnecessarily complex and platform dependent way
alundblad
parents: 26271
diff changeset
   107
                        String logfile,
716ed9a6d607 8042088: Sjavac spawns external processes in a unnecessarily complex and platform dependent way
alundblad
parents: 26271
diff changeset
   108
                        String stdouterrfile,
716ed9a6d607 8042088: Sjavac spawns external processes in a unnecessarily complex and platform dependent way
alundblad
parents: 26271
diff changeset
   109
                        int poolsize,
716ed9a6d607 8042088: Sjavac spawns external processes in a unnecessarily complex and platform dependent way
alundblad
parents: 26271
diff changeset
   110
                        int keepalive,
716ed9a6d607 8042088: Sjavac spawns external processes in a unnecessarily complex and platform dependent way
alundblad
parents: 26271
diff changeset
   111
                        PrintStream err)
716ed9a6d607 8042088: Sjavac spawns external processes in a unnecessarily complex and platform dependent way
alundblad
parents: 26271
diff changeset
   112
                                throws FileNotFoundException {
716ed9a6d607 8042088: Sjavac spawns external processes in a unnecessarily complex and platform dependent way
alundblad
parents: 26271
diff changeset
   113
        this.portfilename = portfilename;
716ed9a6d607 8042088: Sjavac spawns external processes in a unnecessarily complex and platform dependent way
alundblad
parents: 26271
diff changeset
   114
        this.logfile = logfile;
716ed9a6d607 8042088: Sjavac spawns external processes in a unnecessarily complex and platform dependent way
alundblad
parents: 26271
diff changeset
   115
        this.stdouterrfile = stdouterrfile;
716ed9a6d607 8042088: Sjavac spawns external processes in a unnecessarily complex and platform dependent way
alundblad
parents: 26271
diff changeset
   116
        this.poolsize = poolsize;
716ed9a6d607 8042088: Sjavac spawns external processes in a unnecessarily complex and platform dependent way
alundblad
parents: 26271
diff changeset
   117
        this.keepalive = keepalive;
26098
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   118
        this.err = err;
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
        myCookie = new Random().nextLong();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   121
        theLog = new PrintWriter(logfile);
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
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   124
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   125
    /**
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   126
     * 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
   127
     */
32335
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 27383
diff changeset
   128
    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
   129
        if (allPortFiles == null) {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   130
            allPortFiles = new HashMap<>();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   131
        }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   132
        PortFile pf = allPortFiles.get(filename);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   133
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   134
        // Port file known. Does it still exist?
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   135
        if (pf != null) {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   136
            try {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   137
                if (!pf.exists())
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   138
                    pf = null;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   139
            } catch (IOException ioex) {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   140
                ioex.printStackTrace();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   141
            }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   142
        }
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
        if (pf == null) {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   145
            pf = new PortFile(filename);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   146
            allPortFiles.put(filename, pf);
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
        return pf;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   149
    }
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
     * Get the cookie used for this server.
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
    long getCookie() {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   155
        return myCookie;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   156
    }
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
     * Get the port used for this server.
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
    int getPort() {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   162
        return serverSocket.getLocalPort();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   163
    }
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
    /**
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   166
     * 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
   167
     */
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   168
    public void addBuildTime(long inc) {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   169
        totalBuildTime += inc;
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
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
     * Log this message.
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   174
     */
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   175
    public void log(String msg) {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   176
        if (theLog != null) {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   177
            theLog.println(msg);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   178
        } else {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   179
            System.err.println(msg);
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
    }
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
    /**
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   184
     * Make sure the log is flushed.
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
    public void flushLog() {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   187
        if (theLog != null) {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   188
            theLog.flush();
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
    }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   191
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   192
    /**
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   193
     * 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
   194
     * 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
   195
     */
27383
716ed9a6d607 8042088: Sjavac spawns external processes in a unnecessarily complex and platform dependent way
alundblad
parents: 26271
diff changeset
   196
    public int startServer() throws IOException, InterruptedException {
26098
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   197
        long serverStart = System.currentTimeMillis();
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
        // 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
   200
        portFile = getPortFile(portfilename);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   201
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   202
        synchronized (portFile) {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   203
            portFile.lock();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   204
            portFile.getValues();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   205
            if (portFile.containsPortInfo()) {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   206
                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
   207
                portFile.unlock();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   208
                return -1;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   209
            }
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
            //           .-----------.   .--------.   .------.
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   212
            // socket -->| IdleReset |-->| Pooled |-->| Impl |--> javac
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   213
            //           '-----------'   '--------'   '------'
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   214
            sjavac = new SjavacImpl();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   215
            sjavac = new PooledSjavac(sjavac, poolsize);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   216
            sjavac = new IdleResetSjavac(sjavac,
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   217
                                         this,
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   218
                                         keepalive * 1000);
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
            serverSocket = new ServerSocket();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   221
            InetAddress localhost = InetAddress.getByName(null);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   222
            serverSocket.bind(new InetSocketAddress(localhost, 0));
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   223
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   224
            // 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
   225
            // to publish the port / cookie information
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   226
            portFile.setValues(getPort(), getCookie());
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   227
            portFile.unlock();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   228
        }
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
        portFileMonitor = new PortFileMonitor(portFile, this);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   231
        portFileMonitor.start();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   232
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   233
        log("Sjavac server started. Accepting connections...");
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   234
        log("    port: " + getPort());
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   235
        log("    time: " + new java.util.Date());
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   236
        log("    poolsize: " + poolsize);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   237
        flushLog();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   238
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   239
        keepAcceptingRequests.set(true);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   240
        do {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   241
            try {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   242
                Socket socket = serverSocket.accept();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   243
                new Thread(new RequestHandler(socket, sjavac)).start();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   244
            } catch (SocketException se) {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   245
                // Caused by serverSocket.close() and indicates shutdown
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
        } while (keepAcceptingRequests.get());
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   248
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   249
        log("Shutting down.");
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   250
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   251
        // 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
   252
        // 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
   253
        // 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
   254
        // IOException on the client side.
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
        long realTime = System.currentTimeMillis() - serverStart;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   257
        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
   258
        flushLog();
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
        // Shut down
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   261
        sjavac.shutdown();
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
        return 0;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   264
    }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   265
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   266
    @Override
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   267
    public void shutdown(String quitMsg) {
26263
5ce1fe94b270 8055767: Sjavac is leaking servers
erikj
parents: 26107
diff changeset
   268
        if (!keepAcceptingRequests.compareAndSet(true, false)) {
26098
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   269
            // Already stopped, no need to shut down again
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   270
            return;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   271
        }
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
        log("Quitting: " + quitMsg);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   274
        flushLog();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   275
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   276
        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
   277
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   278
        // 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
   279
        // failed connection attempts
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   280
        try {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   281
            portFile.delete();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   282
        } catch (IOException e) {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   283
            e.printStackTrace(theLog);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   284
        }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   285
        try {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   286
            serverSocket.close();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   287
        } catch (IOException e) {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   288
            e.printStackTrace(theLog);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   289
        }
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
}