KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > naming > core > NamingContextEnumeration


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

16
17 package org.apache.naming.core;
18
19
20 import java.util.Enumeration;
21
22 import javax.naming.Binding;
23 import javax.naming.Context;
24 import javax.naming.NameClassPair;
25 import javax.naming.NamingEnumeration;
26 import javax.naming.NamingException;
27
28 /**
29  * Naming enumeration implementation.
30  *
31  * TODO: implement 'throw exceptions on close' part of the spec.
32  * TODO: implement recycling ( for example on close )
33  *
34  * @author Remy Maucherat
35  * @author Costin Manolache
36  */

37 public class NamingContextEnumeration
38     implements NamingEnumeration
39 {
40     /** Constructor.
41      *
42      * @param enum base enumeration. Elements can be Strings, NameClassPair,
43      * Bindings or Entries, we'll provide the wrapping if needed. For String
44      * the Class and value will be lazy-loaded.
45      *
46      * @param ctx The context where this enum belongs. Used to lazy-eval
47      * the class and value
48      *
49      * @param bindings If true, we'll wrap things as Binding ( true for
50      * listBindings, false for list ).
51      */

52     public NamingContextEnumeration( Enumeration enum, Context ctx,
53                                      boolean bindings )
54     {
55         this.ctx = ctx;
56         this.bindings=bindings;
57         this.enum = enum;
58     }
59
60     // -------------------------------------------------------------- Variables
61

62     // return bindings instead of NameClassPair
63
protected boolean bindings;
64     /**
65      * Underlying enumeration.
66      */

67     protected Enumeration enum;
68
69     protected Context ctx;
70
71     // --------------------------------------------------------- Public Methods
72

73
74     /**
75      * Retrieves the next element in the enumeration.
76      */

77     public Object next()
78         throws NamingException
79     {
80         return nextElement();
81     }
82
83
84     /**
85      * Determines whether there are any more elements in the enumeration.
86      */

87     public boolean hasMore()
88         throws NamingException
89     {
90         return enum.hasMoreElements();
91     }
92
93
94     /**
95      * Closes this enumeration.
96      */

97     public void close()
98         throws NamingException
99     {
100         // XXX all exceptions should be thrown on close ( AFAIK )
101
}
102
103
104     public boolean hasMoreElements() {
105         return enum.hasMoreElements();
106     }
107
108     public Object nextElement() {
109         Object next=enum.nextElement();
110         if( next instanceof NamingEntry ) {
111             NamingEntry entry = (NamingEntry) next;
112             return new ServerBinding(entry.name, ctx, true);
113         } else if( next instanceof NameClassPair ) {
114             NameClassPair ncp=(NameClassPair)next;
115             if( bindings )
116                 return new ServerBinding(ncp.getName(), ctx, true);
117             return next;
118         } else if( next instanceof Binding ) {
119             return next;
120         } else if( next instanceof String ) {
121             String name=(String)next;
122             return new ServerBinding( name, ctx, true );
123         }
124         return null;
125     }
126
127 }
128
129
Popular Tags