KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Enumeration JavaDoc;
20 import java.util.Hashtable JavaDoc;
21
22 import javax.naming.CompositeName JavaDoc;
23 import javax.naming.InvalidNameException JavaDoc;
24 import javax.naming.Name JavaDoc;
25 import javax.naming.NameParser JavaDoc;
26 import javax.naming.NamingException JavaDoc;
27 import javax.naming.OperationNotSupportedException JavaDoc;
28 import javax.naming.directory.Attributes JavaDoc;
29 import javax.naming.directory.DirContext JavaDoc;
30
31 import org.apache.tomcat.util.IntrospectionUtils;
32
33 // Based on a merge of various catalina naming contexts
34
// Name is used - it provide better oportunities for reuse and optimizations
35

36 /**
37  * This is the base class for our naming operations, for easy reading.
38  *
39  * <p>Creating a new context:
40  * <ul>
41  * <li>Create a new class, extending BaseContext or BaseDirContext ( second
42  * if you want to support attributes ).
43  * <li>Add setters for configuration. The setters will be called autmatically,
44  * like in ant, from the initial env settings.
45  * <li>Override methods that are defined in BaseNaming. Default behavior
46  * is provided for all.
47  * <li>If performance is a concern or have special behavior - override Context and
48  * DirContext methods. That shouldn't be needed in most cases.
49  * </ul>
50  *
51  * This class is designed to minimize the ammount of code that is required to
52  * create a new context. The usual DirContext interface has way too many methods,
53  * so implementation requires a lot of typing.
54  *
55  * Our contexts are mostly wrappers for files or in memory structures. That means
56  * some operations are cheaper, and we're far from the features that would be
57  * exposed for an LDAP or real Directory server.
58  *
59  * @author Remy Maucherat
60  * @author Costin Manolache
61  */

