KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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  * Ivan Popov - Bug 184211: JDI connectors throw NullPointerException if used separately
11  * from Eclipse
12  *******************************************************************************/

13 package org.eclipse.jdi.internal.connect;
14
15
16 import java.io.IOException JavaDoc;
17 import java.util.HashMap 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.IllegalConnectorArgumentsException;
24 import com.sun.jdi.connect.ListeningConnector;
25
26
27 public class SocketListeningConnectorImpl extends ConnectorImpl implements ListeningConnector {
28     /** Port to which is attached. */
29     private int fPort;
30     /** Timeout before accept returns. */
31     private int fTimeout;
32     
33     
34     /**
35      * Creates new SocketAttachingConnectorImpl.
36      */

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

48     public Map JavaDoc defaultArguments() {
49         HashMap JavaDoc arguments = new HashMap JavaDoc(1);
50         
51         // Port
52
IntegerArgumentImpl intArg = new IntegerArgumentImpl("port", ConnectMessages.SocketListeningConnectorImpl_Port_number_at_which_to_listen_for_VM_connections_1, ConnectMessages.SocketListeningConnectorImpl_Port_2, true, SocketTransportImpl.MIN_PORTNR, SocketTransportImpl.MAX_PORTNR); //$NON-NLS-1$
53
arguments.put(intArg.name(), intArg);
54         
55         // Timeout
56
intArg = new IntegerArgumentImpl("timeout", ConnectMessages.SocketListeningConnectorImpl_Timeout_before_accept_returns_3, ConnectMessages.SocketListeningConnectorImpl_Timeout_4, false, 0, Integer.MAX_VALUE); //$NON-NLS-1$
57
arguments.put(intArg.name(), intArg);
58         
59         return arguments;
60     }
61     
62     /**
63      * @return Returns a short identifier for the connector.
64      */

65     public String JavaDoc name() {
66         return "com.sun.jdi.SocketListen"; //$NON-NLS-1$
67
}
68     
69     /**
70      * @return Returns a human-readable description of this connector and its purpose.
71      */

72     public String JavaDoc description() {
73         return ConnectMessages.SocketListeningConnectorImpl_Accepts_socket_connections_initiated_by_other_VMs_5;
74     }
75     
76     /**
77      * Retrieves connection arguments.
78      */

79     private void getConnectionArguments(Map JavaDoc connectionArgs) throws IllegalConnectorArgumentsException {
80         String JavaDoc attribute = "port"; //$NON-NLS-1$
81
try {
82             // If listening port is not specified, use port 0
83
IntegerArgument argument = (IntegerArgument) connectionArgs.get(attribute);
84             if (argument != null && argument.value() != null) {
85                 fPort = argument.intValue();
86             } else {
87                 fPort = 0;
88             }
89             // Note that timeout is not used in SUN's ListeningConnector, but is used by our
90
// LaunchingConnector.
91
attribute = "timeout"; //$NON-NLS-1$
92
argument = (IntegerArgument) connectionArgs.get(attribute);
93              if (argument != null && argument.value() != null) {
94                  fTimeout = argument.intValue();
95              } else {
96                  fTimeout = 0;
97              }
98         } catch (ClassCastException JavaDoc e) {
99             throw new IllegalConnectorArgumentsException(ConnectMessages.SocketListeningConnectorImpl_Connection_argument_is_not_of_the_right_type_6, attribute);
100         } catch (NullPointerException JavaDoc e) {
101             throw new IllegalConnectorArgumentsException(ConnectMessages.SocketListeningConnectorImpl_Necessary_connection_argument_is_null_7, attribute);
102         } catch (NumberFormatException JavaDoc e) {
103             throw new IllegalConnectorArgumentsException(ConnectMessages.SocketListeningConnectorImpl_Connection_argument_is_not_a_number_8, attribute);
104         }
105     }
106     
107     /**
108      * Listens for one or more connections initiated by target VMs.
109      * @return Returns the address at which the connector is listening for a connection.
110      */

111     public String JavaDoc startListening(Map JavaDoc connectionArgs) throws IOException JavaDoc, IllegalConnectorArgumentsException {
112         getConnectionArguments(connectionArgs);
113         String JavaDoc result = null;
114         try {
115             result = ((SocketTransportImpl)fTransport).startListening(fPort);
116         } catch (IllegalArgumentException JavaDoc e) {
117             throw new IllegalConnectorArgumentsException(ConnectMessages.SocketListeningConnectorImpl_ListeningConnector_Socket_Port, "port"); //$NON-NLS-1$
118
}
119         return result;
120     }
121     
122     /**
123      * Cancels listening for connections.
124      */

125     public void stopListening(Map JavaDoc connectionArgs) throws IOException JavaDoc {
126         ((SocketTransportImpl)fTransport).stopListening();
127     }
128         
129     /**
130      * Waits for a target VM to attach to this connector.
131      * @return Returns a connected Virtual Machine.
132      */

133     public VirtualMachine accept(Map JavaDoc connectionArgs) throws IOException JavaDoc, IllegalConnectorArgumentsException {
134         getConnectionArguments(connectionArgs);
135         SocketConnection connection = (SocketConnection) ((SocketTransportImpl)fTransport).accept(fTimeout, 0);
136         return establishedConnection(connection);
137     }
138     
139     /**
140      * @return Returns whether this listening connector supports multiple connections for a single argument map.
141      */

142     public boolean supportsMultipleConnections() {
143         return true;
144     }
145     
146     /**
147      * @return Returns port number that is listened to.
148      */

149     public int listeningPort() {
150         return fPort;
151     }
152 }
153
Popular Tags