KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.util.ArrayList JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import org.eclipse.jdi.internal.VirtualMachineManagerImpl;
21
22 import com.sun.jdi.VirtualMachine;
23 import com.sun.jdi.connect.AttachingConnector;
24 import com.sun.jdi.connect.Connector;
25 import com.sun.jdi.connect.IllegalConnectorArgumentsException;
26 import com.sun.jdi.connect.spi.Connection;
27
28 public class SocketAttachingConnectorImpl extends ConnectorImpl implements AttachingConnector {
29     /** Hostname to which is attached. */
30     private String JavaDoc fHostname;
31     /** Port to which is attached. */
32     private int fPort;
33     private int fTimeout;
34     
35     /**
36      * Creates new SocketAttachingConnectorImpl.
37      */

38     public SocketAttachingConnectorImpl(VirtualMachineManagerImpl virtualMachineManager) {
39         super(virtualMachineManager);
40         
41         // Create communication protocol specific transport.
42
SocketTransportImpl transport = new SocketTransportImpl();
43         setTransport(transport);
44     }
45     
46     /**
47      * @return Returns the default arguments.
48      */

49     public Map JavaDoc defaultArguments() {
50         HashMap JavaDoc arguments = new HashMap JavaDoc(2);
51         
52         // Hostname
53
StringArgumentImpl strArg = new StringArgumentImpl("hostname", ConnectMessages.SocketAttachingConnectorImpl_Machine_name_to_which_to_attach_for_VM_connections_1, ConnectMessages.SocketAttachingConnectorImpl_Host_2, false); //$NON-NLS-1$
54
strArg.setValue("localhost"); //$NON-NLS-1$
55
arguments.put(strArg.name(), strArg);
56         
57         // Port
58
IntegerArgumentImpl intArg = new IntegerArgumentImpl("port", ConnectMessages.SocketAttachingConnectorImpl_Port_number_to_which_to_attach_for_VM_connections_3, ConnectMessages.SocketAttachingConnectorImpl_Port_4, true, SocketTransportImpl.MIN_PORTNR, SocketTransportImpl.MAX_PORTNR); //$NON-NLS-1$
59
arguments.put(intArg.name(), intArg);
60         
61         // Timeout
62
IntegerArgumentImpl timeoutArg = new IntegerArgumentImpl("timeout", ConnectMessages.SocketAttachingConnectorImpl_1, ConnectMessages.SocketAttachingConnectorImpl_2, false, 0, Integer.MAX_VALUE); //$NON-NLS-1$
63
timeoutArg.setValue(0); // by default wait forever
64
arguments.put(timeoutArg.name(), timeoutArg);
65         
66         return arguments;
67     }
68     
69     /**
70      * @return Returns a short identifier for the connector.
71      */

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

79     public String JavaDoc description() {
80         return ConnectMessages.SocketAttachingConnectorImpl_Attaches_by_socket_to_other_VMs_5;
81     }
82     
83     /**
84      * Retrieves connection arguments.
85      */

86     private void getConnectionArguments(Map JavaDoc connectionArgs) throws IllegalConnectorArgumentsException {
87         String JavaDoc attribute = ""; //$NON-NLS-1$
88
try {
89             attribute = "hostname"; //$NON-NLS-1$
90
fHostname = ((Connector.StringArgument)connectionArgs.get(attribute)).value();
91             attribute = "port"; //$NON-NLS-1$
92
fPort = ((Connector.IntegerArgument)connectionArgs.get(attribute)).intValue();
93             attribute = "timeout"; //$NON-NLS-1$
94
Object JavaDoc object = connectionArgs.get(attribute);
95             if (object != null) {
96                Connector.IntegerArgument timeoutArg = (IntegerArgument) object;
97                if (timeoutArg.value() != null) {
98                 fTimeout = timeoutArg.intValue();
99                }
100             }
101         } catch (ClassCastException JavaDoc e) {
102             throw new IllegalConnectorArgumentsException(ConnectMessages.SocketAttachingConnectorImpl_Connection_argument_is_not_of_the_right_type_6, attribute);
103         } catch (NullPointerException JavaDoc e) {
104             throw new IllegalConnectorArgumentsException(ConnectMessages.SocketAttachingConnectorImpl_Necessary_connection_argument_is_null_7, attribute);
105         } catch (NumberFormatException JavaDoc e) {
106             throw new IllegalConnectorArgumentsException(ConnectMessages.SocketAttachingConnectorImpl_Connection_argument_is_not_a_number_8, attribute);
107         }
108     }
109     
110     /**
111      * Establishes a connection to a virtual machine.
112      * @return Returns a connected Virtual Machine.
113      */

114     public VirtualMachine attach(Map JavaDoc connectionArgs) throws IOException JavaDoc, IllegalConnectorArgumentsException {
115         getConnectionArguments(connectionArgs);
116         Connection connection = null;
117         try {
118             connection = ((SocketTransportImpl)fTransport).attach(fHostname, fPort, fTimeout, 0);
119         } catch (IllegalArgumentException JavaDoc e) {
120             List JavaDoc args = new ArrayList JavaDoc();
121             args.add("hostname"); //$NON-NLS-1$
122
args.add("port"); //$NON-NLS-1$
123
throw new IllegalConnectorArgumentsException(e.getMessage(), args);
124         }
125         return establishedConnection(connection);
126     }
127 }
128
Popular Tags