KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdi > internal > connect > SocketRawLaunchingConnectorImpl


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdi.internal.connect;
12
13
14 import java.io.IOException JavaDoc;
15 import java.io.InterruptedIOException JavaDoc;
16 import com.ibm.icu.text.MessageFormat;
17 import java.util.HashMap JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import org.eclipse.jdi.internal.VirtualMachineImpl;
21 import org.eclipse.jdi.internal.VirtualMachineManagerImpl;
22
23 import com.sun.jdi.VirtualMachine;
24 import com.sun.jdi.connect.Connector;
25 import com.sun.jdi.connect.IllegalConnectorArgumentsException;
26 import com.sun.jdi.connect.LaunchingConnector;
27 import com.sun.jdi.connect.VMStartException;
28
29
30 public class SocketRawLaunchingConnectorImpl extends ConnectorImpl implements LaunchingConnector {
31     /** Time that a launched VM is given to connect to us. */
32     private static final int ACCEPT_TIMEOUT = 10000;
33
34     /** Raw command to start the debugged application VM. */
35     private String JavaDoc fCommand;
36     /** Address from which to listen for a connection after the raw command is run. */
37     private String JavaDoc fAddress;
38     
39     /**
40      * Creates new SocketAttachingConnectorImpl.
41      */

42     public SocketRawLaunchingConnectorImpl(VirtualMachineManagerImpl virtualMachineManager) {
43         super(virtualMachineManager);
44         
45         // Create communication protocol specific transport.
46
SocketTransportImpl transport = new SocketTransportImpl();
47         setTransport(transport);
48     }
49     
50     /**
51      * @return Returns the default arguments.
52      */

53     public Map JavaDoc defaultArguments() {
54         HashMap JavaDoc arguments = new HashMap JavaDoc(3);
55         
56         // Command
57
StringArgumentImpl strArg = new StringArgumentImpl("command", ConnectMessages.SocketRawLaunchingConnectorImpl_Raw_command_to_start_the_debugged_application_VM_1, ConnectMessages.SocketRawLaunchingConnectorImpl_Command_2, true); //$NON-NLS-1$
58
arguments.put(strArg.name(), strArg);
59         
60         // Address
61
strArg = new StringArgumentImpl("address", ConnectMessages.SocketRawLaunchingConnectorImpl_Address_from_which_to_listen_for_a_connection_after_the_raw_command_is_run_3, ConnectMessages.SocketRawLaunchingConnectorImpl_Address_4, true); //$NON-NLS-1$
62
arguments.put(strArg.name(), strArg);
63         
64         // Quote
65
strArg = new StringArgumentImpl("quote", ConnectMessages.SocketRawLaunchingConnectorImpl_Character_used_to_combine_space_delimited_text_into_a_single_command_line_argument_5, ConnectMessages.SocketRawLaunchingConnectorImpl_Quote_6, true); //$NON-NLS-1$
66
strArg.setValue("\""); //$NON-NLS-1$
67
arguments.put(strArg.name(), strArg);
68
69         return arguments;
70     }
71     
72     /**
73      * @return Returns a short identifier for the connector.
74      */

75     public String JavaDoc name() {
76         return "com.sun.jdi.RawCommandLineLaunch"; //$NON-NLS-1$
77
}
78     
79     /**
80      * @return Returns a human-readable description of this connector and its purpose.
81      */

82     public String JavaDoc description() {
83         return ConnectMessages.SocketRawLaunchingConnectorImpl_Launches_target_using_user_specified_command_line_and_attaches_to_it_7;
84     }
85     
86     /**
87      * Retrieves connection arguments.
88      */

89     private void getConnectionArguments(Map JavaDoc connectionArgs) throws IllegalConnectorArgumentsException {
90         String JavaDoc attribute = ""; //$NON-NLS-1$
91
try {
92             attribute = "command"; //$NON-NLS-1$
93
fCommand = ((Connector.StringArgument)connectionArgs.get(attribute)).value();
94             attribute = "address"; //$NON-NLS-1$
95
fAddress = ((Connector.StringArgument)connectionArgs.get(attribute)).value();
96             attribute = "quote"; //$NON-NLS-1$
97
((Connector.StringArgument)connectionArgs.get(attribute)).value();
98         } catch (ClassCastException JavaDoc e) {
99             throw new IllegalConnectorArgumentsException(ConnectMessages.SocketRawLaunchingConnectorImpl_Connection_argument_is_not_of_the_right_type_8, attribute);
100         } catch (NullPointerException JavaDoc e) {
101             throw new IllegalConnectorArgumentsException(ConnectMessages.SocketRawLaunchingConnectorImpl_Necessary_connection_argument_is_null_9, attribute);
102         } catch (NumberFormatException JavaDoc e) {
103             throw new IllegalConnectorArgumentsException(ConnectMessages.SocketRawLaunchingConnectorImpl_Connection_argument_is_not_a_number_10, attribute);
104         }
105     }
106
107     /**
108      * Launches an application and connects to its VM.
109      * @return Returns a connected Virtual Machine.
110      */

111     public VirtualMachine launch(Map JavaDoc connectionArgs) throws IOException JavaDoc, IllegalConnectorArgumentsException, VMStartException {
112         getConnectionArguments(connectionArgs);
113         
114         // A listening connector is used that waits for a connection of the VM that is started up.
115
// Note that port number zero means that a free port is chosen.
116
SocketListeningConnectorImpl listenConnector = new SocketListeningConnectorImpl(virtualMachineManager());
117         Map JavaDoc args = listenConnector.defaultArguments();
118         ((Connector.IntegerArgument)args.get("port")).setValue(fAddress); //$NON-NLS-1$
119
((Connector.IntegerArgument)args.get("timeout")).setValue(ACCEPT_TIMEOUT); //$NON-NLS-1$
120
listenConnector.startListening(args);
121         
122         // Start VM.
123
Process JavaDoc proc = Runtime.getRuntime().exec(fCommand);
124
125         // The accept times out it the VM does not connect.
126
VirtualMachineImpl virtualMachine;
127         try {
128             virtualMachine = (VirtualMachineImpl)listenConnector.accept(args);
129         } catch (InterruptedIOException JavaDoc e) {
130             proc.destroy();
131             String JavaDoc message= MessageFormat.format(ConnectMessages.SocketLaunchingConnectorImpl_VM_did_not_connect_within_given_time___0__ms_1, new String JavaDoc[]{((Connector.IntegerArgument)args.get("timeout")).value()}); //$NON-NLS-1$
132
throw new VMStartException(message, proc);
133         }
134         
135         virtualMachine.setLaunchedProcess(proc);
136         return virtualMachine;
137     }
138 }
139
Popular Tags