KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > debugger > jpda > LaunchingSessionProvider


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.modules.debugger.jpda;
21
22 import java.util.HashSet JavaDoc;
23 import java.util.Map JavaDoc;
24 import org.netbeans.api.debugger.DebuggerManager;
25 import org.netbeans.api.debugger.Session;
26 import org.netbeans.api.debugger.jpda.JPDADebugger;
27 import org.netbeans.api.debugger.jpda.LaunchingDICookie;
28 import org.netbeans.spi.debugger.SessionProvider;
29 import org.netbeans.spi.debugger.ContextProvider;
30 import org.openide.util.NbBundle;
31
32
33 /**
34  *
35  * @author Jan Jancura
36  */

37 public class LaunchingSessionProvider extends SessionProvider {
38
39     private ContextProvider contextProvider;
40     private LaunchingDICookie launchingCookie;
41     
42     public LaunchingSessionProvider (ContextProvider contextProvider) {
43         this.contextProvider = contextProvider;
44         launchingCookie = (LaunchingDICookie) contextProvider.lookupFirst
45             (null, LaunchingDICookie.class);
46     };
47     
48     public String JavaDoc getSessionName () {
49         Map JavaDoc arguments = (Map JavaDoc) contextProvider.lookupFirst
50             (null, Map JavaDoc.class);
51         if (arguments != null) {
52             String JavaDoc processName = (String JavaDoc) arguments.get ("name");
53             if (processName != null)
54                 return findUnique (processName);
55         }
56         String JavaDoc sessionName = launchingCookie.getClassName ();
57         int i = sessionName.lastIndexOf ('.');
58         if (i >= 0)
59             sessionName = sessionName.substring (i + 1);
60         return findUnique (sessionName);
61     };
62     
63     public String JavaDoc getLocationName () {
64         return NbBundle.getMessage
65             (LaunchingSessionProvider.class, "CTL_Localhost");
66     }
67     
68     public String JavaDoc getTypeID () {
69         return JPDADebugger.SESSION_ID;
70     }
71     
72     public Object JavaDoc[] getServices () {
73         return new Object JavaDoc [0];
74     }
75     
76     static String JavaDoc findUnique (String JavaDoc sessionName) {
77         DebuggerManager cd = DebuggerManager.getDebuggerManager ();
78         Session[] ds = cd.getSessions ();
79         
80         // 1) finds all already used indexes and puts them to HashSet
81
int i, k = ds.length;
82         HashSet JavaDoc<Integer JavaDoc> m = new HashSet JavaDoc<Integer JavaDoc>();
83         for (i = 0; i < k; i++) {
84             String JavaDoc pn = ds [i].getName ();
85             if (!pn.startsWith (sessionName)) continue;
86             if (pn.equals (sessionName)) {
87                 m.add (new Integer JavaDoc (0));
88                 continue;
89             }
90
91             try {
92                 int t = Integer.parseInt (pn.substring (sessionName.length ()));
93                 m.add (new Integer JavaDoc (t));
94             } catch (Exception JavaDoc e) {
95             }
96         }
97         
98         // 2) finds first unused index in m
99
k = m.size ();
100         for (i = 0; i < k; i++)
101            if (!m.contains (new Integer JavaDoc (i)))
102                break;
103         if (i > 0) sessionName = sessionName + i;
104         return sessionName;
105     };
106 }
107
108
Popular Tags