1/*
2 * Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.tools.jdi;
27
28import java.io.IOException;
29import java.lang.reflect.InvocationTargetException;
30import java.util.Map;
31
32import com.sun.jdi.VirtualMachine;
33import com.sun.jdi.connect.Connector;
34import com.sun.jdi.connect.IllegalConnectorArgumentsException;
35import com.sun.jdi.connect.Transport;
36import com.sun.jdi.connect.VMStartException;
37import com.sun.jdi.connect.spi.TransportService;
38
39public class RawCommandLineLauncher extends AbstractLauncher {
40
41    static private final String ARG_COMMAND = "command";
42    static private final String ARG_ADDRESS = "address";
43    static private final String ARG_QUOTE   = "quote";
44
45    TransportService transportService;
46    Transport transport;
47
48    public TransportService transportService() {
49        return transportService;
50    }
51
52    public Transport transport() {
53        return transport;
54    }
55
56    public RawCommandLineLauncher() {
57        super();
58
59        try {
60            transportService = (TransportService)Class.
61                forName("com.sun.tools.jdi.SharedMemoryTransportService").
62                getDeclaredConstructor().newInstance();
63            transport = new Transport() {
64                public String name() {
65                    return "dt_shmem";
66                }
67            };
68        } catch (ClassNotFoundException |
69                 UnsatisfiedLinkError |
70                 InstantiationException |
71                 InvocationTargetException |
72                 IllegalAccessException |
73                 NoSuchMethodException x) {
74        };
75
76        if (transportService == null) {
77            transportService = new SocketTransportService();
78            transport = new Transport() {
79                public String name() {
80                    return "dt_socket";
81                }
82            };
83        }
84
85        addStringArgument(
86                ARG_COMMAND,
87                getString("raw.command.label"),
88                getString("raw.command"),
89                "",
90                true);
91        addStringArgument(
92                ARG_QUOTE,
93                getString("raw.quote.label"),
94                getString("raw.quote"),
95                "\"",
96                true);
97
98        addStringArgument(
99                ARG_ADDRESS,
100                getString("raw.address.label"),
101                getString("raw.address"),
102                "",
103                true);
104    }
105
106
107    public VirtualMachine
108        launch(Map<String, ? extends Connector.Argument> arguments)
109        throws IOException, IllegalConnectorArgumentsException,
110               VMStartException
111    {
112        String command = argument(ARG_COMMAND, arguments).value();
113        String address = argument(ARG_ADDRESS, arguments).value();
114        String quote = argument(ARG_QUOTE, arguments).value();
115
116        if (quote.length() > 1) {
117            throw new IllegalConnectorArgumentsException("Invalid length",
118                                                         ARG_QUOTE);
119        }
120
121        TransportService.ListenKey listener = transportService.startListening(address);
122
123        try {
124            return launch(tokenizeCommand(command, quote.charAt(0)),
125                          address, listener, transportService);
126        } finally {
127            transportService.stopListening(listener);
128        }
129    }
130
131    public String name() {
132        return "com.sun.jdi.RawCommandLineLaunch";
133    }
134
135    public String description() {
136        return getString("raw.description");
137    }
138}
139