01: /*
02: * Copyright 2003-2006 Rick Knowles <winstone-devel at lists sourceforge net>
03: * Distributed under the terms of either:
04: * - the common development and distribution license (CDDL), v1.0; or
05: * - the GNU Lesser General Public License, v2.1 or later
06: */
07: package winstone.jndi;
08:
09: import java.util.Arrays;
10: import java.util.Enumeration;
11: import java.util.Map;
12: import java.util.Vector;
13:
14: import javax.naming.NameClassPair;
15: import javax.naming.NamingEnumeration;
16: import javax.naming.NamingException;
17:
18: /**
19: * Enumeration across the names/classes of the bindings in a particular context.
20: * Used by the list() method.
21: *
22: * @author <a href="mailto:rick_knowles@hotmail.com">Rick Knowles</a>
23: * @version $Id: WinstoneNameEnumeration.java,v 1.3 2006/02/28 07:32:48 rickknowles Exp $
24: */
25: public class WinstoneNameEnumeration implements NamingEnumeration {
26: private Enumeration nameEnumeration;
27:
28: /**
29: * Constructor
30: */
31: public WinstoneNameEnumeration(Map bindings) {
32: Object keys[] = bindings.keySet().toArray();
33: Arrays.sort(keys);
34: Vector nameClassPairs = new Vector();
35: for (int n = 0; n < keys.length; n++)
36: nameClassPairs.add(new NameClassPair((String) keys[n],
37: bindings.get(keys[n]).getClass().getName()));
38: this .nameEnumeration = nameClassPairs.elements();
39: }
40:
41: public void close() throws NamingException {
42: this .nameEnumeration = null;
43: }
44:
45: public boolean hasMore() throws NamingException {
46: if (this .nameEnumeration == null)
47: throw new NamingException(
48: ContainerJNDIManager.JNDI_RESOURCES
49: .getString("WinstoneNameEnumeration.AlreadyClosed"));
50: else
51: return this .nameEnumeration.hasMoreElements();
52: }
53:
54: public Object next() throws NamingException {
55: if (hasMore())
56: return this .nameEnumeration.nextElement();
57: else
58: return null;
59: }
60:
61: public boolean hasMoreElements() {
62: try {
63: return hasMore();
64: } catch (NamingException err) {
65: return false;
66: }
67: }
68:
69: public Object nextElement() {
70: try {
71: return next();
72: } catch (NamingException err) {
73: return null;
74: }
75: }
76:
77: }
|