KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > naming > NamingContextBindingsEnumeration


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18
19 package org.apache.naming;
20
21 import java.util.Iterator JavaDoc;
22
23 import javax.naming.Binding JavaDoc;
24 import javax.naming.CompositeName JavaDoc;
25 import javax.naming.Context JavaDoc;
26 import javax.naming.NamingEnumeration JavaDoc;
27 import javax.naming.NamingException JavaDoc;
28
29 /**
30  * Naming enumeration implementation.
31  *
32  * @author Remy Maucherat
33  * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
34  */

35
36 public class NamingContextBindingsEnumeration
37     implements NamingEnumeration JavaDoc {
38
39
40     // ----------------------------------------------------------- Constructors
41

42
43     public NamingContextBindingsEnumeration(Iterator JavaDoc entries, Context JavaDoc ctx) {
44         iterator = entries;
45         this.ctx = ctx;
46     }
47
48     // -------------------------------------------------------------- Variables
49

50
51     /**
52      * Underlying enumeration.
53      */

54     protected Iterator JavaDoc iterator;
55
56     
57     /**
58      * The context for which this enumeration is being generated.
59      */

60     private Context JavaDoc ctx;
61
62
63     // --------------------------------------------------------- Public Methods
64

65
66     /**
67      * Retrieves the next element in the enumeration.
68      */

69     public Object JavaDoc next()
70         throws NamingException JavaDoc {
71         return nextElementInternal();
72     }
73
74
75     /**
76      * Determines whether there are any more elements in the enumeration.
77      */

78     public boolean hasMore()
79         throws NamingException JavaDoc {
80         return iterator.hasNext();
81     }
82
83
84     /**
85      * Closes this enumeration.
86      */

87     public void close()
88         throws NamingException JavaDoc {
89     }
90
91
92     public boolean hasMoreElements() {
93         return iterator.hasNext();
94     }
95
96
97     public Object JavaDoc nextElement() {
98         try {
99             return nextElementInternal();
100         } catch (NamingException JavaDoc e) {
101             throw new RuntimeException JavaDoc(e.getMessage(), e);
102         }
103     }
104     
105     private Object JavaDoc nextElementInternal() throws NamingException JavaDoc {
106         NamingEntry entry = (NamingEntry) iterator.next();
107         
108         // If the entry is a reference, resolve it
109
if (entry.type == NamingEntry.REFERENCE
110                 || entry.type == NamingEntry.LINK_REF) {
111             try {
112                 // A lookup will resolve the entry
113
ctx.lookup(new CompositeName JavaDoc(entry.name));
114             } catch (NamingException JavaDoc e) {
115                 throw e;
116             } catch (Exception JavaDoc e) {
117                 NamingException JavaDoc ne = new NamingException JavaDoc(e.getMessage());
118                 ne.initCause(e);
119                 throw ne;
120             }
121         }
122         
123         return new Binding JavaDoc(entry.name, entry.value.getClass().getName(),
124                            entry.value, true);
125     }
126
127
128 }
129
130
Popular Tags