View Javadoc

1   /*
2    * org.osjava.naming.ContextBindings
3    * 
4    * $Id: ContextBindings.java 1983 2005-09-03 14:03:38Z rzigweid $
5    * $Rev: 1983 $
6    * $Date: 2005-09-03 07:03:38 -0700 (Sat, 03 Sep 2005) $
7    * $Author: rzigweid $
8    * 
9    * Created on Apr 20, 2004
10   * Copyright (c) 2004, Robert M. Zigweid
11   * All rights reserved.
12   * 
13   * Redistribution and use in source and binary forms, with or without 
14   * modification, are permitted provided that the following conditions are met:
15   * 
16   * + Redistributions of source code must retain the above copyright notice, 
17   *   this list of conditions and the following disclaimer. 
18   * 
19   * + Redistributions in binary form must reproduce the above copyright notice,
20   *   this list of conditions and the following disclaimer in the documentation 
21   *   and/or other materials provided with the distribution. 
22   * 
23   * + Neither the name of the Simple-JNDI nor the bindings of its contributors may
24   *   be used to endorse or promote products derived from this software without 
25   *   specific prior written permission.
26   * 
27   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
29   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
30   * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
31   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
32   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
33   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
34   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
35   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
36   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
37   * POSSIBILITY OF SUCH DAMAGE.  
38   */
39  package org.osjava.sj.jndi;
40  
41  import java.util.Iterator;
42  import java.util.Map;
43  
44  import javax.naming.Binding;
45  import javax.naming.NamingEnumeration;
46  import javax.naming.NamingException;
47  
48  /***
49   * This class represents a NamingEnumeration of the bindings of a Context. 
50   * Originally authored by Henri Yandell and modified to make more flexable with other 
51   * Context implementations.
52   * 
53   * @author Robert M. Zigweid and Henri Yandell
54   * @version $Rev: 1983 $ $Date: 2005-09-03 07:03:38 -0700 (Sat, 03 Sep 2005) $
55   */
56  public class ContextBindings implements NamingEnumeration {
57      
58      /***
59       * A Map of the bindings of a Context.
60       */
61      private Map bindings = null;
62      
63      /***
64       * The iterator utilized in the Enumeration
65       */
66      private Iterator iterator = null;
67  
68      /***
69       * Creates a ContextBindings object based upon an a Map of names and the objects 
70       * the names are bound to.  If <code>table</code> is modified after instantiation 
71       * of ContextBindings, behavior is undefined and should be considered invalid.
72       * 
73       * @param table The table upon which the ContextBindings is based.
74       */
75      public ContextBindings(Map table) {
76          bindings = table;
77          iterator = bindings.keySet().iterator();
78      }
79  
80      /***
81       * Returns <code>true</code> if there are more elements available, otherwise
82       * <code>false</code>.
83       * 
84       * @return <code>true</code> if there are more elements available, otherwise <code>
85       *         false</code>
86       */
87      public boolean hasMoreElements() {
88          return iterator.hasNext();
89      }
90  
91      /***
92       * Returns <code>true</code> if there are more elements available, otherwise
93       * <code>false</code>.
94       * 
95       * @return <code>true</code> if there are more elements available, otherwise <code>
96       *         false</code>
97       * @throws NamingException if a naming exception is encountered
98       */
99      public boolean hasMore() throws NamingException {
100         if(bindings == null) {
101             throw new NamingException();
102         }
103         return hasMoreElements();
104     }
105 
106     /***
107      * Returns a {@link Binding Binding} created from the next available name.
108      * 
109      * @return a Binding representing the binding of the name and the object bound to the
110      *         name
111      */
112     public Object nextElement() {
113         if(bindings == null) { 
114             return null;
115         }
116         Object name = iterator.next();
117         /* What comes out of the iterator should be a CompoundName */
118         return new Binding(name.toString(), bindings.get(name));
119     }
120 
121     /***
122      * Returns a {@link Binding Binding} created from the next available name.
123      * 
124      * @return a Binding representing the binding of the name and the object bound to the
125      *         name
126      * @throws NamingException if a naming exception occurs
127      */
128     public Object next() throws NamingException {
129         if(bindings == null) {
130             throw new NamingException();
131         }
132         return nextElement();
133     }
134 
135     /***
136      * Close the ContextBindings instance, rendering it inoperable.
137      */
138     public void close() {
139         bindings = null;
140         iterator = null;
141     }
142 
143 }
144