KickJava   Java API By Example, From Geeks To Geeks.

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


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.IllegalConnectorArgumentsException;
25 import com.sun.jdi.connect.ListeningConnector;
26 import com.sun.jdi.connect.Connector.Argument;
27
28 import java.io.IOException JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.Map JavaDoc;
31
32 /**
33  * Listens on given port for some connection of remotely running JDK
34  * and returns VirtualMachine for it.
35  *
36  * <br><br>
37  * <b>How to use it:</b>
38  * <pre style="background-color: rgb(255, 255, 153);">
39  * DebuggerInfo di = DebuggerInfo.create (
40  * "My First Listening Debugger Info",
41  * new Object [] {
42  * ListeningDICookie.create (
43  * 1234
44  * )
45  * }
46  * );
47  * DebuggerManager.getDebuggerManager ().startDebugging (di);</pre>
48  *
49  * @author Jan Jancura
50  */

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

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

76     public static ListeningDICookie create (
77         ListeningConnector listeningConnector,
78         Map JavaDoc<String JavaDoc, ? extends Argument> args
79     ) {
80         return new ListeningDICookie (
81             listeningConnector,
82             args
83         );
84     }
85
86     /**
87      * Creates a new instance of ListeningDICookie for given parameters.
88      *
89      * @param portNumber a number of port to listen on
90      * @return a new instance of ListeningDICookie for given parameters
91      */

92     public static ListeningDICookie create (
93         int portNumber
94     ) {
95         return new ListeningDICookie (
96             findListeningConnector ("socket"),
97             getArgs (
98                 findListeningConnector ("socket"),
99                 portNumber
100             )
101         );
102     }
103
104     /**
105      * Creates a new instance of ListeningDICookie for given parameters.
106      *
107      * @param name a name of shared memory block to listen on
108      * @return a new instance of ListeningDICookie for given parameters
109      */

110     public static ListeningDICookie create (
111         String JavaDoc name
112     ) {
113         return new ListeningDICookie (
114             findListeningConnector ("socket"),
115             getArgs (
116                 findListeningConnector ("socket"),
117                 name
118             )
119         );
120     }
121
122     private static ListeningConnector findListeningConnector (String JavaDoc s) {
123         Iterator JavaDoc iter = Bootstrap.virtualMachineManager ().
124             listeningConnectors ().iterator ();
125         while (iter.hasNext ()) {
126             ListeningConnector ac = (ListeningConnector) iter.next ();
127             if (ac.transport() != null && ac.transport ().name ().toLowerCase ().indexOf (s) > -1)
128                 return ac;
129         }
130         return null;
131     }
132
133     private static Map JavaDoc<String JavaDoc, ? extends Argument> getArgs (
134         ListeningConnector listeningConnector,
135         int portNumber
136     ) {
137         Map JavaDoc<String JavaDoc, ? extends Argument> args = listeningConnector.defaultArguments ();
138         args.get ("port").setValue ("" + portNumber);
139         return args;
140     }
141
142     private static Map JavaDoc<String JavaDoc, ? extends Argument> getArgs (
143         ListeningConnector listeningConnector,
144         String JavaDoc name
145     ) {
146         Map JavaDoc<String JavaDoc, ? extends Argument> args = listeningConnector.defaultArguments ();
147         args.get ("name").setValue (name);
148         return args;
149     }
150
151     /**
152      * Returns instance of ListeningConnector.
153      *
154      * @return instance of ListeningConnector
155      */

156     public ListeningConnector getListeningConnector () {
157         return listeningConnector;
158     }
159
160     /**
161      * Returns map of arguments to be used.
162      *
163      * @return map of arguments to be used
164      */

165     public Map JavaDoc<String JavaDoc, ? extends Argument> getArgs () {
166         return args;
167     }
168
169     /**
170      * Returns port number.
171      *
172      * @return port number
173      */

174     public int getPortNumber () {
175         Argument a = (Argument) args.get ("port");
176         if (a == null) return -1;
177         String JavaDoc pn = a.value ();
178         if (pn == null) return -1;
179         try {
180             return Integer.parseInt (pn);
181         } catch (NumberFormatException JavaDoc e) {
182             return -1;
183         }
184     }
185
186     /**
187      * Returns shared memory block name.
188      *
189      * @return shared memory block name
190      */

191     public String JavaDoc getSharedMemoryName () {
192         Argument a = (Argument) args.get ("name");
193         if (a == null) return null;
194         return a.value ();
195     }
196
197     /**
198      * Creates a new instance of VirtualMachine for this DebuggerInfo Cookie.
199      *
200      * @return a new instance of VirtualMachine for this DebuggerInfo Cookie
201      */

202     public VirtualMachine getVirtualMachine () throws IOException JavaDoc,
203     IllegalConnectorArgumentsException {
204         try {
205             try {
206                 listeningConnector.startListening(args);
207             } catch (Exception JavaDoc e) {
208                 // most probably already listening
209
}
210             return listeningConnector.accept (args);
211         } finally {
212             try {
213                 listeningConnector.stopListening(args);
214             } catch (Exception JavaDoc e) {
215                 // most probably not listening anymore
216
}
217         }
218     }
219 }
220
Popular Tags