src/jdk.jdi/share/classes/com/sun/tools/jdi/GenericListeningConnector.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 45714 jdk/src/jdk.jdi/share/classes/com/sun/tools/jdi/GenericListeningConnector.java@1820d351198d
child 51955 d38cb687d631
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
45714
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 25859
diff changeset
     2
 * Copyright (c) 2003, 2017, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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
45714
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 25859
diff changeset
    28
import java.io.IOException;
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 25859
diff changeset
    29
import java.util.ArrayList;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.HashMap;
45714
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 25859
diff changeset
    31
import java.util.Map;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import com.sun.jdi.Bootstrap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import com.sun.jdi.VirtualMachine;
45714
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 25859
diff changeset
    35
import com.sun.jdi.connect.Connector;
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 25859
diff changeset
    36
import com.sun.jdi.connect.IllegalConnectorArgumentsException;
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 25859
diff changeset
    37
import com.sun.jdi.connect.ListeningConnector;
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 25859
diff changeset
    38
import com.sun.jdi.connect.Transport;
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 25859
diff changeset
    39
import com.sun.jdi.connect.spi.Connection;
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 25859
diff changeset
    40
import com.sun.jdi.connect.spi.TransportService;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * A ListeningConnector to listen for connections from target VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * using the configured transport service
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
public class GenericListeningConnector
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        extends ConnectorImpl implements ListeningConnector
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    static final String ARG_ADDRESS = "address";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    static final String ARG_TIMEOUT = "timeout";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    Map<Map<String,? extends Connector.Argument>, TransportService.ListenKey>  listenMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    TransportService transportService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    Transport transport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * Initialize a new instance of this connector. The connector
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * encapsulates a transport service, has a "timeout" connector argument,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * and optionally an "address" connector argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private GenericListeningConnector(TransportService ts,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                                      boolean addAddressArgument)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        transportService = ts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        transport = new Transport() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                public String name() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                    return transportService.name();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        if (addAddressArgument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            addStringArgument(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                ARG_ADDRESS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                getString("generic_listening.address.label"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                getString("generic_listening.address"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                "",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        addIntegerArgument(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                ARG_TIMEOUT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                getString("generic_listening.timeout.label"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                getString("generic_listening.timeout"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                "",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                0, Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
45714
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 25859
diff changeset
    88
        listenMap = new HashMap<Map<String, ? extends Connector.Argument>, TransportService.ListenKey>(10);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * Initialize a new instance of this connector. This constructor is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * when sub-classing - the resulting connector will a "timeout" connector
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    protected GenericListeningConnector(TransportService ts) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        this(ts, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * Create an instance of this Connector. The resulting ListeningConnector will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * have "address" and "timeout" connector arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public static GenericListeningConnector create(TransportService ts) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        return new GenericListeningConnector(ts, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    public String startListening(String address, Map<String,? extends Connector.Argument> args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        throws IOException, IllegalConnectorArgumentsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        TransportService.ListenKey listener = listenMap.get(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (listener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
           throw new IllegalConnectorArgumentsException("Already listening",
45714
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 25859
diff changeset
   114
               new ArrayList<>(args.keySet()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        listener = transportService.startListening(address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        listenMap.put(args, listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        return listener.address();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public String
45714
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 25859
diff changeset
   123
        startListening(Map<String, ? extends Connector.Argument> args)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        throws IOException, IllegalConnectorArgumentsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        String address = argument(ARG_ADDRESS, args).value();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        return startListening(address, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
45714
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 25859
diff changeset
   130
    public void stopListening(Map<String, ? extends Connector.Argument> args)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        throws IOException, IllegalConnectorArgumentsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        TransportService.ListenKey listener = listenMap.get(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        if (listener == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
           throw new IllegalConnectorArgumentsException("Not listening",
45714
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 25859
diff changeset
   136
               new ArrayList<>(args.keySet()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        transportService.stopListening(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        listenMap.remove(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    public VirtualMachine
45714
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 25859
diff changeset
   143
        accept(Map<String, ? extends Connector.Argument> args)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        throws IOException, IllegalConnectorArgumentsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        String ts = argument(ARG_TIMEOUT, args).value();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        int timeout = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (ts.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            timeout = Integer.decode(ts).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        TransportService.ListenKey listener = listenMap.get(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        Connection connection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        if (listener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            connection = transportService.accept(listener, timeout, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
             * Keep compatibility with previous releases - if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
             * debugger hasn't called startListening then we do a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
             * once-off accept
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
             startListening(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
             listener = listenMap.get(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
             assert listener != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
             connection = transportService.accept(listener, timeout, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
             stopListening(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        return Bootstrap.virtualMachineManager().createVirtualMachine(connection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    public boolean supportsMultipleConnections() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        return transportService.capabilities().supportsMultipleConnections();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    public String name() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        return transport.name() + "Listen";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    public String description() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        return transportService.description();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    public Transport transport() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        return transport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
}