1 13 package org.eclipse.jdi.internal.connect; 14 15 16 import java.io.IOException ; 17 import java.io.InterruptedIOException ; 18 import java.net.ServerSocket ; 19 import java.util.HashMap ; 20 import java.util.Map ; 21 22 import org.eclipse.debug.core.DebugPlugin; 23 import org.eclipse.jdi.internal.VirtualMachineImpl; 24 import org.eclipse.jdi.internal.VirtualMachineManagerImpl; 25 26 import com.ibm.icu.text.MessageFormat; 27 import com.sun.jdi.VirtualMachine; 28 import com.sun.jdi.connect.Connector; 29 import com.sun.jdi.connect.IllegalConnectorArgumentsException; 30 import com.sun.jdi.connect.LaunchingConnector; 31 import com.sun.jdi.connect.VMStartException; 32 33 34 public class SocketLaunchingConnectorImpl extends ConnectorImpl implements LaunchingConnector { 35 36 private static final int ACCEPT_TIMEOUT = 10*1000; 37 38 39 private String fHome; 40 41 private String fOptions; 42 43 private String fMain; 44 45 private boolean fSuspend; 46 47 private String fLauncher; 48 49 52 public SocketLaunchingConnectorImpl(VirtualMachineManagerImpl virtualMachineManager) { 53 super(virtualMachineManager); 54 55 SocketTransportImpl transport = new SocketTransportImpl(); 57 setTransport(transport); 58 } 59 60 63 public Map defaultArguments() { 64 HashMap arguments = new HashMap (6); 65 66 StringArgumentImpl strArg = new StringArgumentImpl("home", ConnectMessages.SocketLaunchingConnectorImpl_Home_directory_of_the_SDK_or_runtime_environment_used_to_launch_the_application_1, ConnectMessages.SocketLaunchingConnectorImpl_Home_2, false); strArg.setValue(System.getProperty("java.home")); arguments.put(strArg.name(), strArg); 70 71 strArg = new StringArgumentImpl("options", ConnectMessages.SocketLaunchingConnectorImpl_Launched_VM_options_3, ConnectMessages.SocketLaunchingConnectorImpl_Options_4, false); arguments.put(strArg.name(), strArg); 74 75 strArg = new StringArgumentImpl("main", ConnectMessages.SocketLaunchingConnectorImpl_Main_class_and_arguments__or_if__jar_is_an_option__the_main_jar_file_and_arguments_5, ConnectMessages.SocketLaunchingConnectorImpl_Main_6, true); arguments.put(strArg.name(), strArg); 78 79 BooleanArgumentImpl boolArg = new BooleanArgumentImpl("suspend", ConnectMessages.SocketLaunchingConnectorImpl_All_threads_will_be_suspended_before_execution_of_main_7, ConnectMessages.SocketLaunchingConnectorImpl_Suspend_8, false); boolArg.setValue(true); 82 arguments.put(boolArg.name(), boolArg); 83 84 strArg = new StringArgumentImpl("quote", ConnectMessages.SocketLaunchingConnectorImpl_Character_used_to_combine_space_delimited_text_into_a_single_command_line_argument_9, ConnectMessages.SocketLaunchingConnectorImpl_Quote_10, true); strArg.setValue("\""); arguments.put(strArg.name(), strArg); 88 89 strArg = new StringArgumentImpl("vmexec", ConnectMessages.SocketLaunchingConnectorImpl_Name_of_the_Java_VM_launcher_11, ConnectMessages.SocketLaunchingConnectorImpl_Launcher_12, true); strArg.setValue("java"); arguments.put(strArg.name(), strArg); 93 94 return arguments; 95 } 96 97 100 public String name() { 101 return "com.sun.jdi.CommandLineLaunch"; } 103 104 107 public String description() { 108 return ConnectMessages.SocketLaunchingConnectorImpl_Launches_target_using_Sun_Java_VM_command_line_and_attaches_to_it_13; 109 } 110 111 114 private void getConnectionArguments(Map connectionArgs) throws IllegalConnectorArgumentsException { 115 String attribute = ""; try { 117 attribute = "home"; fHome = ((Connector.StringArgument)connectionArgs.get(attribute)).value(); 119 attribute = "options"; fOptions = ((Connector.StringArgument)connectionArgs.get(attribute)).value(); 121 attribute = "main"; fMain = ((Connector.StringArgument)connectionArgs.get(attribute)).value(); 123 attribute = "suspend"; fSuspend = ((Connector.BooleanArgument)connectionArgs.get(attribute)).booleanValue(); 125 attribute = "quote"; ((Connector.StringArgument)connectionArgs.get(attribute)).value(); 127 attribute = "vmexec"; fLauncher = ((Connector.StringArgument)connectionArgs.get(attribute)).value(); 129 } catch (ClassCastException e) { 130 throw new IllegalConnectorArgumentsException(ConnectMessages.SocketLaunchingConnectorImpl_Connection_argument_is_not_of_the_right_type_14, attribute); 131 } catch (NullPointerException e) { 132 throw new IllegalConnectorArgumentsException(ConnectMessages.SocketLaunchingConnectorImpl_Necessary_connection_argument_is_null_15, attribute); 133 } catch (NumberFormatException e) { 134 throw new IllegalConnectorArgumentsException(ConnectMessages.SocketLaunchingConnectorImpl_Connection_argument_is_not_a_number_16, attribute); 135 } 136 } 137 138 142 public VirtualMachine launch(Map connectionArgs) throws IOException , IllegalConnectorArgumentsException, VMStartException { 143 getConnectionArguments(connectionArgs); 144 145 SocketListeningConnectorImpl listenConnector = new SocketListeningConnectorImpl(virtualMachineManager()); 148 Map args = listenConnector.defaultArguments(); 149 ((Connector.IntegerArgument)args.get("timeout")).setValue(ACCEPT_TIMEOUT); String address = listenConnector.startListening(args); 151 152 String slash = System.getProperty("file.separator"); String execString = fHome + slash + "bin" + slash + fLauncher; 156 execString += " -Xdebug -Xnoagent -Djava.compiler=NONE"; execString += " -Xrunjdwp:transport=dt_socket,address=" + address + ",server=n,suspend=" + (fSuspend ? "y" : "n"); 160 if (fOptions != null) 162 execString += " " + fOptions; 164 execString += " " + fMain; 167 String [] cmdLine = DebugPlugin.parseArguments(execString); 169 Process proc = Runtime.getRuntime().exec(cmdLine); 170 171 VirtualMachineImpl virtualMachine; 173 try { 174 virtualMachine = (VirtualMachineImpl)listenConnector.accept(args); 175 } catch (InterruptedIOException e) { 176 proc.destroy(); 177 String message= MessageFormat.format(ConnectMessages.SocketLaunchingConnectorImpl_VM_did_not_connect_within_given_time___0__ms_1, new String []{((Connector.IntegerArgument)args.get("timeout")).value()}); throw new VMStartException(message, proc); 179 } 180 181 virtualMachine.setLaunchedProcess(proc); 182 return virtualMachine; 183 } 184 185 191 public static int findFreePort() { 192 ServerSocket socket= null; 193 try { 194 socket= new ServerSocket (0); 195 return socket.getLocalPort(); 196 } catch (IOException e) { 197 } finally { 198 if (socket != null) { 199 try { 200 socket.close(); 201 } catch (IOException e) { 202 } 203 } 204 } 205 return -1; 206 } 207 } 208 | Popular Tags |