001: /*
002:
003: * <copyright>
004: *
005: * Copyright 2002-2007 BBNT Solutions, LLC
006: * under sponsorship of the Defense Advanced Research Projects
007: * Agency (DARPA).
008: *
009: * You can redistribute this software and/or modify it under the
010: * terms of the Cougaar Open Source License as published on the
011: * Cougaar Open Source Website (www.cougaar.org).
012: *
013: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
014: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
015: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
016: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
017: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
018: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
019: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
020: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
021: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
022: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
023: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
024: *
025: * </copyright>
026:
027: */
028:
029: package org.cougaar.qos.qrs;
030:
031: import org.cougaar.util.log.Logger;
032: import org.omg.CosNaming.Binding;
033: import org.omg.CosNaming.BindingHolder;
034: import org.omg.CosNaming.BindingIterator;
035: import org.omg.CosNaming.BindingIteratorHolder;
036: import org.omg.CosNaming.BindingListHolder;
037: import org.omg.CosNaming.BindingType;
038: import org.omg.CosNaming.NameComponent;
039: import org.omg.CosNaming.NamingContext;
040: import org.omg.CosNaming.NamingContextHelper;
041:
042: import java.util.Iterator;
043:
044: public class NSIterator implements Iterator<Object> {
045: Logger logger;
046: NameComponent[] context_name;
047: Binding[] list;
048: BindingIterator itr;
049: BindingHolder binding_holder = new BindingHolder();
050: NamingContext context;
051: BindingListHolder list_holder = new BindingListHolder();
052: BindingIteratorHolder itr_holder = new BindingIteratorHolder();
053: int list_index;
054: org.omg.CORBA.Object next;
055: String current_name, next_name;
056:
057: public static class NameServerException extends Exception {
058: NameServerException(Exception cause) {
059: super (cause);
060: }
061: }
062:
063: public NSIterator(NameComponent[] context_name)
064: throws NameServerException {
065: logger = Logging.getLogger(CorbaUtils.class);
066: this .context_name = new NameComponent[context_name.length];
067: System.arraycopy(context_name, 0, this .context_name, 0,
068: context_name.length);
069: try {
070: org.omg.CORBA.Object context_ref = CorbaUtils
071: .nsResolve(context_name);
072: context = NamingContextHelper.narrow(context_ref);
073: } catch (Exception ex) {
074: throw new NameServerException(ex);
075: }
076: initialize();
077: }
078:
079: public void initialize() {
080: context.list(50, list_holder, itr_holder);
081: list = list_holder.value;
082: itr = itr_holder.value;
083: list_index = 0;
084: advance();
085: }
086:
087: private boolean computeNext(Binding binding) {
088: boolean success = false;
089: if (binding.binding_type.value() == BindingType._nobject) {
090: NameComponent[] name = binding.binding_name;
091: NameComponent leaf = name[name.length - 1];
092: try {
093: next = context.resolve(name);
094: next_name = leaf.id;
095: success = true;
096: } catch (Exception ex) {
097: logger.error(null, ex);
098: }
099: }
100: return success;
101: }
102:
103: private void advance() {
104: while (true) {
105: if (list_index < list.length) {
106: if (computeNext(list[list_index++])) {
107: break;
108: }
109: } else if (itr != null && itr.next_one(binding_holder)) {
110: if (computeNext(binding_holder.value)) {
111: break;
112: }
113: } else {
114: if (itr != null) {
115: itr.destroy();
116: }
117: next = null;
118: break;
119: }
120: }
121: }
122:
123: public void remove() {
124: throw new RuntimeException("remove() is illegal");
125: }
126:
127: public Object next() {
128: if (next != null) {
129: Object current = next;
130: current_name = next_name;
131: advance();
132: return current;
133: } else {
134: return null;
135: }
136: }
137:
138: public String getName() {
139: return current_name;
140: }
141:
142: public boolean hasNext() {
143: return next != null;
144: }
145:
146: }
|