001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 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: * Ivan Popov - Bug 184211: JDI connectors throw NullPointerException if used separately
011: * from Eclipse
012: *******************************************************************************/package org.eclipse.jdi.internal.connect;
013:
014: import java.io.IOException;
015: import java.util.List;
016:
017: import org.eclipse.jdi.Bootstrap;
018: import org.eclipse.jdi.internal.VirtualMachineImpl;
019: import org.eclipse.jdi.internal.VirtualMachineManagerImpl;
020:
021: import com.sun.jdi.VirtualMachine;
022: import com.sun.jdi.connect.Connector;
023: import com.sun.jdi.connect.Transport;
024: import com.sun.jdi.connect.spi.Connection;
025:
026: /**
027: * this class implements the corresponding interfaces
028: * declared by the JDI specification. See the com.sun.jdi package
029: * for more information.
030: *
031: */
032: public abstract class ConnectorImpl implements Connector {
033: /** Virtual machine manager that created this connector. */
034: private VirtualMachineManagerImpl fVirtualMachineManager;
035:
036: /** Transport that is used for communication. */
037: protected Transport fTransport;
038: /** Virtual Machine that is connected. */
039: protected VirtualMachineImpl fVirtualMachine;
040:
041: /**
042: * Creates a new Connector.
043: */
044: public ConnectorImpl(VirtualMachineManagerImpl virtualMachineManager) {
045: fVirtualMachineManager = virtualMachineManager;
046: }
047:
048: /**
049: * @return Returns Virtual Machine Manager.
050: */
051: public VirtualMachineManagerImpl virtualMachineManager() {
052: return fVirtualMachineManager;
053: }
054:
055: /**
056: * @return Returns Virtual Machine Manager.
057: */
058: public VirtualMachineImpl virtualMachine() {
059: return fVirtualMachine;
060: }
061:
062: /**
063: * @return Returns a human-readable description of this connector and its purpose.
064: */
065: public abstract String description();
066:
067: /**
068: * @return Returns a short identifier for the connector.
069: */
070: public abstract String name();
071:
072: /**
073: * Assigns Transport.
074: */
075: /*package*/void setTransport(Transport transport) {
076: fTransport = transport;
077: }
078:
079: /**
080: * @return Returns the transport mechanism used by this connector to establish connections with a target VM.
081: */
082: public Transport transport() {
083: return fTransport;
084: }
085:
086: /**
087: * Closes connection with Virtual Machine.
088: */
089: /*package*/synchronized void close() {
090: virtualMachineManager().removeConnectedVM(fVirtualMachine);
091: }
092:
093: /**
094: * @return Returns a connected Virtual Machine.
095: */
096: protected VirtualMachine establishedConnection(Connection connection)
097: throws IOException {
098: fVirtualMachine = (VirtualMachineImpl) Bootstrap
099: .virtualMachineManager().createVirtualMachine(
100: connection);
101: return fVirtualMachine;
102: }
103:
104: /**
105: * Argument class for arguments that are used to establish a connection.
106: */
107: public abstract class ArgumentImpl implements
108: com.sun.jdi.connect.Connector.Argument {
109: private String fName;
110: private String fDescription;
111: private String fLabel;
112: private boolean fMustSpecify;
113:
114: protected ArgumentImpl(String name, String description,
115: String label, boolean mustSpecify) {
116: fName = name;
117: fLabel = label;
118: fDescription = description;
119: fMustSpecify = mustSpecify;
120: }
121:
122: public String name() {
123: return fName;
124: }
125:
126: public String description() {
127: return fDescription;
128: }
129:
130: public String label() {
131: return fLabel;
132: }
133:
134: public boolean mustSpecify() {
135: return fMustSpecify;
136: }
137:
138: public abstract String value();
139:
140: public abstract void setValue(String value);
141:
142: public abstract boolean isValid(String value);
143:
144: public abstract String toString();
145: }
146:
147: public class StringArgumentImpl extends ArgumentImpl implements
148: com.sun.jdi.connect.Connector.StringArgument {
149: private static final long serialVersionUID = 6009335074727417445L;
150:
151: private String fValue;
152:
153: protected StringArgumentImpl(String name, String description,
154: String label, boolean mustSpecify) {
155: super (name, description, label, mustSpecify);
156: }
157:
158: public String value() {
159: return fValue;
160: }
161:
162: public void setValue(String value) {
163: fValue = value;
164: }
165:
166: public boolean isValid(String value) {
167: return true;
168: }
169:
170: public String toString() {
171: return fValue;
172: }
173:
174: }
175:
176: public class IntegerArgumentImpl extends ArgumentImpl implements
177: com.sun.jdi.connect.Connector.IntegerArgument {
178: private static final long serialVersionUID = 6009335074727417445L;
179: private Integer fValue;
180: private int fMin;
181: private int fMax;
182:
183: protected IntegerArgumentImpl(String name, String description,
184: String label, boolean mustSpecify, int min, int max) {
185: super (name, description, label, mustSpecify);
186: fMin = min;
187: fMax = max;
188: }
189:
190: public String value() {
191: return (fValue == null) ? null : fValue.toString();
192: }
193:
194: public void setValue(String value) {
195: fValue = new Integer(value);
196: }
197:
198: public boolean isValid(String value) {
199: Integer val;
200: try {
201: val = new Integer(value);
202: } catch (NumberFormatException e) {
203: return false;
204: }
205: return isValid(val.intValue());
206: }
207:
208: public String toString() {
209: return value();
210: }
211:
212: public int intValue() {
213: return fValue.intValue();
214: }
215:
216: public void setValue(int value) {
217: fValue = new Integer(value);
218: }
219:
220: public int min() {
221: return fMin;
222: }
223:
224: public int max() {
225: return fMax;
226: }
227:
228: public boolean isValid(int value) {
229: return fMin <= value && value <= fMax;
230: }
231:
232: public String stringValueOf(int value) {
233: return new Integer(value).toString();
234: }
235: }
236:
237: public class BooleanArgumentImpl extends ArgumentImpl implements
238: com.sun.jdi.connect.Connector.BooleanArgument {
239: private static final long serialVersionUID = 6009335074727417445L;
240: private Boolean fValue;
241:
242: protected BooleanArgumentImpl(String name, String description,
243: String label, boolean mustSpecify) {
244: super (name, description, label, mustSpecify);
245: }
246:
247: public String value() {
248: return (fValue == null) ? null : fValue.toString();
249: }
250:
251: public void setValue(String value) {
252: fValue = Boolean.valueOf(value);
253: }
254:
255: public boolean isValid(String value) {
256: return true;
257: }
258:
259: public String toString() {
260: return value();
261: }
262:
263: public boolean booleanValue() {
264: return fValue.booleanValue();
265: }
266:
267: public void setValue(boolean value) {
268: fValue = Boolean.valueOf(value);
269: }
270:
271: public String stringValueOf(boolean value) {
272: return Boolean.valueOf(value).toString();
273: }
274: }
275:
276: public class SelectedArgumentImpl extends StringArgumentImpl
277: implements com.sun.jdi.connect.Connector.SelectedArgument {
278: private static final long serialVersionUID = 6009335074727417445L;
279: private List fChoices;
280:
281: protected SelectedArgumentImpl(String name, String description,
282: String label, boolean mustSpecify, List choices) {
283: super (name, description, label, mustSpecify);
284: fChoices = choices;
285: }
286:
287: public List choices() {
288: return fChoices;
289: }
290:
291: public boolean isValid(java.lang.String value) {
292: return fChoices.contains(value);
293: }
294: }
295: }
|