KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > naming > AbstractContext


1 /*
2
3  ============================================================================
4                    The Apache Software License, Version 1.1
5  ============================================================================
6  
7  Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
8  
9  Redistribution and use in source and binary forms, with or without modifica-
10  tion, are permitted provided that the following conditions are met:
11  
12  1. Redistributions of source code must retain the above copyright notice,
13     this list of conditions and the following disclaimer.
14  
15  2. Redistributions in binary form must reproduce the above copyright notice,
16     this list of conditions and the following disclaimer in the documentation
17     and/or other materials provided with the distribution.
18  
19  3. The end-user documentation included with the redistribution, if any, must
20     include the following acknowledgment: "This product includes software
21     developed by the Apache Software Foundation (http://www.apache.org/)."
22     Alternately, this acknowledgment may appear in the software itself, if
23     and wherever such third-party acknowledgments normally appear.
24  
25  4. The names "Jakarta", "Avalon", "Excalibur" and "Apache Software Foundation"
26     must not be used to endorse or promote products derived from this software
27     without prior written permission. For written permission, please contact
28     apache@apache.org.
29  
30  5. Products derived from this software may not be called "Apache", nor may
31     "Apache" appear in their name, without prior written permission of the
32     Apache Software Foundation.
33  
34  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
35  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
36  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37  APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
38  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
39  DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
40  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
41  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
43  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44  
45  This software consists of voluntary contributions made by many individuals
46  on behalf of the Apache Software Foundation. For more information on the
47  Apache Software Foundation, please see <http://www.apache.org/>.
48  
49 */

50 package org.apache.avalon.excalibur.naming;
51
52 import java.util.Hashtable JavaDoc;
53
54 import javax.naming.Context JavaDoc;
55 import javax.naming.InvalidNameException JavaDoc;
56 import javax.naming.Name JavaDoc;
57 import javax.naming.NameParser JavaDoc;
58 import javax.naming.NamingEnumeration JavaDoc;
59 import javax.naming.NamingException JavaDoc;
60
61 /**
62  * Abstract JNDI Context that can be inherited from to
63  * provide a particular type of Context.
64  *
65  * @author <a HREF="mailto:peter@apache.org">Peter Donald</a>
66  * @version $Revision: 1.1 $
67  * @deprecated Toolkit deprecated and replaced by http://spice.sourceforge.net/jndikit/
68  */

