001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.naming;
019:
020: import java.util.Iterator;
021:
022: import javax.naming.Binding;
023: import javax.naming.CompositeName;
024: import javax.naming.Context;
025: import javax.naming.NamingEnumeration;
026: import javax.naming.NamingException;
027:
028: /**
029: * Naming enumeration implementation.
030: *
031: * @author Remy Maucherat
032: * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
033: */
034:
035: public class NamingContextBindingsEnumeration implements
036: NamingEnumeration {
037:
038: // ----------------------------------------------------------- Constructors
039:
040: public NamingContextBindingsEnumeration(Iterator entries,
041: Context ctx) {
042: iterator = entries;
043: this .ctx = ctx;
044: }
045:
046: // -------------------------------------------------------------- Variables
047:
048: /**
049: * Underlying enumeration.
050: */
051: protected Iterator iterator;
052:
053: /**
054: * The context for which this enumeration is being generated.
055: */
056: private Context ctx;
057:
058: // --------------------------------------------------------- Public Methods
059:
060: /**
061: * Retrieves the next element in the enumeration.
062: */
063: public Object next() throws NamingException {
064: return nextElementInternal();
065: }
066:
067: /**
068: * Determines whether there are any more elements in the enumeration.
069: */
070: public boolean hasMore() throws NamingException {
071: return iterator.hasNext();
072: }
073:
074: /**
075: * Closes this enumeration.
076: */
077: public void close() throws NamingException {
078: }
079:
080: public boolean hasMoreElements() {
081: return iterator.hasNext();
082: }
083:
084: public Object nextElement() {
085: try {
086: return nextElementInternal();
087: } catch (NamingException e) {
088: throw new RuntimeException(e.getMessage(), e);
089: }
090: }
091:
092: private Object nextElementInternal() throws NamingException {
093: NamingEntry entry = (NamingEntry) iterator.next();
094:
095: // If the entry is a reference, resolve it
096: if (entry.type == NamingEntry.REFERENCE
097: || entry.type == NamingEntry.LINK_REF) {
098: try {
099: // A lookup will resolve the entry
100: ctx.lookup(new CompositeName(entry.name));
101: } catch (NamingException e) {
102: throw e;
103: } catch (Exception e) {
104: NamingException ne = new NamingException(e.getMessage());
105: ne.initCause(e);
106: throw ne;
107: }
108: }
109:
110: return new Binding(entry.name,
111: entry.value.getClass().getName(), entry.value, true);
112: }
113:
114: }
|