62 public class BaseNaming {
63
64     /**
65      * Builds a base directory context.
66      */

67     public BaseNaming() {
68         this.env=new Hashtable JavaDoc();
69     }
70
71     /**
72      * Builds a base directory context using the given environment.
73      */

74     public BaseNaming(Hashtable JavaDoc env) {
75         this.env=new Hashtable JavaDoc();
76         if (env != null ) {
77             Enumeration JavaDoc envEntries = env.keys();
78             while (envEntries.hasMoreElements()) {
79                 String JavaDoc entryName = (String JavaDoc) envEntries.nextElement();
80                 Object JavaDoc entryValue=env.get(entryName);
81                 this.env.put(entryName, entryValue);
82                 try {
83                     // XXX We need a mechanism to select properties for
84
// this task. Maybe all contexts should use as property prefix the
85
// class name ? Or base class name ?
86
IntrospectionUtils.setAttribute( this, entryName, entryValue );
87                 } catch(Exception JavaDoc ex ) {
88                     System.out.println("Unsuported property " + entryName + " " + ex.getMessage());
89                 }
90             }
91         }
92     }
93
94     // ----------------------------------------------------- Instance Variables
95

96     /**
97      * Environment. All context config info.
98      */

99     protected Hashtable JavaDoc env;
100
101     /**
102      * Default name parser for this context.
103      * XXX This should be combined with the Tomcat mapper, and
104      * optimized for MessageBytes.
105      */

106     protected final NameParser JavaDoc nameParser = new NameParserImpl();
107
108     /** Prefix used for URL-based namming lookup. It must be removed
109      * from all names.
110      * Deprecated ? Do we need it ?
111      */

112     protected String JavaDoc urlPrefix="";
113
114     // ------------------------------------------------------------- Properties
115
// Common properties, apply to all jtc naming contexts.
116

117     // XXX Introspection should be used to turn the Hashtable attributes
118
// into setters.
119
public void setURLPrefix( String JavaDoc s ) {
120         urlPrefix=s;
121     }
122
123     private boolean cached;
124     private int cacheTTL;
125     private int cacheObjectMaxSize;
126
127     public boolean isCached() {
128         return cached;
129     }
130
131     public void setCached(boolean cached) {
132         this.cached = cached;
133     }
134
135     public int getCacheTTL() {
136         return cacheTTL;
137     }
138
139     public void setCacheTTL(int cacheTTL) {
140         this.cacheTTL = cacheTTL;
141     }
142
143     public int getCacheObjectMaxSize() {
144         return cacheObjectMaxSize;
145     }
146
147     public void setCacheObjectMaxSize(int cacheObjectMaxSize) {
148         this.cacheObjectMaxSize = cacheObjectMaxSize;
149     }
150
151     // -------------------- Not so Abstract methods --------------------
152
// This is what a subclass should implement.
153

154     // XXX Base resolveLinks() method ?? And then use lookup without resolveLinks flag
155

156     /** The lookup method. This is the main method you should implement.
157      *
158      * @param name
159      * @param resolveLinks If false, this is a lookupLink call.
160      */

161     public Object JavaDoc lookup(Name JavaDoc name, boolean resolveLinks)
162         throws NamingException JavaDoc
163     {
164         throw new OperationNotSupportedException JavaDoc();
165     }
166
167     /** The setter method. Implement it if the context is read/write.
168      *
169      * @param name
170      * @param obj The object to be bound.
171      * @param attrs Attributes - if this is a dir context, null otherwise
172      * @param rebind What to do if the name is already bound.
173      * XXX can be further simplified - do a lookup and implement it.
174      */

175     public void bind(Name JavaDoc name, Object JavaDoc obj, Attributes JavaDoc attrs, boolean rebind )
176         throws NamingException JavaDoc
177     {
178         throw new OperationNotSupportedException JavaDoc();
179     }
180
181     /** Remove a binding. XXX do we need the isContext case ?
182      */

183     public void unbind(Name JavaDoc name, boolean isContext)
184         throws NamingException JavaDoc
185     {
186         throw new OperationNotSupportedException JavaDoc();
187     }
188
189     /* XXX There are 2 ways to list the childs: array ( size/elementAt ) or
190        iterator/Enumeration. Since the JNDI interface uses iteration -
191        that's what we should use.
192        */

193
194     /** Return the child elements, if any.
195      *
196      * This is a String or Name or Binding or NameClassPari enumeration -
197      * the Context implementation will wrap it as a NamingEnumeration and
198      * construct the right information.
199      *
200      * XXX name is all we need - all other info can be extracted - with some
201      * penalty. It's easy to do some instanceof tricks to avoid it, if possible,
202      * but the goal is to make it easy to write contexts, and name should be
203      * enough.
204      */

205     public Enumeration JavaDoc getChildren() throws NamingException JavaDoc {
206         return null;
207     }
208
209     public DirContext JavaDoc createSubcontext(Name JavaDoc name, Attributes JavaDoc attrs)
210         throws NamingException JavaDoc
211     {
212         // XXX We can implement a decent default using bind and the current class.
213
throw new OperationNotSupportedException JavaDoc();
214     }
215
216     /** Implement for directories
217      *
218      */

219     public Object JavaDoc getAttribute( Name JavaDoc name, String JavaDoc attName )
220         throws NamingException JavaDoc
221     {
222         throw new OperationNotSupportedException JavaDoc();
223     }
224
225     public void setAttribute( Name JavaDoc name, String JavaDoc attName, Object JavaDoc value )
226         throws NamingException JavaDoc
227     {
228         throw new OperationNotSupportedException JavaDoc();
229     }
230
231     public String JavaDoc[] getAttributeNames(Name JavaDoc name )
232         throws NamingException JavaDoc
233     {
234         throw new OperationNotSupportedException JavaDoc();
235     }
236
237
238     // -------------------- Utils --------------------
239
// XXX Implement this
240

241     /**
242      * Returns true if writing is allowed on this context.
243      */

244     protected boolean isWritable(Name JavaDoc name) {
245         return true;
246         //return ContextAccessController.isWritable(name);
247
}
248
249
250     /**
251      * Throws a naming exception is Context is not writable.
252      */

253     protected void checkWritable(Name JavaDoc n)
254         throws NamingException JavaDoc
255     {
256         if (!isWritable(n))
257             throw new NamingException JavaDoc("read only");
258     }
259
260     protected Name JavaDoc string2Name(String JavaDoc s ) throws InvalidNameException JavaDoc {
261         // XXX uniq
262
// try {
263
return new CompositeName JavaDoc( s );
264 // } catch( InvalidNameException ex ) {
265
// ex.printStackTrace();
266
// return null;
267
// }
268
}
269     
270     // -------------------- Lifecycle methods ? --------------------
271

272     /**
273      * Allocate resources for this directory context.
274      */

275     public void allocate() {
276         ; // No action taken by the default implementation
277
}
278
279
280     /**
281      * Release any resources allocated for this directory context.
282      */

283     public void release() {
284         ; // No action taken by the default implementation
285
}
286
287     public void recycle() {
288         // nothing yet.
289
}
290
291     //-------------------- Helpers --------------------
292

293     /** Just a hack so that all DirContexts can be used as tasks.
294      * They'll do nothing - the setters will be called ( just like
295      * new Context(Hashtable) - since we use introspection ) and the
296      * context can be registred as a reference in the Project ns.
297      *
298      * Then other tasks could manipulate it by name.
299      *
300      * In a future version of ant we should have the 'references'
301      * pluggable and a possible impl should be JNDI.
302      *
303      * Alternative: there is a way to use tasks without this method,
304      * but for now it's simpler.
305      */

306     public void execute() {
307     }
308 }
309
310
Popular Tags