KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > spice > jndikit > AbstractContext


1 /*
2  * Copyright (C) The Spice Group. All rights reserved.
3  *
4  * This software is published under the terms of the Spice
5  * Software License version 1.1, a copy of which has been included
6  * with this distribution in the LICENSE.txt file.
7  */

8 package org.codehaus.spice.jndikit;
9
10 import java.util.Hashtable JavaDoc;
11 import javax.naming.Context JavaDoc;
12 import javax.naming.InvalidNameException JavaDoc;
13 import javax.naming.Name JavaDoc;
14 import javax.naming.NameParser JavaDoc;
15 import javax.naming.NamingEnumeration JavaDoc;
16 import javax.naming.NamingException JavaDoc;
17
18 /**
19  * Abstract JNDI Context that can be inherited from to
20  * provide a particular type of Context.
21  *
22  * @author Peter Donald
23  * @version $Revision: 1.1 $
24  */

25 public abstract class AbstractContext
26     implements Context JavaDoc
27 {
28     private Hashtable JavaDoc m_environment;
29
30     public AbstractContext()
31     {
32         this( new Hashtable JavaDoc() );
33     }
34
35     public AbstractContext( final Hashtable JavaDoc environment )
36     {
37         m_environment = environment;
38     }
39
40     protected abstract NameParser JavaDoc getNameParser()
41         throws NamingException JavaDoc;
42
43     /**
44      * Add a key-value pair to environment
45      *
46      * @param key the key
47      * @param value the value
48      * @return the value
49      */

50     public Object JavaDoc addToEnvironment( final String JavaDoc key, final Object JavaDoc value )
51         throws NamingException JavaDoc
52     {
53         if( null == m_environment )
54         {
55             m_environment = new Hashtable JavaDoc( 5, 0.75f );
56         }
57         return m_environment.put( key, value );
58     }
59
60     /**
61      * Release resources associated with context.
62      *
63      */

64     public void close()
65     {
66         m_environment = null;
67     }
68
69     protected boolean isSelf( final Name JavaDoc name )
70     {
71         return ( name.isEmpty() || name.get( 0 ).equals( "" ) );
72     }
73
74     /**
75      * Bind an object to a name.
76      *
77      * @param name the name to bind to
78      * @param object the object
79      * @throws NamingException if an error occurs such as
80      * bad name or invalid binding
81      */

82     public void bind( final String JavaDoc name, final Object JavaDoc object )
83         throws NamingException JavaDoc
84     {
85         bind( getNameParser().parse( name ), object );
86     }
87
88     /**
89      * Bind an object to a name.
90      *
91      * @param name the name to bind to
92      * @param object the object
93      * @throws NamingException if an error occurs such as
94      * bad name or invalid binding
95      */

96     public void bind( final Name JavaDoc name, final Object JavaDoc object )
97         throws NamingException JavaDoc
98     {
99         bind( name, object, false );
100     }
101
102     /**
103      * Helper method to bind
104      */

105     protected abstract void bind( Name JavaDoc name, Object JavaDoc object, boolean rebind )
106         throws NamingException JavaDoc;
107
108     /**
109      * Compose a name form a name and a prefix.
110      *
111      * @param name the name
112      * @param prefix the prefix
113      * @return the composed name
114      * @throws javax.naming.NamingException if a badly formatted name for context
115      */

116     public String JavaDoc composeName( final String JavaDoc name, final String JavaDoc prefix )
117         throws NamingException JavaDoc
118     {
119         final NameParser JavaDoc nameParser = getNameParser();
120         final Name JavaDoc result =
121             composeName( nameParser.parse( name ), nameParser.parse( prefix ) );
122         return result.toString();
123     }
124
125     /**
126      * Compose a name form a name and a prefix.
127      *
128      * @param name the name
129      * @param prefix the prefix
130      * @return the composed name
131      * @throws javax.naming.NamingException if a badly formatted name for context
132      */

133     public Name JavaDoc composeName( final Name JavaDoc name, final Name JavaDoc prefix )
134         throws NamingException JavaDoc
135     {
136         final Name JavaDoc result = (Name JavaDoc)( prefix.clone() );
137         result.addAll( name );
138         return result;
139     }
140
141     /**
142      * Create a Subcontext.
143      *
144      * @param name the name of subcontext
145      * @return the created context
146      * @throws NamingException if an error occurs
147      * (ie context exists, badly formated name etc)
148      */

149     public Context JavaDoc createSubcontext( final String JavaDoc name )
150         throws NamingException JavaDoc
151     {
152         return createSubcontext( getNameParser().parse( name ) );
153     }
154
155     /**
156      * Destroy a Subcontext.
157      *
158      * @param name the name of subcontext to destroy
159      * @throws javax.naming.NamingException if an error occurs such as malformed name or
160      * context not exiting or not empty
161      */

162     public void destroySubcontext( final String JavaDoc name )
163         throws NamingException JavaDoc
164     {
165         destroySubcontext( getNameParser().parse( name ) );
166     }
167
168     /**
169      * Return a copy of environment.
170      *
171      * @return the environment
172      */

173     public Hashtable JavaDoc getEnvironment()
174         throws NamingException JavaDoc
175     {
176         if( null == m_environment )
177         {
178             return new Hashtable JavaDoc( 3, 0.75f );
179         }
180         else
181         {
182             return (Hashtable JavaDoc)m_environment.clone();
183         }
184     }
185
186     /**
187      * Get the NameParser for the named context.
188      *
189      * @param name
190      * @return the NameParser
191      * @throws javax.naming.NamingException if an error occurs
192      */

193     public NameParser JavaDoc getNameParser( final String JavaDoc name )
194         throws NamingException JavaDoc
195     {
196         return getNameParser( getNameParser().parse( name ) );
197     }
198
199     /**
200      * Get the NameParser for the named context.
201      *
202      * @param name
203      * @return the NameParser
204      * @throws javax.naming.NamingException if an error occurs
205      */

206     public NameParser JavaDoc getNameParser( final Name JavaDoc name )
207         throws NamingException JavaDoc
208     {
209         if( name.isEmpty() )
210         {
211             return getNameParser();
212         }
213
214         Object JavaDoc object = lookup( name );
215         if( !( object instanceof Context JavaDoc ) )
216         {
217             object = lookup( getPathName( name ) );
218         }
219
220         final Context JavaDoc context = (Context JavaDoc)object;
221         final NameParser JavaDoc parser = context.getNameParser( "" );
222         context.close();
223         return parser;
224     }
225
226     /**
227      * Enumerates the names bound in the named context, along with the objects bound to them.
228      *
229      * @param name the name of the context
230      * @return the enumeration
231      * @throws javax.naming.NamingException if an error occurs
232      */

233     public NamingEnumeration JavaDoc list( final String JavaDoc name )
234         throws NamingException JavaDoc
235     {
236         return list( getNameParser().parse( name ) );
237     }
238
239     /**
240      * Enumerates the names bound in the named context, along with the objects bound to them.
241      *
242      * @param name the name of the context
243      * @return the enumeration
244      * @throws javax.naming.NamingException if an error occurs
245      */

246     public NamingEnumeration JavaDoc listBindings( final String JavaDoc name )
247         throws NamingException JavaDoc
248     {
249         return listBindings( getNameParser().parse( name ) );
250     }
251
252     /**
253      * Get the object named.
254      *
255      * @param name the name
256      * @return the object
257      * @throws NamingException if an error occurs
258      * (ie object name is inavlid or unbound)
259      */

260     public Object JavaDoc lookup( final String JavaDoc name )
261         throws NamingException JavaDoc
262     {
263         return lookup( getNameParser().parse( name ) );
264     }
265
266     /**
267      * Get the object named following all links.
268      *
269      * @param name the name
270      * @return the object
271      * @throws NamingException if an error occurs
272      * (ie object name is inavlid or unbound)
273      */

274     public Object JavaDoc lookupLink( final String JavaDoc name )
275         throws NamingException JavaDoc
276     {
277         return lookupLink( getNameParser().parse( name ) );
278     }
279
280     /**
281      * Get the object named following all links.
282      *
283      * @param name the name
284      * @return the object
285      * @throws NamingException if an error occurs
286      * (ie object name is inavlid or unbound)
287      */

288     public Object JavaDoc lookupLink( final Name JavaDoc name )
289         throws NamingException JavaDoc
290     {
291         return lookup( name );
292     }
293
294     /**
295      * Binds a name to an object, overwriting any existing binding.
296      *
297      * @param name the name
298      * @param object the object
299      * @throws javax.naming.NamingException if an error occurs
300      */

301     public void rebind( final String JavaDoc name, final Object JavaDoc object )
302         throws NamingException JavaDoc
303     {
304         rebind( getNameParser().parse( name ), object );
305     }
306
307     /**
308      * Binds a name to an object, overwriting any existing binding.
309      *
310      * @param name the name
311      * @param object the object
312      * @throws javax.naming.NamingException if an error occurs
313      */

314     public void rebind( final Name JavaDoc name, final Object JavaDoc object )
315         throws NamingException JavaDoc
316     {
317         bind( name, object, true );
318     }
319
320     /**
321      * Remove a key-value pair form environment and return it.
322      *
323      * @param key the key
324      * @return the value
325      */

326     public Object JavaDoc removeFromEnvironment( final String JavaDoc key )
327         throws NamingException JavaDoc
328     {
329         if( null == m_environment )
330         {
331             return null;
332         }
333         return m_environment.remove( key );
334     }
335
336     /**
337      * Rename a already bound object
338      *
339      * @param oldName the old name
340      * @param newName the new name
341      * @throws javax.naming.NamingException if an error occurs
342      */

343     public void rename( final String JavaDoc oldName, final String JavaDoc newName )
344         throws NamingException JavaDoc
345     {
346         rename( getNameParser().parse( oldName ), getNameParser().parse( newName ) );
347     }
348
349     public void rename( final Name JavaDoc oldName, final Name JavaDoc newName )
350         throws NamingException JavaDoc
351     {
352         if( isSelf( oldName ) || isSelf( newName ) )
353         {
354             final String JavaDoc message = "Failed to rebind self";
355             throw new InvalidNameException JavaDoc( message );
356         }
357         else if( oldName.equals( newName ) )
358         {
359             final String JavaDoc message = "Failed to rebind identical names";
360             throw new InvalidNameException JavaDoc( message );
361         }
362
363         bind( newName, lookup( oldName ) );
364         unbind( oldName );
365     }
366
367     /**
368      * Unbind a object from a name.
369      *
370      * @param name the name
371      * @throws javax.naming.NamingException if an error occurs
372      */

373     public void unbind( final String JavaDoc name )
374         throws NamingException JavaDoc
375     {
376         unbind( getNameParser().parse( name ) );
377     }
378
379     /**
380      * Utility method to retrieve raw environment value.
381      * This means that null will be returned if the value is null.
382      *
383      * @return the environment hashtable or null
384      */

385     protected final Hashtable JavaDoc getRawEnvironment()
386     {
387         return m_environment;
388     }
389
390     /**
391      * Get name components minus leaf name component.
392      *
393      * @param name the name elements leading up to last element
394      * @return the name
395      * @throws javax.naming.NamingException if an error occurs
396      */

397     protected Name JavaDoc getPathName( final Name JavaDoc name )
398         throws NamingException JavaDoc
399     {
400         return name.getPrefix( name.size() - 1 );
401     }
402
403     /**
404      * Get leaf name component from specified Name object.
405      *
406      * @param name the name to retrieve leaf from
407      * @return the leaf name component
408      * @throws javax.naming.NamingException if an error occurs
409      */

410     protected Name JavaDoc getLeafName( final Name JavaDoc name )
411         throws NamingException JavaDoc
412     {
413         return name.getSuffix( name.size() - 1 );
414     }
415 }
416
Popular Tags