KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > winstone > jndi > WinstoneBindingEnumeration


1 /*
2  * Copyright 2003-2006 Rick Knowles <winstone-devel at lists sourceforge net>
3  * Distributed under the terms of either:
4  * - the common development and distribution license (CDDL), v1.0; or
5  * - the GNU Lesser General Public License, v2.1 or later
6  */

7 package winstone.jndi;
8
9 import java.util.Arrays JavaDoc;
10 import java.util.Enumeration JavaDoc;
11 import java.util.Hashtable JavaDoc;
12 import java.util.Vector JavaDoc;
13
14 import javax.naming.Binding JavaDoc;
15 import javax.naming.CompositeName JavaDoc;
16 import javax.naming.Context JavaDoc;
17 import javax.naming.NamingEnumeration JavaDoc;
18 import javax.naming.NamingException JavaDoc;
19 import javax.naming.spi.NamingManager JavaDoc;
20
21 /**
22  * Enumeration over the set of bindings for this context.
23  *
24  * @author <a HREF="mailto:rick_knowles@hotmail.com">Rick Knowles</a>
25  * @version $Id: WinstoneBindingEnumeration.java,v 1.3 2006/02/28 07:32:48 rickknowles Exp $
26  */

27 public class WinstoneBindingEnumeration implements NamingEnumeration JavaDoc {
28     private Enumeration JavaDoc nameEnumeration;
29     private Hashtable JavaDoc bindings;
30     private Hashtable JavaDoc contextEnvironment;
31     private Context JavaDoc context;
32
33     /**
34      * Constructor - sets up the enumeration ready for retrieving bindings
35      * instead of NameClassPairs.
36      *
37      * @param bindings
38      * The source binding set
39      */

40     public WinstoneBindingEnumeration(Hashtable JavaDoc bindings,
41             Hashtable JavaDoc environment, Context JavaDoc context) {
42         Object JavaDoc keys[] = bindings.keySet().toArray();
43         Arrays.sort(keys);
44         Vector JavaDoc nameList = new Vector JavaDoc(Arrays.asList(keys));
45         this.nameEnumeration = nameList.elements();
46         this.bindings = (Hashtable JavaDoc) bindings.clone();
47         this.context = context;
48         this.contextEnvironment = environment;
49     }
50
51     public Object JavaDoc next() throws NamingException JavaDoc {
52         if (this.nameEnumeration == null)
53             throw new NamingException JavaDoc(ContainerJNDIManager.JNDI_RESOURCES
54                     .getString("WinstoneBindingEnumeration.AlreadyClosed"));
55
56         String JavaDoc name = (String JavaDoc) this.nameEnumeration.nextElement();
57         Object JavaDoc value = this.bindings.get(name);
58         try {
59             value = NamingManager.getObjectInstance(value, new CompositeName JavaDoc()
60                     .add(name), this.context, this.contextEnvironment);
61         } catch (Throwable JavaDoc err) {
62             NamingException JavaDoc errNaming = new NamingException JavaDoc(
63                     ContainerJNDIManager.JNDI_RESOURCES
64                             .getString("WinstoneBindingEnumeration.FailedToGetInstance"));
65             errNaming.setRootCause(err);
66             throw errNaming;
67         }
68         return new Binding JavaDoc(name, value);
69     }
70
71     public boolean hasMore() throws NamingException JavaDoc {
72         if (this.nameEnumeration == null)
73             throw new NamingException JavaDoc(ContainerJNDIManager.JNDI_RESOURCES
74                     .getString("WinstoneBindingEnumeration.AlreadyClosed"));
75         else
76             return this.nameEnumeration.hasMoreElements();
77     }
78
79     public void close() throws NamingException JavaDoc {
80         this.nameEnumeration = null;
81     }
82
83     public boolean hasMoreElements() {
84         try {
85             return hasMore();
86         } catch (NamingException JavaDoc err) {
87             return false;
88         }
89     }
90
91     public Object JavaDoc nextElement() {
92         try {
93             return next();
94         } catch (NamingException JavaDoc err) {
95             return null;
96         }
97     }
98 }
99
Popular Tags