KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > scripting > php > dbginterface > DbgSessionProvider


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.scripting.php.dbginterface;
21
22 import java.util.HashSet JavaDoc;
23 import java.util.Set JavaDoc;
24 import org.netbeans.api.debugger.DebuggerManager;
25 import org.netbeans.api.debugger.Session;
26 import org.netbeans.modules.scripting.php.dbginterface.api.DbgDICookie;
27 import org.netbeans.modules.scripting.php.dbginterface.api.DbgDebugger;
28 import org.netbeans.spi.debugger.ContextProvider;
29 import org.netbeans.spi.debugger.SessionProvider;
30 import org.openide.util.NbBundle;
31
32 /**
33  *
34  * @author marcow
35  */

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