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;
22
23 import javax.naming.Binding;
24 import javax.naming.CompositeName;
25 import javax.naming.Context;
26 import javax.naming.NamingEnumeration;
27 import javax.naming.NamingException;
28
29 /**
30 * Naming enumeration implementation.
31 *
32 * @author Remy Maucherat
33 */
34 public class NamingContextBindingsEnumeration
35 implements NamingEnumeration<Binding> {
36
37
38 // ----------------------------------------------------------- Constructors
39
40
41 public NamingContextBindingsEnumeration(Iterator<NamingEntry> entries,
42 Context ctx) {
43 iterator = entries;
44 this.ctx = ctx;
45 }
46
47 // -------------------------------------------------------------- Variables
48
49
50 /**
51 * Underlying enumeration.
52 */
53 protected final Iterator<NamingEntry> iterator;
54
55
56 /**
57 * The context for which this enumeration is being generated.
58 */
59 private final Context ctx;
60
61
62 // --------------------------------------------------------- Public Methods
63
64
65 /**
66 * Retrieves the next element in the enumeration.
67 */
68 @Override
69 public Binding next()
70 throws NamingException {
71 return nextElementInternal();
72 }
73
74
75 /**
76 * Determines whether there are any more elements in the enumeration.
77 */
78 @Override
79 public boolean hasMore()
80 throws NamingException {
81 return iterator.hasNext();
82 }
83
84
85 /**
86 * Closes this enumeration.
87 */
88 @Override
89 public void close()
90 throws NamingException {
91 }
92
93
94 @Override
95 public boolean hasMoreElements() {
96 return iterator.hasNext();
97 }
98
99
100 @Override
101 public Binding nextElement() {
102 try {
103 return nextElementInternal();
104 } catch (NamingException e) {
105 throw new RuntimeException(e.getMessage(), e);
106 }
107 }
108
109 private Binding nextElementInternal() throws NamingException {
110 NamingEntry entry = iterator.next();
111 Object value;
112
113 // If the entry is a reference, resolve it
114 if (entry.type == NamingEntry.REFERENCE
115 || entry.type == NamingEntry.LINK_REF) {
116 try {
117 value = ctx.lookup(new CompositeName(entry.name));
118 } catch (NamingException e) {
119 throw e;
120 } catch (Exception e) {
121 NamingException ne = new NamingException(e.getMessage());
122 ne.initCause(e);
123 throw ne;
124 }
125 } else {
126 value = entry.value;
127 }
128
129 return new Binding(entry.name, value.getClass().getName(), value, true);
130 }
131 }
132
133