001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdi.internal.connect;
011:
012: import java.io.IOException;
013: import java.util.ArrayList;
014: import java.util.HashMap;
015: import java.util.List;
016: import java.util.Map;
017:
018: import org.eclipse.jdi.internal.VirtualMachineManagerImpl;
019:
020: import com.sun.jdi.VirtualMachine;
021: import com.sun.jdi.connect.AttachingConnector;
022: import com.sun.jdi.connect.Connector;
023: import com.sun.jdi.connect.IllegalConnectorArgumentsException;
024: import com.sun.jdi.connect.spi.Connection;
025:
026: public class SocketAttachingConnectorImpl extends ConnectorImpl
027: implements AttachingConnector {
028: /** Hostname to which is attached. */
029: private String fHostname;
030: /** Port to which is attached. */
031: private int fPort;
032: private int fTimeout;
033:
034: /**
035: * Creates new SocketAttachingConnectorImpl.
036: */
037: public SocketAttachingConnectorImpl(
038: VirtualMachineManagerImpl virtualMachineManager) {
039: super (virtualMachineManager);
040:
041: // Create communication protocol specific transport.
042: SocketTransportImpl transport = new SocketTransportImpl();
043: setTransport(transport);
044: }
045:
046: /**
047: * @return Returns the default arguments.
048: */
049: public Map defaultArguments() {
050: HashMap arguments = new HashMap(2);
051:
052: // Hostname
053: StringArgumentImpl strArg = new StringArgumentImpl(
054: "hostname", ConnectMessages.SocketAttachingConnectorImpl_Machine_name_to_which_to_attach_for_VM_connections_1, ConnectMessages.SocketAttachingConnectorImpl_Host_2, false); //$NON-NLS-1$
055: strArg.setValue("localhost"); //$NON-NLS-1$
056: arguments.put(strArg.name(), strArg);
057:
058: // Port
059: IntegerArgumentImpl intArg = new IntegerArgumentImpl(
060: "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$
061: arguments.put(intArg.name(), intArg);
062:
063: // Timeout
064: IntegerArgumentImpl timeoutArg = new IntegerArgumentImpl(
065: "timeout", ConnectMessages.SocketAttachingConnectorImpl_1, ConnectMessages.SocketAttachingConnectorImpl_2, false, 0, Integer.MAX_VALUE); //$NON-NLS-1$
066: timeoutArg.setValue(0); // by default wait forever
067: arguments.put(timeoutArg.name(), timeoutArg);
068:
069: return arguments;
070: }
071:
072: /**
073: * @return Returns a short identifier for the connector.
074: */
075: public String name() {
076: return "com.sun.jdi.SocketAttach"; //$NON-NLS-1$
077: }
078:
079: /**
080: * @return Returns a human-readable description of this connector and its purpose.
081: */
082: public String description() {
083: return ConnectMessages.SocketAttachingConnectorImpl_Attaches_by_socket_to_other_VMs_5;
084: }
085:
086: /**
087: * Retrieves connection arguments.
088: */
089: private void getConnectionArguments(Map connectionArgs)
090: throws IllegalConnectorArgumentsException {
091: String attribute = ""; //$NON-NLS-1$
092: try {
093: attribute = "hostname"; //$NON-NLS-1$
094: fHostname = ((Connector.StringArgument) connectionArgs
095: .get(attribute)).value();
096: attribute = "port"; //$NON-NLS-1$
097: fPort = ((Connector.IntegerArgument) connectionArgs
098: .get(attribute)).intValue();
099: attribute = "timeout"; //$NON-NLS-1$
100: Object object = connectionArgs.get(attribute);
101: if (object != null) {
102: Connector.IntegerArgument timeoutArg = (IntegerArgument) object;
103: if (timeoutArg.value() != null) {
104: fTimeout = timeoutArg.intValue();
105: }
106: }
107: } catch (ClassCastException e) {
108: throw new IllegalConnectorArgumentsException(
109: ConnectMessages.SocketAttachingConnectorImpl_Connection_argument_is_not_of_the_right_type_6,
110: attribute);
111: } catch (NullPointerException e) {
112: throw new IllegalConnectorArgumentsException(
113: ConnectMessages.SocketAttachingConnectorImpl_Necessary_connection_argument_is_null_7,
114: attribute);
115: } catch (NumberFormatException e) {
116: throw new IllegalConnectorArgumentsException(
117: ConnectMessages.SocketAttachingConnectorImpl_Connection_argument_is_not_a_number_8,
118: attribute);
119: }
120: }
121:
122: /**
123: * Establishes a connection to a virtual machine.
124: * @return Returns a connected Virtual Machine.
125: */
126: public VirtualMachine attach(Map connectionArgs)
127: throws IOException, IllegalConnectorArgumentsException {
128: getConnectionArguments(connectionArgs);
129: Connection connection = null;
130: try {
131: connection = ((SocketTransportImpl) fTransport).attach(
132: fHostname, fPort, fTimeout, 0);
133: } catch (IllegalArgumentException e) {
134: List args = new ArrayList();
135: args.add("hostname"); //$NON-NLS-1$
136: args.add("port"); //$NON-NLS-1$
137: throw new IllegalConnectorArgumentsException(
138: e.getMessage(), args);
139: }
140: return establishedConnection(connection);
141: }
142: }
|