1 /* 2 * Copyright (c) 2003, 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 java.io.IOException; 29 import java.util.Map; 30 31 import com.sun.jdi.Bootstrap; 32 import com.sun.jdi.VirtualMachine; 33 import com.sun.jdi.connect.*; 34 import com.sun.jdi.connect.spi.*; 35 36 /* 37 * An AttachingConnector to attach to a running VM using any 38 * TransportService. 39 */ 40 41 public class GenericAttachingConnector 42 extends ConnectorImpl implements AttachingConnector 43 { 44 /* 45 * The arguments that this connector supports 46 */ 47 static final String ARG_ADDRESS = "address"; 48 static final String ARG_TIMEOUT = "timeout"; 49 50 TransportService transportService; 51 Transport transport; 52 53 /* 54 * Initialize a new instance of this connector. The connector 55 * encapsulates a transport service and optionally has an 56 * "address" connector argument. 57 */ GenericAttachingConnector(TransportService ts, boolean addAddressArgument)58 private GenericAttachingConnector(TransportService ts, 59 boolean addAddressArgument) 60 { 61 transportService = ts; 62 transport = new Transport() { 63 public String name() { 64 // delegate name to the transport service 65 return transportService.name(); 66 } 67 }; 68 69 if (addAddressArgument) { 70 addStringArgument( 71 ARG_ADDRESS, 72 getString("generic_attaching.address.label"), 73 getString("generic_attaching.address"), 74 "", 75 true); 76 } 77 78 79 addIntegerArgument( 80 ARG_TIMEOUT, 81 getString("generic_attaching.timeout.label"), 82 getString("generic_attaching.timeout"), 83 "", 84 false, 85 0, Integer.MAX_VALUE); 86 } 87 88 /** 89 * Initializes a new instance of this connector. This constructor 90 * is used when sub-classing - the resulting connector will have 91 * a "timeout" connector argument. 92 */ GenericAttachingConnector(TransportService ts)93 protected GenericAttachingConnector(TransportService ts) { 94 this(ts, false); 95 } 96 97 /* 98 * Create an instance of this connector. The resulting AttachingConnector 99 * will have address and timeout connector arguments. 100 */ create(TransportService ts)101 public static GenericAttachingConnector create(TransportService ts) { 102 return new GenericAttachingConnector(ts, true); 103 } 104 105 /** 106 * Attach to a target VM using the specified address and Connector arguments. 107 */ attach(String address, Map<String, ? extends Connector.Argument> args)108 public VirtualMachine attach(String address, Map<String, ? extends Connector.Argument> args) 109 throws IOException, IllegalConnectorArgumentsException 110 { 111 String ts = argument(ARG_TIMEOUT, args).value(); 112 int timeout = 0; 113 if (ts.length() > 0) { 114 timeout = Integer.decode(ts).intValue(); 115 } 116 Connection connection = transportService.attach(address, timeout, 0); 117 return Bootstrap.virtualMachineManager().createVirtualMachine(connection); 118 } 119 120 /** 121 * Attach to a target VM using the specified arguments - the address 122 * of the target VM is specified by the <code>address</code> connector 123 * argument. 124 */ 125 public VirtualMachine attach(Map<String,? extends Connector.Argument> args)126 attach(Map<String,? extends Connector.Argument> args) 127 throws IOException, IllegalConnectorArgumentsException 128 { 129 String address = argument(ARG_ADDRESS, args).value(); 130 return attach(address, args); 131 } 132 name()133 public String name() { 134 return transport.name() + "Attach"; 135 } 136 description()137 public String description() { 138 return transportService.description(); 139 } 140 transport()141 public Transport transport() { 142 return transport; 143 } 144 145 } 146