69 public abstract class AbstractContext
70     implements Context JavaDoc
71 {
72     private Hashtable JavaDoc m_environment;
73
74     public AbstractContext()
75     {
76         this( new Hashtable JavaDoc() );
77     }
78
79     public AbstractContext( final Hashtable JavaDoc environment )
80     {
81         m_environment = environment;
82     }
83
84     protected abstract NameParser JavaDoc getNameParser()
85         throws NamingException JavaDoc;
86
87     /**
88      * Add a key-value pair to environment
89      *
90      * @param key the key
91      * @param value the value
92      * @return the value
93      */

94     public Object JavaDoc addToEnvironment( final String JavaDoc key, final Object JavaDoc value )
95         throws NamingException JavaDoc
96     {
97         if( null == m_environment ) m_environment = new Hashtable JavaDoc( 5, 0.75f );
98         return m_environment.put( key, value );
99     }
100
101     /**
102      * Release resources associated with context.
103      *
104      */

105     public void close()
106     {
107         m_environment = null;
108     }
109
110     protected boolean isSelf( final Name JavaDoc name )
111     {
112         return ( name.isEmpty() || name.get( 0 ).equals( "" ) );
113     }
114
115     /**
116      * Bind an object to a name.
117      *
118      * @param name the name to bind to
119      * @param object the object
120      * @exception NamingException if an error occurs such as bad name or invalid binding
121      */

122     public void bind( final String JavaDoc name, final Object JavaDoc object )
123         throws NamingException JavaDoc
124     {
125         bind( getNameParser().parse( name ), object );
126     }
127
128     /**
129      * Bind an object to a name.
130      *
131      * @param name the name to bind to
132      * @param object the object
133      * @exception NamingException if an error occurs such as bad name or invalid binding
134      */

135     public void bind( final Name JavaDoc name, final Object JavaDoc object )
136         throws NamingException JavaDoc
137     {
138         bind( name, object, false );
139     }
140
141     /**
142      * Helper method to bind
143      */

144     protected abstract void bind( Name JavaDoc name, Object JavaDoc object, boolean rebind )
145         throws NamingException JavaDoc;
146
147     /**
148      * Compose a name form a name and a prefix.
149      *
150      * @param name the name
151      * @param prefix the prefix
152      * @return the composed name
153      * @exception NamingException if a badly formatted name for context
154      */

155     public String JavaDoc composeName( final String JavaDoc name, final String JavaDoc prefix )
156         throws NamingException JavaDoc
157     {
158         final NameParser JavaDoc nameParser = getNameParser();
159         final Name JavaDoc result =
160             composeName( nameParser.parse( name ), nameParser.parse( prefix ) );
161         return result.toString();
162     }
163
164     /**
165      * Compose a name form a name and a prefix.
166      *
167      * @param name the name
168      * @param prefix the prefix
169      * @return the composed name
170      * @exception NamingException if a badly formatted name for context
171      */

172     public Name JavaDoc composeName( final Name JavaDoc name, final Name JavaDoc prefix )
173         throws NamingException JavaDoc
174     {
175         final Name JavaDoc result = (Name JavaDoc)( prefix.clone() );
176         result.addAll( name );
177         return result;
178     }
179
180     /**
181      * Create a Subcontext.
182      *
183      * @param name the name of subcontext
184      * @return the created context
185      * @exception NamingException if an error occurs (ie context exists, badly formated name etc)
186      */

187     public Context JavaDoc createSubcontext( final String JavaDoc name )
188         throws NamingException JavaDoc
189     {
190         return createSubcontext( getNameParser().parse( name ) );
191     }
192
193     /**
194      * Destroy a Subcontext.
195      *
196      * @param name the name of subcontext to destroy
197      * @exception NamingException if an error occurs such as malformed name or
198      * context not exiting or not empty
199      */

200     public void destroySubcontext( final String JavaDoc name )
201         throws NamingException JavaDoc
202     {
203         destroySubcontext( getNameParser().parse( name ) );
204     }
205
206     /**
207      * Return a copy of environment.
208      *
209      * @return the environment
210      */

211     public Hashtable JavaDoc getEnvironment()
212         throws NamingException JavaDoc
213     {
214         if( null == m_environment )
215             return new Hashtable JavaDoc( 3, 0.75f );
216         else
217             return (Hashtable JavaDoc)m_environment.clone();
218     }
219
220     /**
221      * Get the NameParser for the named context.
222      *
223      * @param name
224      * @return the NameParser
225      * @exception NamingException if an error occurs
226      */

227     public NameParser JavaDoc getNameParser( final String JavaDoc name )
228         throws NamingException JavaDoc
229     {
230         return getNameParser( getNameParser().parse( name ) );
231     }
232
233     /**
234      * Get the NameParser for the named context.
235      *
236      * @param name
237      * @return the NameParser
238      * @exception NamingException if an error occurs
239      */

240     public NameParser JavaDoc getNameParser( final Name JavaDoc name )
241         throws NamingException JavaDoc
242     {
243         if( name.isEmpty() )
244         {
245             return getNameParser();
246         }
247
248         Object JavaDoc object = lookup( name );
249         if( !( object instanceof Context JavaDoc ) )
250         {
251             object = lookup( getPathName( name ) );
252         }
253
254         final Context JavaDoc context = (Context JavaDoc)object;
255         final NameParser JavaDoc parser = context.getNameParser( "" );
256         context.close();
257         return parser;
258     }
259
260     /**
261      * Enumerates the names bound in the named context, along with the objects bound to them.
262      *
263      * @param name the name of the context
264      * @return the enumeration
265      * @exception NamingException if an error occurs
266      */

267     public NamingEnumeration JavaDoc list( final String JavaDoc name )
268         throws NamingException JavaDoc
269     {
270         return list( getNameParser().parse( name ) );
271     }
272
273     /**
274      * Enumerates the names bound in the named context, along with the objects bound to them.
275      *
276      * @param name the name of the context
277      * @return the enumeration
278      * @exception NamingException if an error occurs
279      */

280     public NamingEnumeration JavaDoc listBindings( final String JavaDoc name )
281         throws NamingException JavaDoc
282     {
283         return listBindings( getNameParser().parse( name ) );
284     }
285
286     /**
287      * Get the object named.
288      *
289      * @param name the name
290      * @return the object
291      * @exception NamingException if an error occurs (ie object name is inavlid or unbound)
292      */

293     public Object JavaDoc lookup( final String JavaDoc name )
294         throws NamingException JavaDoc
295     {
296         return lookup( getNameParser().parse( name ) );
297     }
298
299     /**
300      * Get the object named following all links.
301      *
302      * @param name the name
303      * @return the object
304      * @exception NamingException if an error occurs (ie object name is inavlid or unbound)
305      */

306     public Object JavaDoc lookupLink( final String JavaDoc name )
307         throws NamingException JavaDoc
308     {
309         return lookupLink( getNameParser().parse( name ) );
310     }
311
312     /**
313      * Get the object named following all links.
314      *
315      * @param name the name
316      * @return the object
317      * @exception NamingException if an error occurs (ie object name is inavlid or unbound)
318      */

319     public Object JavaDoc lookupLink( final Name JavaDoc name )
320         throws NamingException JavaDoc
321     {
322         return lookup( name );
323     }
324
325     /**
326      * Binds a name to an object, overwriting any existing binding.
327      *
328      * @param name the name
329      * @param object the object
330      * @exception NamingException if an error occurs
331      */

332     public void rebind( final String JavaDoc name, final Object JavaDoc object )
333         throws NamingException JavaDoc
334     {
335         rebind( getNameParser().parse( name ), object );
336     }
337
338     /**
339      * Binds a name to an object, overwriting any existing binding.
340      *
341      * @param name the name
342      * @param object the object
343      * @exception NamingException if an error occurs
344      */

345     public void rebind( final Name JavaDoc name, final Object JavaDoc object )
346         throws NamingException JavaDoc
347     {
348         bind( name, object, true );
349     }
350
351     /**
352      * Remove a key-value pair form environment and return it.
353      *
354      * @param key the key
355      * @return the value
356      */

357     public Object JavaDoc removeFromEnvironment( final String JavaDoc key )
358         throws NamingException JavaDoc
359     {
360         if( null == m_environment ) return null;
361         return m_environment.remove( key );
362     }
363
364     /**
365      * Rename a already bound object
366      *
367      * @param oldName the old name
368      * @param newName the new name
369      * @exception NamingException if an error occurs
370      */

371     public void rename( final String JavaDoc oldName, final String JavaDoc newName )
372         throws NamingException JavaDoc
373     {
374         rename( getNameParser().parse( oldName ), getNameParser().parse( newName ) );
375     }
376
377     public void rename( final Name JavaDoc oldName, final Name JavaDoc newName )
378         throws NamingException JavaDoc
379     {
380         if( isSelf( oldName ) || isSelf( newName ) )
381         {
382             throw new InvalidNameException JavaDoc( "Failed to rebind self" );
383         }
384         else if( oldName.equals( newName ) )
385         {
386             throw new InvalidNameException JavaDoc( "Failed to rebind identical names" );
387         }
388
389         bind( newName, lookup( oldName ) );
390         unbind( oldName );
391     }
392
393     /**
394      * Unbind a object from a name.
395      *
396      * @param name the name
397      * @exception NamingException if an error occurs
398      */

399     public void unbind( final String JavaDoc name )
400         throws NamingException JavaDoc
401     {
402         unbind( getNameParser().parse( name ) );
403     }
404
405     /**
406      * Utility method to retrieve raw environment value.
407      * This means that null will be returned if the value is null.
408      *
409      * @return the environment hashtable or null
410      */

411     protected final Hashtable JavaDoc getRawEnvironment()
412     {
413         return m_environment;
414     }
415
416     /**
417      * Get name components minus leaf name component.
418      *
419      * @param name the name elements leading up to last element
420      * @return the name
421      * @exception NamingException if an error occurs
422      */

423     protected Name JavaDoc getPathName( final Name JavaDoc name )
424         throws NamingException JavaDoc
425     {
426         return name.getPrefix( name.size() - 1 );
427     }
428
429     /**
430      * Get leaf name component from specified Name object.
431      *
432      * @param name the name to retrieve leaf from
433      * @return the leaf name component
434      * @exception NamingException if an error occurs
435      */

436     protected Name JavaDoc getLeafName( final Name JavaDoc name )
437         throws NamingException JavaDoc
438     {
439         return name.getSuffix( name.size() - 1 );
440     }
441 }
442
Popular Tags