KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > coach > actor > namingBrowser > NameNode


1 /***************************************************************************/
2 /* COACH: Component Based Open Source Architecture for */
3 /* Distributed Telecom Applications */
4 /* See: http://www.objectweb.org/ */
5 /* */
6 /* Copyright (C) 2003 Lucent Technologies Nederland BV */
7 /* Bell Labs Advanced Technologies - EMEA */
8 /* */
9 /* Initial developer(s): Harold Batteram */
10 /* */
11 /* This library is free software; you can redistribute it and/or */
12 /* modify it under the terms of the GNU Lesser General Public */
13 /* License as published by the Free Software Foundation; either */
14 /* version 2.1 of the License, or (at your option) any later version. */
15 /* */
16 /* This library is distributed in the hope that it will be useful, */
17 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
18 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
19 /* Lesser General Public License for more details. */
20 /* */
21 /* You should have received a copy of the GNU Lesser General Public */
22 /* License along with this library; if not, write to the Free Software */
23 /* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
24 /***************************************************************************/
25 package org.coach.actor.namingBrowser;
26
27 import javax.swing.tree.*;
28 import org.omg.CosNaming.*;
29 import org.omg.CosNaming.NamingContextPackage.*;
30 import org.omg.CORBA.*;
31
32 public class NameNode extends DefaultMutableTreeNode {
33     private static org.omg.CORBA.ORB JavaDoc orb;
34     private boolean explored = false;
35     private Binding binding;
36     private NamingContext context;
37     private org.omg.CORBA.Object JavaDoc obj;
38
39     public NameNode(Binding bi, NamingContext nc) {
40         binding = bi;
41         context = nc;
42         setUserObject(this);
43     }
44
45     public static void setOrb(org.omg.CORBA.ORB JavaDoc o) {
46         orb = o;
47     }
48
49     public static org.omg.CORBA.ORB JavaDoc getOrb() {
50         return orb;
51     }
52     
53     public boolean getAllowsChildren() {
54         return isContext();
55     }
56
57     public boolean isLeaf() {
58         return !isContext();
59     }
60
61     public NamingContext getNamingContext() {
62         return context;
63     }
64
65     public void explore() {
66         explore(false);
67     }
68
69     public boolean isExplored() {
70         return explored;
71     }
72
73     public boolean isContext() {
74         return context != null;
75     }
76
77     public String JavaDoc toString() {
78         return binding.binding_name[0].id + " " + binding.binding_name[0].kind;
79     }
80
81     public String JavaDoc getIor() {
82         String JavaDoc ior = "context!";
83         org.omg.CORBA.Object JavaDoc obj = getObject();
84         if (obj != null) {
85             try {
86                 ior = orb.object_to_string(obj);
87             } catch (Exception JavaDoc e) {
88                 e.printStackTrace();
89             }
90         }
91         return ior;
92     }
93
94     public org.omg.CORBA.Object JavaDoc getObject() {
95         if (obj != null) {
96             return obj;
97         }
98         if (!isContext()) {
99             try {
100                 NameNode p = (NameNode)getParent();
101                 NamingContext ctx = p.getNamingContext();
102                 obj = ctx.resolve(binding.binding_name);
103                 return obj;
104             } catch (Exception JavaDoc e) {
105                 e.printStackTrace();
106             }
107         }
108         return null;
109     }
110
111     public boolean ping() {
112         if (!isContext()) {
113             String JavaDoc ior = getIor();
114             org.omg.CORBA.Object JavaDoc obj = orb.string_to_object(ior);
115             if (obj != null) {
116                 try {
117                     obj._is_a("dummy");
118                 } catch (Exception JavaDoc e) {
119                     return false;
120                 }
121                 return true;
122             }
123         }
124         return false;
125     }
126
127     public void explore(boolean force) {
128         if(!isExplored() || force) {
129             removeAllChildren();
130             BindingListHolder blHolder = new BindingListHolder();
131             BindingIteratorHolder biHolder = new BindingIteratorHolder();
132
133             context.list(1000, blHolder, biHolder);
134
135             Binding[] children = blHolder.value;
136
137             NamingContext nc;
138             try {
139                 for(int i=0; i < children.length; i++) {
140                    if (children[i].binding_type == BindingType.ncontext) {
141                         nc = NamingContextHelper.narrow(context.resolve(children[i].binding_name));
142                     } else {
143                         nc = null;
144                     }
145                     add(new NameNode(children[i], nc));
146                 }
147             } catch (Exception JavaDoc e) {
148                 e.printStackTrace();
149             }
150
151             explored = true;
152         }
153     }
154     
155     private void removeAllBindings() {
156         int cnt = getChildCount() - 1;
157         for (int i = 0; i < cnt; i++) {
158             removeBinding(i);
159         }
160     }
161
162     public void removeBinding(int n) {
163         NameNode child = (NameNode)getChildAt(n);
164         child.removeBinding();
165     }
166
167     public void removeBinding() {
168         try {
169             NamingContext ctx = ((NameNode)getParent()).getNamingContext();
170             ctx.unbind(binding.binding_name);
171             removeFromParent();
172         } catch (Exception JavaDoc e) {
173             e.printStackTrace();
174         }
175     }
176
177     private void printPath(NameComponent[] nc) {
178         System.out.print("path:");
179         for (int i = 0; i < nc.length; i++) {
180             System.out.print(nc[i].id);
181         }
182     }
183 }
184
Popular Tags