1 /*
   2  * Copyright (c) 1999, 2011, 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 
  26 package com.sun.tools.jdi;
  27 
  28 import com.sun.tools.jdi.*;
  29 import com.sun.jdi.connect.*;
  30 import com.sun.jdi.connect.spi.*;
  31 import com.sun.jdi.VirtualMachine;
  32 import java.util.Map;
  33 import java.io.IOException;
  34 
  35 public class RawCommandLineLauncher extends AbstractLauncher implements LaunchingConnector {
  36 
  37     static private final String ARG_COMMAND = "command";
  38     static private final String ARG_ADDRESS = "address";
  39     static private final String ARG_QUOTE   = "quote";
  40 
  41     TransportService transportService;
  42     Transport transport;
  43 
  44     public TransportService transportService() {
  45         return transportService;
  46     }
  47 
  48     public Transport transport() {
  49         return transport;
  50     }
  51 
  52     public RawCommandLineLauncher() {
  53         super();
  54 
  55         try {
  56             @SuppressWarnings("deprecation")
  57             Object o =
  58                 Class.forName("com.sun.tools.jdi.SharedMemoryTransportService").newInstance();
  59             transportService = (TransportService)o;
  60             transport = new Transport() {
  61                 public String name() {
  62                     return "dt_shmem";
  63                 }
  64             };
  65         } catch (ClassNotFoundException |
  66                  UnsatisfiedLinkError |
  67                  InstantiationException |
  68                  IllegalAccessException x) {
  69         };
  70 
  71         if (transportService == null) {
  72             transportService = new SocketTransportService();
  73             transport = new Transport() {
  74                 public String name() {
  75                     return "dt_socket";
  76                 }
  77             };
  78         }
  79 
  80         addStringArgument(
  81                 ARG_COMMAND,
  82                 getString("raw.command.label"),
  83                 getString("raw.command"),
  84                 "",
  85                 true);
  86         addStringArgument(
  87                 ARG_QUOTE,
  88                 getString("raw.quote.label"),
  89                 getString("raw.quote"),
  90                 "\"",
  91                 true);
  92 
  93         addStringArgument(
  94                 ARG_ADDRESS,
  95                 getString("raw.address.label"),
  96                 getString("raw.address"),
  97                 "",
  98                 true);
  99     }
 100 
 101 
 102     public VirtualMachine
 103         launch(Map<String,? extends Connector.Argument> arguments)
 104         throws IOException, IllegalConnectorArgumentsException,
 105                VMStartException
 106     {
 107         String command = argument(ARG_COMMAND, arguments).value();
 108         String address = argument(ARG_ADDRESS, arguments).value();
 109 
 110         String quote = argument(ARG_QUOTE, arguments).value();
 111 
 112         if (quote.length() > 1) {
 113             throw new IllegalConnectorArgumentsException("Invalid length",
 114                                                          ARG_QUOTE);
 115         }
 116 
 117         TransportService.ListenKey listener = transportService.startListening(address);
 118 
 119         try {
 120             return launch(tokenizeCommand(command, quote.charAt(0)),
 121                           address, listener, transportService);
 122         } finally {
 123             transportService.stopListening(listener);
 124         }
 125     }
 126 
 127     public String name() {
 128         return "com.sun.jdi.RawCommandLineLaunch";
 129     }
 130 
 131     public String description() {
 132         return getString("raw.description");
 133     }
 134 }