001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.api.debugger.jpda;
043:
044: import com.sun.jdi.Bootstrap;
045: import com.sun.jdi.VirtualMachine;
046: import com.sun.jdi.connect.AttachingConnector;
047: import com.sun.jdi.connect.Connector.Argument;
048: import com.sun.jdi.connect.IllegalConnectorArgumentsException;
049: import java.io.IOException;
050: import java.util.Iterator;
051: import java.util.Map;
052:
053: /**
054: * Attaches to some already running JDK and returns VirtualMachine for it.
055: *
056: * <br><br>
057: * <b>How to use it:</b>
058: * <pre style="background-color: rgb(255, 255, 153);">
059: * DebuggerInfo di = DebuggerInfo.create (
060: * "My Attaching First Debugger Info",
061: * new Object [] {
062: * AttachingDICookie.create (
063: * "localhost",
064: * 1234
065: * )
066: * }
067: * );
068: * DebuggerManager.getDebuggerManager ().startDebugging (di);</pre>
069: *
070: * @author Jan Jancura
071: */
072: public final class AttachingDICookie extends AbstractDICookie {
073:
074: /**
075: * Public ID used for registration in Meta-inf/debugger.
076: */
077: public static final String ID = "netbeans-jpda-AttachingDICookie";
078:
079: private AttachingConnector attachingConnector;
080: private Map<String, ? extends Argument> args;
081:
082: private AttachingDICookie(AttachingConnector attachingConnector,
083: Map<String, ? extends Argument> args) {
084: this .attachingConnector = attachingConnector;
085: this .args = args;
086: }
087:
088: /**
089: * Creates a new instance of AttachingDICookie for given parameters.
090: *
091: * @param attachingConnector a connector to be used
092: * @param args map of arguments
093: * @return a new instance of AttachingDICookie for given parameters
094: */
095: public static AttachingDICookie create(
096: AttachingConnector attachingConnector,
097: Map<String, ? extends Argument> args) {
098: return new AttachingDICookie(attachingConnector, args);
099: }
100:
101: /**
102: * Creates a new instance of AttachingDICookie for given parameters.
103: *
104: * @param hostName a name of computer to attach to
105: * @param portNumber a potr number
106: * @return a new instance of AttachingDICookie for given parameters
107: */
108: public static AttachingDICookie create(String hostName,
109: int portNumber) {
110: return new AttachingDICookie(findAttachingConnector("socket"),
111: getArgs(findAttachingConnector("socket"), hostName,
112: portNumber));
113: }
114:
115: /**
116: * Creates a new instance of AttachingDICookie for given parameters.
117: *
118: * @param name a name of shared memory block
119: * @return a new instance of AttachingDICookie for given parameters
120: */
121: public static AttachingDICookie create(String name) {
122: return new AttachingDICookie(findAttachingConnector("shmem"),
123: getArgs(findAttachingConnector("shmem"), name));
124: }
125:
126: /**
127: * Returns instance of AttachingDICookie.
128: *
129: * @return instance of AttachingDICookie
130: */
131: public AttachingConnector getAttachingConnector() {
132: return attachingConnector;
133: }
134:
135: /**
136: * Returns map of arguments.
137: *
138: * @return map of arguments
139: */
140: public Map<String, ? extends Argument> getArgs() {
141: return args;
142: }
143:
144: /**
145: * Returns port number.
146: *
147: * @return port number
148: */
149: public int getPortNumber() {
150: Argument a = args.get("port");
151: if (a == null)
152: return -1;
153: String pn = a.value();
154: if (pn == null)
155: return -1;
156: return Integer.parseInt(pn);
157: }
158:
159: /**
160: * Returns name of computer.
161: *
162: * @return name of computer
163: */
164: public String getHostName() {
165: Argument a = args.get("hostname");
166: if (a == null)
167: return null;
168: return a.value();
169: }
170:
171: /**
172: * Returns shared memory block name.
173: *
174: * @return shared memory block name
175: */
176: public String getSharedMemoryName() {
177: Argument a = args.get("name");
178: if (a == null)
179: return null;
180: return a.value();
181: }
182:
183: /**
184: * Creates a new instance of VirtualMachine for this DebuggerInfo Cookie.
185: *
186: * @return a new instance of VirtualMachine for this DebuggerInfo Cookie
187: */
188: public VirtualMachine getVirtualMachine() throws IOException,
189: IllegalConnectorArgumentsException {
190: return attachingConnector.attach(args);
191: }
192:
193: // private helper methods ..................................................
194:
195: private static Map<String, ? extends Argument> getArgs(
196: AttachingConnector attachingConnector, String hostName,
197: int portNumber) {
198: Map<String, ? extends Argument> args = attachingConnector
199: .defaultArguments();
200: args.get("hostname").setValue(hostName);
201: args.get("port").setValue("" + portNumber);
202: return args;
203: }
204:
205: private static Map<String, ? extends Argument> getArgs(
206: AttachingConnector attachingConnector, String name) {
207: Map<String, ? extends Argument> args = attachingConnector
208: .defaultArguments();
209: args.get("name").setValue(name);
210: return args;
211: }
212:
213: private static AttachingConnector findAttachingConnector(String s) {
214: Iterator<AttachingConnector> iter = Bootstrap
215: .virtualMachineManager().attachingConnectors()
216: .iterator();
217: while (iter.hasNext()) {
218: AttachingConnector ac = iter.next();
219: if (ac.transport() != null
220: && ac.transport().name().toLowerCase().indexOf(s) > -1)
221: return ac;
222: }
223: return null;
224: }
225: }
|