KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > naming > namemanager > ContextNode


1 package org.jacorb.naming.namemanager;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1997-2004 Gerald Brose.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */

22
23 import org.omg.CosNaming.*;
24 import org.jacorb.naming.*;
25 import org.jacorb.orb.*;
26 import org.jacorb.orb.iiop.*;
27
28 import java.util.*;
29 import javax.swing.tree.*;
30
31 /**
32  * @author Gerald Brose, FU Berlin
33  * @version $Id: ContextNode.java,v 1.14 2004/05/06 12:39:59 nicolas Exp $
34  */

35
36 public class ContextNode
37 {
38     protected Binding[] contents;
39     public boolean matched;
40     public boolean used;
41
42     public NamingContextExt context;
43     private DefaultMutableTreeNode myDefaultNode;
44     private DefaultTreeModel model;
45     private Binding binding;
46     private Vector bindingData;
47     private String JavaDoc myName;
48
49     public ContextNode( NamingContextExt context, DefaultTreeModel model )
50     {
51     used = false;
52     this.model = model;
53     this.context = context;
54     }
55
56     public ContextNode( NamingContextExt context,
57                         Binding b,
58                         DefaultTreeModel model)
59     {
60     used = false;
61     this.model = model;
62     this.context = context;
63     binding = b;
64     }
65
66     /**
67      *
68      */

69
70     public void display()
71     {
72     update();
73     if( bindingData != null )
74         NSTree.nsTable.setData( bindingData, this );
75     }
76
77     public boolean equals(ContextNode bnode)
78     {
79     return toString().equals(bnode.toString());
80     }
81
82     public NameComponent[] getName()
83     {
84     return binding.binding_name;
85     }
86
87     /**
88      *
89      * @param node javax.swing.tree.DefaultMutableTreeNode
90      */

91
92     public void setNode(DefaultMutableTreeNode node)
93     {
94     this.myDefaultNode = node;
95     }
96     
97
98     public String JavaDoc toString()
99     {
100     if( binding == null )
101         return "RootContext";
102     else
103     {
104             if (myName == null )
105             {
106                 NameComponent[] name = binding.binding_name;
107                 String JavaDoc kind = name[name.length-1].kind;
108                 myName = name[name.length-1].id +
109                     ( kind != null && kind.length() > 0 ? "." + kind : "");
110             }
111             return myName;
112     }
113     }
114
115     /**
116      *
117      * @param name java.lang.String
118      */

119
120     public void unbind(NameComponent [] nc)
121     throws org.omg.CosNaming.NamingContextPackage.NotFound JavaDoc,
122     org.omg.CosNaming.NamingContextPackage.CannotProceed JavaDoc,
123     org.omg.CosNaming.NamingContextPackage.InvalidName JavaDoc
124     {
125     context.unbind(nc);
126     }
127
128     /**
129      * update the content of this node and all its children
130      */

131
132     public synchronized void update()
133     {
134     try
135     {
136         BindingListHolder blsoh = new BindingListHolder();
137         BindingIteratorHolder bioh = new BindingIteratorHolder();
138         ContextNode context_node;
139     
140         context.list( NSTree.MAX_BIND, blsoh, bioh);
141         Binding[] bindings = blsoh.value;
142     
143         int childCount = myDefaultNode.getChildCount();
144     
145             // set up lists of object bindings and subcontext bindings
146

147         int context_count = 0;
148         int object_count = 0;
149                 
150         for( int i = 0; i < bindings.length; i++ )
151         {
152         if( bindings[i].binding_type == BindingType.ncontext )
153             context_count++;
154         else
155             object_count++;
156         }
157     
158         ContextNode[] contexts = new ContextNode[context_count];
159         Binding[] objects = new Binding[object_count];
160     
161         for( int i = 0; i < bindings.length; i++ )
162         {
163         if( bindings[i].binding_type == BindingType.ncontext)
164             contexts[ --context_count ] =
165             new ContextNode( NamingContextExtHelper.narrow( context.resolve(bindings[i].binding_name)), bindings[i], model );
166         else
167             objects[ --object_count ] = bindings[i];
168         }
169                 
170             // Compare this node's sub contexts and mark those found
171
// in the list of context bindings as used
172

173         for( int i = 0; i < childCount; i++ )
174         {
175         DefaultMutableTreeNode dmtn =
176                     (DefaultMutableTreeNode)myDefaultNode.getChildAt(i);
177         context_node = (ContextNode)dmtn.getUserObject();
178         for (int j = 0; j < contexts.length; j++)
179         {
180             if (context_node.equals(contexts[j]))
181             {
182             context_node.matched = true;
183             contexts[j].matched = true;
184             }
185         }
186         }
187     
188             // Delete those child nodes that were not found in the
189
// list
190

191         Vector removeList = new Vector();
192         for (int i = 0; i < childCount; i++)
193         {
194         DefaultMutableTreeNode node =
195                     (DefaultMutableTreeNode)myDefaultNode.getChildAt(i);
196         context_node = (ContextNode)node.getUserObject();
197         if (!context_node.matched)
198         {
199             removeList.addElement(node);
200         }
201         else
202             context_node.matched=false;
203         }
204     
205         int rsize = removeList.size();
206         for(int i = 0; i < rsize; i++)
207         {
208         model.removeNodeFromParent((DefaultMutableTreeNode)removeList.elementAt(i));
209         }
210     
211             bindingData = new Vector();
212
213             // Insert new context nodes found in the list as
214
// children of this tree node
215

216         for (int i = 0; i < contexts.length; i++)
217         {
218         if (!contexts[i].matched)
219         {
220             contexts[i].used=true;
221     
222             DefaultMutableTreeNode node = new DefaultMutableTreeNode();
223     
224             // tree node and context node need to know each other:
225
contexts[i].setNode(node);
226             node.setUserObject(contexts[i]);
227             node.setAllowsChildren(true);
228             model.insertNodeInto(node,myDefaultNode,0);
229         }
230         NameComponent last =
231                     contexts[i].binding.binding_name[ contexts[i].binding.binding_name.length-1];
232         NameComponent[] ncs = {last};
233
234         org.jacorb.orb.ParsedIOR pior = null;
235         try
236         {
237             pior =
238                         ((org.jacorb.orb.Delegate)((org.omg.CORBA.portable.ObjectImpl JavaDoc)context.resolve(
239                                            ncs ))._get_delegate()).getParsedIOR();
240         }
241         catch( org.omg.CosNaming.NamingContextPackage.NotFound JavaDoc nf )
242         {
243             // the named object could have disappeared from the
244
// naming context in the meantime. If it has, we simply
245
// continue
246
continue;
247         }
248         Vector row = new Vector();
249
250         row.addElement( last.id );
251         row.addElement( last.kind);
252         row.addElement( pior.getTypeId() );
253         IIOPProfile p = (IIOPProfile)pior.getEffectiveProfile();
254         row.addElement( p.getAddress().getIP() );
255                     
256         bindingData.addElement( row );
257
258         }
259     
260     
261         for( int i = 0; i < objects.length; i++ )
262         {
263         NameComponent last =
264                     objects[i].binding_name[ objects[i].binding_name.length-1];
265         NameComponent[] ncs = {last};
266         org.jacorb.orb.ParsedIOR pior = null;
267         try
268         {
269             pior =
270                         ((org.jacorb.orb.Delegate)((org.omg.CORBA.portable.ObjectImpl JavaDoc)context.resolve(
271                                            ncs ))._get_delegate()).getParsedIOR();
272         }
273         catch( org.omg.CosNaming.NamingContextPackage.NotFound JavaDoc nf )
274         {
275             // the named object could have disappeared from the
276
// naming context in the meantime. If it has, we simply
277
// continue
278
continue;
279         }
280         Vector row = new Vector();
281
282         row.addElement( last.id );
283         row.addElement( last.kind);
284         row.addElement( pior.getTypeId() );
285         IIOPProfile p = (IIOPProfile)pior.getEffectiveProfile();
286         row.addElement( p.getAddress().getIP() );
287                     
288         bindingData.addElement( row );
289         }
290     
291             // recursively update child nodes
292

293         childCount = myDefaultNode.getChildCount();
294         for (int i = 0; i < childCount; i++ )
295         {
296         DefaultMutableTreeNode dmtn =
297                     (DefaultMutableTreeNode)myDefaultNode.getChildAt(i);
298         context_node = (ContextNode)dmtn.getUserObject();
299                 // Name name = new Name(bindings[i].binding_name);
300
context_node.update();
301         }
302     }
303     catch (Exception JavaDoc e)
304     {
305         e.printStackTrace();
306     }
307     }
308 }
309
310
Popular Tags