KickJava   Java API By Example, From Geeks To Geeks.

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


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.ContextNotEmptyException JavaDoc;
56 import javax.naming.InvalidNameException JavaDoc;
57 import javax.naming.Name JavaDoc;
58 import javax.naming.NameAlreadyBoundException JavaDoc;
59 import javax.naming.NameParser JavaDoc;
60 import javax.naming.NamingEnumeration JavaDoc;
61 import javax.naming.NamingException JavaDoc;
62 import javax.naming.NotContextException JavaDoc;
63 import javax.naming.OperationNotSupportedException JavaDoc;
64 import javax.naming.Reference JavaDoc;
65 import javax.naming.Referenceable JavaDoc;
66
67 /**
68  * Abstract local JNDI Context that can be inherited from to
69  * provide a particular type of Context. These contexts are assumed to be
70  * on the same machine.
71  *
72  * @author <a HREF="mailto:peter@apache.org">Peter Donald</a>
73  * @version $Revision: 1.1 $
74  * @deprecated Toolkit deprecated and replaced by http://spice.sourceforge.net/jndikit/
75  */

76 public abstract class AbstractLocalContext
77     extends AbstractContext
78 {
79     private Context JavaDoc m_parent;
80     private Namespace m_namespace;
81
82     public AbstractLocalContext( final Namespace namespace,
83                                  final Hashtable JavaDoc environment,
84                                  final Context JavaDoc parent )
85     {
86         super( environment );
87         m_namespace = namespace;
88         m_parent = parent;
89     }
90
91     /**
92      * Utility method to retrieve parent Context.
93      *
94      * @return the parent Context if any
95      */

96     protected final Context JavaDoc getParent()
97     {
98         return m_parent;
99     }
100
101     /**
102      * Utility method to retrieve the Namespace.
103      *
104      * @return the Namespace
105      */

106     protected final Namespace getNamespace()
107     {
108         return m_namespace;
109     }
110
111     protected boolean isDestroyableContext( final Object JavaDoc object )
112         throws NamingException JavaDoc
113     {
114         return getClass().isInstance( object );
115     }
116
117     protected abstract Context JavaDoc newContext()
118         throws NamingException JavaDoc;
119
120     protected abstract Context JavaDoc cloneContext()
121         throws NamingException JavaDoc;
122
123     /**
124      * Helper method to bind
125      */

126     protected void bind( final Name JavaDoc name, Object JavaDoc object, final boolean rebind )
127         throws NamingException JavaDoc
128     {
129         if( isSelf( name ) )
130         {
131             throw new InvalidNameException JavaDoc( "Failed to bind self" );
132         }
133
134         if( 1 == name.size() )
135         {
136             boolean alreadyBound = false;
137             try
138             {
139                 localLookup( name );
140                 alreadyBound = true;
141             }
142             catch( final NamingException JavaDoc ne )
143             {
144             }
145
146             if( !rebind && alreadyBound )
147             {
148                 throw new NameAlreadyBoundException JavaDoc( name.get( 0 ) );
149             }
150             else
151             {
152                 //Should this occur here or in the factories ???
153
if( object instanceof Referenceable JavaDoc )
154                 {
155                     object = ( (Referenceable JavaDoc)object ).getReference();
156                 }
157
158                 // Call getStateToBind for using any state factories
159
final Name JavaDoc atom = name.getPrefix( 1 );
160                 object = m_namespace.getStateToBind( object, atom, this, getRawEnvironment() );
161
162                 doLocalBind( name, object );
163             }
164         }
165         else
166         {
167             final Context JavaDoc context = lookupSubContext( getPathName( name ) );
168             if( rebind )
169             {
170                 context.rebind( getLeafName( name ), object );
171             }
172             else
173             {
174                 context.bind( getLeafName( name ), object );
175             }
176         }
177     }
178
179     protected abstract void doLocalBind( Name JavaDoc name, Object JavaDoc object )
180         throws NamingException JavaDoc;
181
182     public void close()
183     {
184         m_parent = null;
185         m_namespace = null;
186     }
187
188     /**
189      * Create a Subcontext.
190      *
191      * @param name the name of subcontext
192      * @return the created context
193      * @exception NamingException if an error occurs (ie context exists, badly formated name etc)
194      */

195     public Context JavaDoc createSubcontext( final Name JavaDoc name )
196         throws NamingException JavaDoc
197     {
198         final Context JavaDoc context = newContext();
199         bind( name, context );
200         return context;
201     }
202
203     public void destroySubcontext( final Name JavaDoc name )
204         throws NamingException JavaDoc
205     {
206         if( isSelf( name ) )
207         {
208             throw new InvalidNameException JavaDoc( "Failed to destroy self" );
209         }
210
211         if( 1 == name.size() )
212         {
213             Object JavaDoc object = null;
214             try
215             {
216                 object = localLookup( name );
217             }
218             catch( final NamingException JavaDoc ne )
219             {
220                 return;
221             }
222
223             checkUnbindContext( name, object );
224
225             doLocalUnbind( name );
226         }
227         else
228         {
229             final Context JavaDoc context = lookupSubContext( getPathName( name ) );
230
231             Object JavaDoc object = null;
232
233             final Name JavaDoc atom = getLeafName( name );
234             try
235             {
236                 object = context.lookup( atom );
237             }
238             catch( final NamingException JavaDoc ne )
239             {
240                 return;
241             }
242
243             checkUnbindContext( atom, object );
244
245             context.destroySubcontext( atom );
246         }
247     }
248
249     protected void checkUnbindContext( final Name JavaDoc name, final Object JavaDoc entry )
250         throws NamingException JavaDoc
251     {
252         if( !isDestroyableContext( entry ) )
253         {
254             throw new NotContextException JavaDoc( name.toString() );
255         }
256
257         final Context JavaDoc context = (Context JavaDoc)entry;
258         if( context.list( "" ).hasMoreElements() )
259         {
260             throw new ContextNotEmptyException JavaDoc( name.toString() );
261         }
262     }
263
264     public String JavaDoc getNameInNamespace()
265         throws NamingException JavaDoc
266     {
267         throw new OperationNotSupportedException JavaDoc( "Namespace has no notion of a 'full name'" );
268     }
269
270     protected NameParser JavaDoc getNameParser()
271         throws NamingException JavaDoc
272     {
273         return m_namespace.getNameParser();
274     }
275
276     /**
277      * Enumerates the names bound in the named context.
278      *
279      * @param name the name of the context
280      * @return the enumeration
281      * @exception NamingException if an error occurs
282      */

283     public NamingEnumeration JavaDoc list( final Name JavaDoc name )
284         throws NamingException JavaDoc
285     {
286         if( isSelf( name ) )
287         {
288             return doLocalList();
289         }
290         else
291         {
292             // Perhaps 'name' names a context
293
final Context JavaDoc context = lookupSubContext( name );
294             return context.list( "" );
295         }
296     }
297
298     protected abstract NamingEnumeration JavaDoc doLocalList()
299         throws NamingException JavaDoc;
300
301     protected abstract NamingEnumeration JavaDoc doLocalListBindings()
302         throws NamingException JavaDoc;
303
304     /**
305      * Enumerates the names bound in the named context, along with the objects bound to them.
306      *
307      * @param name the name of the context
308      * @return the enumeration
309      * @exception NamingException if an error occurs
310      */

311     public NamingEnumeration JavaDoc listBindings( final Name JavaDoc name )
312         throws NamingException JavaDoc
313     {
314         if( isSelf( name ) )
315         {
316             return doLocalListBindings();
317         }
318         else
319         {
320             // Perhaps 'name' names a context
321
final Context JavaDoc context = lookupSubContext( name );
322             return context.listBindings( "" );
323         }
324     }
325
326     /**
327      * Get the object named.
328      *
329      * @param name the name
330      * @return the object
331      * @exception NamingException if an error occurs (ie object name is inavlid or unbound)
332      */

333     public Object JavaDoc lookup( final Name JavaDoc name )
334         throws NamingException JavaDoc
335     {
336         //if it refers to base context return a copy of it.
337
if( isSelf( name ) )
338         {
339             return cloneContext();
340         }
341
342         if( 1 == name.size() )
343         {
344             Object JavaDoc obj = localLookup( name );
345             if( obj instanceof Reference JavaDoc )
346             {
347                 try
348                 {
349                     obj = m_namespace.getObjectInstance( obj, name, this, this.getEnvironment() );
350                 }
351                 catch( Exception JavaDoc e )
352                 {
353                     throw new NamingException JavaDoc( "Could not resolve reference" );
354                 }
355             }
356
357             return obj;
358         }
359         else
360         {
361             final Context JavaDoc context = lookupSubContext( getPathName( name ) );
362             return context.lookup( getLeafName( name ) );
363         }
364     }
365
366     /**
367      * Lookup entry in local context.
368      *
369      * @param name the name in local context (size() == 1)
370      * @return the bound object
371      * @exception NamingException if an error occurs
372      */

373     protected Object JavaDoc localLookup( final Name JavaDoc name )
374         throws NamingException JavaDoc
375     {
376         final Object JavaDoc value = doLocalLookup( name );
377
378         // Call getObjectInstance for using any object factories
379
try
380         {
381             final Name JavaDoc atom = name.getPrefix( 1 );
382             return m_namespace.getObjectInstance( value, atom, this, getRawEnvironment() );
383         }
384         catch( final Exception JavaDoc e )
385         {
386             final NamingException JavaDoc ne = new NamingException JavaDoc( "getObjectInstance failed" );
387             ne.setRootCause( e );
388             throw ne;
389         }
390     }
391
392     /**
393      * Actually lookup raw entry in local context.
394      * When overidding this it is not neccesary to resolve references etc.
395      *
396      * @param name the name in local context (size() == 1)
397      * @return the bound object
398      * @exception NamingException if an error occurs
399      */

400     protected abstract Object JavaDoc doLocalLookup( Name JavaDoc name )
401         throws NamingException JavaDoc;
402
403     /**
404      * Lookup a sub-context of current context.
405      * Note that name must have 1 or more elements.
406      *
407      * @param name the name of subcontext
408      * @return the sub-Context
409      * @exception NamingException if an error occurs (like named entry is not a Context)
410      */

411     protected Context JavaDoc lookupSubContext( final Name JavaDoc name )
412         throws NamingException JavaDoc
413     {
414         final Name JavaDoc atom = name.getPrefix( 1 );
415         Object JavaDoc object = localLookup( atom );
416
417         if( 1 != name.size() )
418         {
419             if( !( object instanceof Context JavaDoc ) )
420             {
421                 throw new NotContextException JavaDoc( atom.toString() );
422             }
423
424             object = ( (Context JavaDoc)object ).lookup( name.getSuffix( 1 ) );
425         }
426
427         if( !( object instanceof Context JavaDoc ) )
428         {
429             throw new NotContextException JavaDoc( name.toString() );
430         }
431
432         //((Context)object).close();
433
return (Context JavaDoc)object;
434     }
435
436     /**
437      * Unbind a object from a name.
438      *
439      * @param name the name
440      * @exception NamingException if an error occurs
441      */

442     public void unbind( final Name JavaDoc name )
443         throws NamingException JavaDoc
444     {
445         if( isSelf( name ) )
446         {
447             throw new InvalidNameException JavaDoc( "Cannot unbind self" );
448         }
449         else if( 1 == name.size() )
450         {
451             doLocalUnbind( name );
452         }
453         else
454         {
455             final Context JavaDoc context = lookupSubContext( getPathName( name ) );
456             context.unbind( getLeafName( name ) );
457         }
458     }
459
460     /**
461      * Actually unbind raw entry in local context.
462      *
463      * @param name the name in local context (size() == 1)
464      * @exception NamingException if an error occurs
465      */

466     protected abstract void doLocalUnbind( Name JavaDoc name )
467         throws NamingException JavaDoc;
468 }
469
Popular Tags