001: package org.jacorb.naming.namemanager;
002:
003: /*
004: * JacORB - a free Java ORB
005: *
006: * Copyright (C) 1997-2004 Gerald Brose.
007: *
008: * This library is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Library General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * This library is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * Library General Public License for more details.
017: *
018: * You should have received a copy of the GNU Library General Public
019: * License along with this library; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: */
022:
023: import org.omg.CosNaming.*;
024: import org.jacorb.orb.iiop.*;
025:
026: import java.util.*;
027: import javax.swing.tree.*;
028:
029: /**
030: * @author Gerald Brose, FU Berlin
031: * @version $Id: ContextNode.java,v 1.16 2005/11/13 20:45:33 alphonse.bendt Exp $
032: */
033:
034: public class ContextNode {
035: protected Binding[] contents;
036: public boolean matched;
037: public boolean used;
038:
039: public NamingContextExt context;
040: private DefaultMutableTreeNode myDefaultNode;
041: private DefaultTreeModel model;
042: private Binding binding;
043: private Vector bindingData;
044: private String myName;
045:
046: public ContextNode(NamingContextExt context, DefaultTreeModel model) {
047: used = false;
048: this .model = model;
049: this .context = context;
050: }
051:
052: public ContextNode(NamingContextExt context, Binding b,
053: DefaultTreeModel model) {
054: used = false;
055: this .model = model;
056: this .context = context;
057: binding = b;
058: }
059:
060: /**
061: *
062: */
063:
064: public void display() {
065: update();
066: if (bindingData != null)
067: NSTree.nsTable.setData(bindingData, this );
068: }
069:
070: public boolean equals(ContextNode bnode) {
071: return toString().equals(bnode.toString());
072: }
073:
074: public NameComponent[] getName() {
075: return binding.binding_name;
076: }
077:
078: /**
079: *
080: * @param node javax.swing.tree.DefaultMutableTreeNode
081: */
082:
083: public void setNode(DefaultMutableTreeNode node) {
084: this .myDefaultNode = node;
085: }
086:
087: public String toString() {
088: if (binding == null) {
089: return "RootContext";
090: }
091:
092: if (myName == null) {
093: NameComponent[] name = binding.binding_name;
094: String kind = name[name.length - 1].kind;
095: myName = name[name.length - 1].id
096: + (kind != null && kind.length() > 0 ? "." + kind
097: : "");
098: }
099: return myName;
100: }
101:
102: public void unbind(NameComponent[] nc)
103: throws org.omg.CosNaming.NamingContextPackage.NotFound,
104: org.omg.CosNaming.NamingContextPackage.CannotProceed,
105: org.omg.CosNaming.NamingContextPackage.InvalidName {
106: context.unbind(nc);
107: }
108:
109: /**
110: * update the content of this node and all its children
111: */
112:
113: public synchronized void update() {
114: try {
115: BindingListHolder blsoh = new BindingListHolder();
116: BindingIteratorHolder bioh = new BindingIteratorHolder();
117: ContextNode context_node;
118:
119: context.list(NSTree.MAX_BIND, blsoh, bioh);
120: Binding[] bindings = blsoh.value;
121:
122: int childCount = myDefaultNode.getChildCount();
123:
124: // set up lists of object bindings and subcontext bindings
125:
126: int context_count = 0;
127: int object_count = 0;
128:
129: for (int i = 0; i < bindings.length; i++) {
130: if (bindings[i].binding_type == BindingType.ncontext)
131: context_count++;
132: else
133: object_count++;
134: }
135:
136: ContextNode[] contexts = new ContextNode[context_count];
137: Binding[] objects = new Binding[object_count];
138:
139: for (int i = 0; i < bindings.length; i++) {
140: if (bindings[i].binding_type == BindingType.ncontext)
141: contexts[--context_count] = new ContextNode(
142: NamingContextExtHelper.narrow(context
143: .resolve(bindings[i].binding_name)),
144: bindings[i], model);
145: else
146: objects[--object_count] = bindings[i];
147: }
148:
149: // Compare this node's sub contexts and mark those found
150: // in the list of context bindings as used
151:
152: for (int i = 0; i < childCount; i++) {
153: DefaultMutableTreeNode dmtn = (DefaultMutableTreeNode) myDefaultNode
154: .getChildAt(i);
155: context_node = (ContextNode) dmtn.getUserObject();
156: for (int j = 0; j < contexts.length; j++) {
157: if (context_node.equals(contexts[j])) {
158: context_node.matched = true;
159: contexts[j].matched = true;
160: }
161: }
162: }
163:
164: // Delete those child nodes that were not found in the
165: // list
166:
167: Vector removeList = new Vector();
168: for (int i = 0; i < childCount; i++) {
169: DefaultMutableTreeNode node = (DefaultMutableTreeNode) myDefaultNode
170: .getChildAt(i);
171: context_node = (ContextNode) node.getUserObject();
172: if (!context_node.matched) {
173: removeList.addElement(node);
174: } else
175: context_node.matched = false;
176: }
177:
178: int rsize = removeList.size();
179: for (int i = 0; i < rsize; i++) {
180: model
181: .removeNodeFromParent((DefaultMutableTreeNode) removeList
182: .elementAt(i));
183: }
184:
185: bindingData = new Vector();
186:
187: // Insert new context nodes found in the list as
188: // children of this tree node
189:
190: for (int i = 0; i < contexts.length; i++) {
191: if (!contexts[i].matched) {
192: contexts[i].used = true;
193:
194: DefaultMutableTreeNode node = new DefaultMutableTreeNode();
195:
196: // tree node and context node need to know each other:
197: contexts[i].setNode(node);
198: node.setUserObject(contexts[i]);
199: node.setAllowsChildren(true);
200: model.insertNodeInto(node, myDefaultNode, 0);
201: }
202: NameComponent last = contexts[i].binding.binding_name[contexts[i].binding.binding_name.length - 1];
203: NameComponent[] ncs = { last };
204:
205: org.jacorb.orb.ParsedIOR pior = null;
206: try {
207: pior = ((org.jacorb.orb.Delegate) ((org.omg.CORBA.portable.ObjectImpl) context
208: .resolve(ncs))._get_delegate())
209: .getParsedIOR();
210: } catch (org.omg.CosNaming.NamingContextPackage.NotFound nf) {
211: // the named object could have disappeared from the
212: // naming context in the meantime. If it has, we simply
213: // continue
214: continue;
215: }
216: Vector row = createRow(last, pior);
217: bindingData.addElement(row);
218:
219: }
220:
221: for (int i = 0; i < objects.length; i++) {
222: NameComponent last = objects[i].binding_name[objects[i].binding_name.length - 1];
223: NameComponent[] ncs = { last };
224: org.jacorb.orb.ParsedIOR pior = null;
225: try {
226: pior = ((org.jacorb.orb.Delegate) ((org.omg.CORBA.portable.ObjectImpl) context
227: .resolve(ncs))._get_delegate())
228: .getParsedIOR();
229: } catch (org.omg.CosNaming.NamingContextPackage.NotFound nf) {
230: // the named object could have disappeared from the
231: // naming context in the meantime. If it has, we simply
232: // continue
233: continue;
234: }
235: Vector row = createRow(last, pior);
236:
237: bindingData.addElement(row);
238: }
239:
240: // recursively update child nodes
241:
242: childCount = myDefaultNode.getChildCount();
243: for (int i = 0; i < childCount; i++) {
244: DefaultMutableTreeNode dmtn = (DefaultMutableTreeNode) myDefaultNode
245: .getChildAt(i);
246: context_node = (ContextNode) dmtn.getUserObject();
247: // Name name = new Name(bindings[i].binding_name);
248: context_node.update();
249: }
250: } catch (Exception e) {
251: e.printStackTrace();
252: }
253: }
254:
255: private Vector createRow(NameComponent last,
256: org.jacorb.orb.ParsedIOR pior) {
257: Vector row = new Vector();
258:
259: row.addElement(last.id);
260: row.addElement(last.kind);
261: row.addElement(pior.getTypeId());
262: IIOPProfile p = (IIOPProfile) pior.getEffectiveProfile();
263: final IIOPAddress iiopAddress = (IIOPAddress) p.getAddress();
264: row.addElement(iiopAddress.getIP());
265: row.addElement(Integer.toString(iiopAddress.getPort()));
266: return row;
267: }
268: }
|