001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: NamingEnumerationImpl.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.naming.context;
025:
026: import java.util.Enumeration;
027: import java.util.Hashtable;
028: import java.util.NoSuchElementException;
029:
030: import javax.naming.NameClassPair;
031: import javax.naming.NamingEnumeration;
032: import javax.naming.NamingException;
033:
034: /**
035: * Implementation of the NamingEnumeration for list operations Each element is
036: * of type NameClassPair.
037: * @author Florent Benoit
038: */
039: public class NamingEnumerationImpl implements
040: NamingEnumeration<NameClassPair> {
041:
042: /**
043: * list of names.
044: */
045: private Enumeration names;
046:
047: /**
048: * List of bindings.
049: */
050: private Hashtable bindings;
051:
052: /**
053: * Constructor. Called by list()
054: * @param bindings list of bindings
055: */
056: NamingEnumerationImpl(final Hashtable bindings) {
057: this .bindings = bindings;
058: this .names = bindings.keys();
059: }
060:
061: /**
062: * Determines whether there are any more elements in the enumeration.
063: * @return true if there is more in the enumeration ; false otherwise.
064: * @throws NamingException If a naming exception is encountered while
065: * attempting to determine whether there is another element in the
066: * enumeration.
067: */
068: public boolean hasMore() throws NamingException {
069: return names.hasMoreElements();
070: }
071:
072: /**
073: * Retrieves the next element in the enumeration.
074: * @return The possibly null element in the enumeration. null is only valid
075: * for enumerations that can return null (e.g. Attribute.getAll()
076: * returns an enumeration of attribute values, and an attribute
077: * value can be null).
078: * @throws NamingException If a naming exception is encountered while
079: * attempting to retrieve the next element. See NamingException and
080: * its subclasses for the possible naming exceptions.
081: */
082: public NameClassPair next() throws NamingException {
083: String name = (String) names.nextElement();
084: String className = bindings.get(name).getClass().getName();
085: return new NameClassPair(name, className);
086: }
087:
088: /**
089: * Closes this enumeration.
090: */
091: public void close() {
092: }
093:
094: /**
095: * Returns the next element of this enumeration if this enumeration object
096: * has at least one more element to provide.
097: * @return the next element of this enumeration.
098: */
099: public NameClassPair nextElement() {
100: try {
101: return next();
102: } catch (NamingException e) {
103: throw new NoSuchElementException(e.toString());
104: }
105: }
106:
107: /**
108: * Tests if this enumeration contains more elements.
109: * @return <code>true</code> if and only if this enumeration object
110: * contains at least one more element to provide; <code>false</code>
111: * otherwise.
112: */
113: public boolean hasMoreElements() {
114: try {
115: return hasMore();
116: } catch (NamingException e) {
117: return false;
118: }
119: }
120:
121: }
|