jdk/src/share/classes/com/sun/tools/jdi/AbstractLauncher.java
author jbachorik
Tue, 29 Apr 2014 11:15:21 +0200
changeset 24125 b85eeaae56c7
parent 23010 6dadb192ad81
permissions -rw-r--r--
8031195: Support default and static interface methods in JDI, JDWP and JDB Reviewed-by: sla, sspitsyn, dcubed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 17484
diff changeset
     2
 * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.jdi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import com.sun.tools.jdi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import com.sun.jdi.connect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import com.sun.jdi.connect.spi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import com.sun.jdi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.io.InterruptedIOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
abstract class AbstractLauncher extends ConnectorImpl implements LaunchingConnector {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    abstract public VirtualMachine
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        launch(Map<String,? extends Connector.Argument> arguments)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
                                 throws IOException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
                                        IllegalConnectorArgumentsException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
                                        VMStartException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    abstract public String name();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    abstract public String description();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    ThreadGroup grp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    AbstractLauncher() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        grp = Thread.currentThread().getThreadGroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        ThreadGroup parent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        while ((parent = grp.getParent()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            grp = parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    String[] tokenizeCommand(String command, char quote) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        String quoteStr = String.valueOf(quote); // easier to deal with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
         * Tokenize the command, respecting the given quote character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        StringTokenizer tokenizer = new StringTokenizer(command,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                                                        quote + " \t\r\n\f",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                                                        true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        String quoted = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        String pending = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        List<String> tokenList = new ArrayList<String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        while (tokenizer.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            String token = tokenizer.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            if (quoted != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                if (token.equals(quoteStr)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                    tokenList.add(quoted);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                    quoted = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                    quoted += token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            } else if (pending != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                if (token.equals(quoteStr)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                    quoted = pending;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                } else if ((token.length() == 1) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                           Character.isWhitespace(token.charAt(0))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                    tokenList.add(pending);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                    throw new InternalException("Unexpected token: " + token);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                pending = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                if (token.equals(quoteStr)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                    quoted = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                } else if ((token.length() == 1) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                           Character.isWhitespace(token.charAt(0))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                    // continue
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                    pending = token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
         * Add final token.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        if (pending != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            tokenList.add(pending);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
         * An unclosed quote at the end of the command. Do an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
         * implicit end quote.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        if (quoted != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            tokenList.add(quoted);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        String[] tokenArray = new String[tokenList.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        for (int i = 0; i < tokenList.size(); i++) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   122
            tokenArray[i] = tokenList.get(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        return tokenArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    protected VirtualMachine launch(String[] commandArray, String address,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                                    TransportService.ListenKey listenKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                                    TransportService ts)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                                    throws IOException, VMStartException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        Helper helper = new Helper(commandArray, address, listenKey, ts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        helper.launchAndAccept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        VirtualMachineManager manager =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            Bootstrap.virtualMachineManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        return manager.createVirtualMachine(helper.connection(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                                            helper.process());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * This class simply provides a context for a single launch and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * accept. It provides instance fields that can be used by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * all threads involved. This stuff can't be in the Connector proper
17484
4ad440adc4b1 8014676: Java debugger may fail to run
dmeetry
parents: 5506
diff changeset
   145
     * because the connector is a singleton and is not specific to any
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * one launch.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    private class Helper {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        private final String address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        private TransportService.ListenKey listenKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        private TransportService ts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        private final String[] commandArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        private Process process = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        private Connection connection = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        private IOException acceptException = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        private boolean exited = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        Helper(String[] commandArray, String address, TransportService.ListenKey listenKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            TransportService ts) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            this.commandArray = commandArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            this.address = address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            this.listenKey = listenKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            this.ts = ts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        String commandString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            String str = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            for (int i = 0; i < commandArray.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                if (i > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                    str += " ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                str += commandArray[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            return str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        synchronized void launchAndAccept() throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                                IOException, VMStartException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            process = Runtime.getRuntime().exec(commandArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            Thread acceptingThread = acceptConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            Thread monitoringThread = monitorTarget();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                while ((connection == null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                       (acceptException == null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                       !exited) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                if (exited) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                    throw new VMStartException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                        "VM initialization failed for: " + commandString(), process);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                if (acceptException != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                    // Rethrow the exception in this thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                    throw acceptException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                throw new InterruptedIOException("Interrupted during accept");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                acceptingThread.interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                monitoringThread.interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        Process process() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            return process;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        Connection connection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            return connection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        synchronized void notifyOfExit() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            exited = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            notify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        synchronized void notifyOfConnection(Connection connection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            this.connection = connection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            notify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        synchronized void notifyOfAcceptException(IOException acceptException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            this.acceptException = acceptException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            notify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        Thread monitorTarget() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            Thread thread = new Thread(grp,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                                       "launched target monitor") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                        process.waitFor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                         * Notify waiting thread of VM error termination
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                        notifyOfExit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                    } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                        // Connection has been established, stop monitoring
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            thread.setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            thread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            return thread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        Thread acceptConnection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            Thread thread = new Thread(grp,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                                       "connection acceptor") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                        Connection connection = ts.accept(listenKey, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                         * Notify waiting thread of connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                        notifyOfConnection(connection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                    } catch (InterruptedIOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                        // VM terminated, stop accepting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                        // Report any other exception to waiting thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                        notifyOfAcceptException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            thread.setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            thread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            return thread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
}