001: /*
002: * Copyright 2003-2006 Rick Knowles <winstone-devel at lists sourceforge net>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: */
007: package winstone.jndi;
008:
009: import java.util.Arrays;
010: import java.util.Enumeration;
011: import java.util.Hashtable;
012: import java.util.Vector;
013:
014: import javax.naming.Binding;
015: import javax.naming.CompositeName;
016: import javax.naming.Context;
017: import javax.naming.NamingEnumeration;
018: import javax.naming.NamingException;
019: import javax.naming.spi.NamingManager;
020:
021: /**
022: * Enumeration over the set of bindings for this context.
023: *
024: * @author <a href="mailto:rick_knowles@hotmail.com">Rick Knowles</a>
025: * @version $Id: WinstoneBindingEnumeration.java,v 1.3 2006/02/28 07:32:48 rickknowles Exp $
026: */
027: public class WinstoneBindingEnumeration implements NamingEnumeration {
028: private Enumeration nameEnumeration;
029: private Hashtable bindings;
030: private Hashtable contextEnvironment;
031: private Context context;
032:
033: /**
034: * Constructor - sets up the enumeration ready for retrieving bindings
035: * instead of NameClassPairs.
036: *
037: * @param bindings
038: * The source binding set
039: */
040: public WinstoneBindingEnumeration(Hashtable bindings,
041: Hashtable environment, Context context) {
042: Object keys[] = bindings.keySet().toArray();
043: Arrays.sort(keys);
044: Vector nameList = new Vector(Arrays.asList(keys));
045: this .nameEnumeration = nameList.elements();
046: this .bindings = (Hashtable) bindings.clone();
047: this .context = context;
048: this .contextEnvironment = environment;
049: }
050:
051: public Object next() throws NamingException {
052: if (this .nameEnumeration == null)
053: throw new NamingException(
054: ContainerJNDIManager.JNDI_RESOURCES
055: .getString("WinstoneBindingEnumeration.AlreadyClosed"));
056:
057: String name = (String) this .nameEnumeration.nextElement();
058: Object value = this .bindings.get(name);
059: try {
060: value = NamingManager.getObjectInstance(value,
061: new CompositeName().add(name), this .context,
062: this .contextEnvironment);
063: } catch (Throwable err) {
064: NamingException errNaming = new NamingException(
065: ContainerJNDIManager.JNDI_RESOURCES
066: .getString("WinstoneBindingEnumeration.FailedToGetInstance"));
067: errNaming.setRootCause(err);
068: throw errNaming;
069: }
070: return new Binding(name, value);
071: }
072:
073: public boolean hasMore() throws NamingException {
074: if (this .nameEnumeration == null)
075: throw new NamingException(
076: ContainerJNDIManager.JNDI_RESOURCES
077: .getString("WinstoneBindingEnumeration.AlreadyClosed"));
078: else
079: return this .nameEnumeration.hasMoreElements();
080: }
081:
082: public void close() throws NamingException {
083: this .nameEnumeration = null;
084: }
085:
086: public boolean hasMoreElements() {
087: try {
088: return hasMore();
089: } catch (NamingException err) {
090: return false;
091: }
092: }
093:
094: public Object nextElement() {
095: try {
096: return next();
097: } catch (NamingException err) {
098: return null;
099: }
100: }
101: }
|