KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > debugger > jpda > AttachingDICookie


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.api.debugger.jpda;
21
22 import com.sun.jdi.Bootstrap;
23 import com.sun.jdi.VirtualMachine;
24 import com.sun.jdi.connect.AttachingConnector;
25 import com.sun.jdi.connect.Connector.Argument;
26 import com.sun.jdi.connect.IllegalConnectorArgumentsException;
27 import java.io.IOException JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Map JavaDoc;
30
31
32 /**
33  * Attaches to some already running JDK and returns VirtualMachine for it.
34  *
35  * <br><br>
36  * <b>How to use it:</b>
37  * <pre style="background-color: rgb(255, 255, 153);">
38  * DebuggerInfo di = DebuggerInfo.create (
39  * "My Attaching First Debugger Info",
40  * new Object [] {
41  * AttachingDICookie.create (
42  * "localhost",
43  * 1234
44  * )
45  * }
46  * );
47  * DebuggerManager.getDebuggerManager ().startDebugging (di);</pre>
48  *
49  * @author Jan Jancura
50  */

51 public final class AttachingDICookie extends AbstractDICookie {
52
53     /**
54      * Public ID used for registration in Meta-inf/debugger.
55      */

56     public static final String JavaDoc ID = "netbeans-jpda-AttachingDICookie";
57
58     private AttachingConnector attachingConnector;
59     private Map JavaDoc<String JavaDoc,? extends Argument> args;
60
61     
62     private AttachingDICookie (
63         AttachingConnector attachingConnector,
64         Map JavaDoc<String JavaDoc,? extends Argument> args
65     ) {
66         this.attachingConnector = attachingConnector;
67         this.args = args;
68     }
69
70     /**
71      * Creates a new instance of AttachingDICookie for given parameters.
72      *
73      * @param attachingConnector a connector to be used
74      * @param args map of arguments
75      * @return a new instance of AttachingDICookie for given parameters
76      */

77     public static AttachingDICookie create (
78         AttachingConnector attachingConnector,
79         Map JavaDoc<String JavaDoc,? extends Argument> args
80     ) {
81         return new AttachingDICookie (
82             attachingConnector,
83             args
84         );
85     }
86
87     /**
88      * Creates a new instance of AttachingDICookie for given parameters.
89      *
90      * @param hostName a name of computer to attach to
91      * @param portNumber a potr number
92      * @return a new instance of AttachingDICookie for given parameters
93      */

94     public static AttachingDICookie create (
95         String JavaDoc hostName,
96         int portNumber
97     ) {
98         return new AttachingDICookie (
99             findAttachingConnector ("socket"),
100             getArgs (
101                 findAttachingConnector ("socket"),
102                 hostName,
103                 portNumber
104             )
105         );
106     }
107
108     /**
109      * Creates a new instance of AttachingDICookie for given parameters.
110      *
111      * @param name a name of shared memory block
112      * @return a new instance of AttachingDICookie for given parameters
113      */

114     public static AttachingDICookie create (
115         String JavaDoc name
116     ) {
117         return new AttachingDICookie (
118             findAttachingConnector ("shmem"),
119             getArgs (
120                 findAttachingConnector ("shmem"),
121                 name
122             )
123         );
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 JavaDoc<String JavaDoc,? 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) return -1;
152         String JavaDoc pn = a.value ();
153         if (pn == null) return -1;
154         return Integer.parseInt (pn);
155     }
156
157     /**
158      * Returns name of computer.
159      *
160      * @return name of computer
161      */

162     public String JavaDoc getHostName () {
163         Argument a = args.get ("hostname");
164         if (a == null) return null;
165         return a.value ();
166     }
167
168     /**
169      * Returns shared memory block name.
170      *
171      * @return shared memory block name
172      */

173     public String JavaDoc getSharedMemoryName () {
174         Argument a = args.get ("name");
175         if (a == null) return null;
176         return a.value ();
177     }
178
179     /**
180      * Creates a new instance of VirtualMachine for this DebuggerInfo Cookie.
181      *
182      * @return a new instance of VirtualMachine for this DebuggerInfo Cookie
183      */

184     public VirtualMachine getVirtualMachine () throws IOException JavaDoc,
185     IllegalConnectorArgumentsException {
186         return attachingConnector.attach (args);
187     }
188     
189     
190     // private helper methods ..................................................
191

192     private static Map JavaDoc<String JavaDoc,? extends Argument> getArgs (
193         AttachingConnector attachingConnector,
194         String JavaDoc hostName,
195         int portNumber
196     ) {
197         Map JavaDoc<String JavaDoc,? extends Argument> args = attachingConnector.defaultArguments ();
198         args.get ("hostname").setValue (hostName);
199         args.get ("port").setValue ("" + portNumber);
200         return args;
201     }
202
203     private static Map JavaDoc<String JavaDoc,? extends Argument> getArgs (
204         AttachingConnector attachingConnector,
205         String JavaDoc name
206     ) {
207         Map JavaDoc<String JavaDoc,? extends Argument> args = attachingConnector.defaultArguments ();
208         args.get ("name").setValue (name);
209         return args;
210     }
211     
212     private static AttachingConnector findAttachingConnector (String JavaDoc s) {
213         Iterator JavaDoc<AttachingConnector> iter = Bootstrap.virtualMachineManager ().
214             attachingConnectors ().iterator ();
215         while (iter.hasNext ()) {
216             AttachingConnector ac = iter.next ();
217             if (ac.transport() != null && ac.transport ().name ().toLowerCase ().indexOf (s) > -1)
218                 return ac;
219         }
220         return null;
221     }
222 }
223
Popular Tags