KickJava   Java API By Example, From Geeks To Geeks.

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


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.ContextNotEmptyException JavaDoc;
13 import javax.naming.InvalidNameException JavaDoc;
14 import javax.naming.Name JavaDoc;
15 import javax.naming.NameAlreadyBoundException JavaDoc;
16 import javax.naming.NameParser JavaDoc;
17 import javax.naming.NamingEnumeration JavaDoc;
18 import javax.naming.NamingException JavaDoc;
19 import javax.naming.NotContextException JavaDoc;
20 import javax.naming.OperationNotSupportedException JavaDoc;
21 import javax.naming.Referenceable JavaDoc;
22
23 /**
24  * Abstract local JNDI Context that can be inherited from to
25  * provide a particular type of Context. These contexts are assumed to be
26  * on the same machine.
27  *
28  * @author Peter Donald
29  * @version $Revision: 1.3 $
30  */

31 public abstract class AbstractLocalContext
32     extends AbstractContext
33 {
34     private Context JavaDoc m_parent;
35     private Namespace m_namespace;
36
37     public AbstractLocalContext( final Namespace namespace,
38                                  final Hashtable JavaDoc environment,
39                                  final Context JavaDoc parent )
40     {
41         super( environment );
42         m_namespace = namespace;
43         m_parent = parent;
44     }
45
46     /**
47      * Utility method to retrieve parent Context.
48      *
49      * @return the parent Context if any
50      */

51     protected final Context JavaDoc getParent()
52     {
53         return m_parent;
54     }
55
56     /**
57      * Utility method to retrieve the Namespace.
58      *
59      * @return the Namespace
60      */

61     protected final Namespace getNamespace()
62     {
63         return m_namespace;
64     }
65
66     protected boolean isDestroyableContext( final Object JavaDoc object )
67         throws NamingException JavaDoc
68     {
69         return getClass().isInstance( object );
70     }
71
72     protected abstract Context JavaDoc newContext()
73         throws NamingException JavaDoc;
74
75     protected abstract Context JavaDoc cloneContext()
76         throws NamingException JavaDoc;
77
78     /**
79      * Helper method to bind
80      */

81     protected void bind( final Name JavaDoc name, Object JavaDoc object, final boolean rebind )
82         throws NamingException JavaDoc
83     {
84         if( isSelf( name ) )
85         {
86             throw new InvalidNameException JavaDoc( "Failed to bind self" );
87         }
88
89         if( 1 == name.size() )
90         {
91             boolean alreadyBound;
92             try
93             {
94                 localLookup( name );
95                 alreadyBound = true;
96             }
97             catch( final NamingException JavaDoc ne )
98             {
99                 alreadyBound = false;
100             }
101
102             if( !rebind && alreadyBound )
103             {
104                 throw new NameAlreadyBoundException JavaDoc( name.get( 0 ) );
105             }
106             else
107             {
108                 if( object instanceof Referenceable JavaDoc )
109                 {
110                     object = ( (Referenceable JavaDoc)object ).getReference();
111                 }
112
113                 // Call getStateToBind for using any state factories
114
final Name JavaDoc atom = name.getPrefix( 1 );
115                 object = m_namespace.getStateToBind( object, atom, this, getRawEnvironment() );
116
117                 doLocalBind( name, object );
118             }
119         }
120         else
121         {
122             final Context JavaDoc context = lookupSubContext( getPathName( name ) );
123             if( rebind )
124             {
125                 context.rebind( getLeafName( name ), object );
126             }
127             else
128             {
129                 context.bind( getLeafName( name ), object );
130             }
131         }
132     }
133
134     protected abstract void doLocalBind( Name JavaDoc name, Object JavaDoc object )
135         throws NamingException JavaDoc;
136
137     public void close()
138     {
139         m_parent = null;
140         m_namespace = null;
141     }
142
143     /**
144      * Create a Subcontext.
145      *
146      * @param name the name of subcontext
147      * @return the created context
148      * @throws NamingException if an error occurs
149      * (ie context exists, badly formated name etc)
150      */

151     public Context JavaDoc createSubcontext( final Name JavaDoc name )
152         throws NamingException JavaDoc
153     {
154         final Context JavaDoc context = newContext();
155         bind( name, context );
156         return context;
157     }
158
159     public void destroySubcontext( final Name JavaDoc name )
160         throws NamingException JavaDoc
161     {
162         if( isSelf( name ) )
163         {
164             throw new InvalidNameException JavaDoc( "Failed to destroy self" );
165         }
166
167         if( 1 == name.size() )
168         {
169             Object JavaDoc object = null;
170             try
171             {
172                 object = localLookup( name );
173             }
174             catch( final NamingException JavaDoc ne )
175             {
176                 return;
177             }
178
179             checkUnbindContext( name, object );
180
181             doLocalUnbind( name );
182         }
183         else
184         {
185             final Context JavaDoc context = lookupSubContext( getPathName( name ) );
186
187             Object JavaDoc object = null;
188
189             final Name JavaDoc atom = getLeafName( name );
190             try
191             {
192                 object = context.lookup( atom );
193             }
194             catch( final NamingException JavaDoc ne )
195             {
196                 return;
197             }
198
199             checkUnbindContext( atom, object );
200
201             context.destroySubcontext( atom );
202         }
203     }
204
205     protected void checkUnbindContext( final Name JavaDoc name, final Object JavaDoc entry )
206         throws NamingException JavaDoc
207     {
208         if( !isDestroyableContext( entry ) )
209         {
210             throw new NotContextException JavaDoc( name.toString() );
211         }
212
213         final Context JavaDoc context = (Context JavaDoc)entry;
214         if( context.list( "" ).hasMoreElements() )
215         {
216             throw new ContextNotEmptyException JavaDoc( name.toString() );
217         }
218     }
219
220     public String JavaDoc getNameInNamespace()
221         throws NamingException JavaDoc
222     {
223         throw new OperationNotSupportedException JavaDoc( "Namespace has no notion of a 'full name'" );
224     }
225
226     protected NameParser JavaDoc getNameParser()
227         throws NamingException JavaDoc
228     {
229         return m_namespace.getNameParser();
230     }
231
232     /**
233      * Enumerates the names bound in the named context.
234      *
235      * @param name the name of the context
236      * @return the enumeration
237      * @throws javax.naming.NamingException if an error occurs
238      */

239     public NamingEnumeration JavaDoc list( final Name JavaDoc name )
240         throws NamingException JavaDoc
241     {
242         if( isSelf( name ) )
243         {
244             return doLocalList();
245         }
246         else
247         {
248             // Perhaps 'name' names a context
249
final Context JavaDoc context = lookupSubContext( name );
250             return context.list( "" );
251         }
252     }
253
254     protected abstract NamingEnumeration JavaDoc doLocalList()
255         throws NamingException JavaDoc;
256
257     protected abstract NamingEnumeration JavaDoc doLocalListBindings()
258         throws NamingException JavaDoc;
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      * @throws javax.naming.NamingException if an error occurs
266      */

267     public NamingEnumeration JavaDoc listBindings( final Name JavaDoc name )
268         throws NamingException JavaDoc
269     {
270         if( isSelf( name ) )
271         {
272             return doLocalListBindings();
273         }
274         else
275         {
276             // Perhaps 'name' names a context
277
final Context JavaDoc context = lookupSubContext( name );
278             return context.listBindings( "" );
279         }
280     }
281
282     /**
283      * Get the object named.
284      *
285      * @param name the name
286      * @return the object
287      * @throws NamingException if an error occurs
288      * (ie object name is inavlid or unbound)
289      */

290     public Object JavaDoc lookup( final Name JavaDoc name )
291         throws NamingException JavaDoc
292     {
293         //if it refers to base context return a copy of it.
294
if( isSelf( name ) )
295         {
296             return cloneContext();
297         }
298
299         if( 1 == name.size() )
300         {
301             Object JavaDoc obj = localLookup( name );
302             if (obj instanceof AbstractLocalContext)
303             {
304                 return ((AbstractLocalContext) obj).cloneContext();
305             }
306
307             return obj;
308         }
309         else
310         {
311             final Context JavaDoc context = lookupSubContext( getPathName( name ) );
312
313             return context.lookup( getLeafName( name ) );
314         }
315     }
316
317     /**
318      * Lookup entry in local context.
319      *
320      * @param name the name in local context (size() == 1)
321      * @return the bound object
322      * @throws javax.naming.NamingException if an error occurs
323      */

324     protected Object JavaDoc localLookup( final Name JavaDoc name )
325         throws NamingException JavaDoc
326     {
327         final Object JavaDoc value = doLocalLookup( name );
328
329         // Call getObjectInstance for using any object factories
330
try
331         {
332             final Name JavaDoc atom = name.getPrefix( 1 );
333             return m_namespace.getObjectInstance( value, atom, this, getRawEnvironment() );
334         }
335         catch( final Exception JavaDoc e )
336         {
337             final NamingException JavaDoc ne = new NamingException JavaDoc( "getObjectInstance failed" );
338             ne.setRootCause( e );
339             throw ne;
340         }
341     }
342
343     /**
344      * Actually lookup raw entry in local context.
345      * When overidding this it is not neccesary to resolve references etc.
346      *
347      * @param name the name in local context (size() == 1)
348      * @return the bound object
349      * @throws javax.naming.NamingException if an error occurs
350      */

351     protected abstract Object JavaDoc doLocalLookup( Name JavaDoc name )
352         throws NamingException JavaDoc;
353
354     /**
355      * Lookup a sub-context of current context.
356      * Note that name must have 1 or more elements.
357      *
358      * @param name the name of subcontext
359      * @return the sub-Context
360      * @throws javax.naming.NamingException if an error occurs (like named entry is not a Context)
361      */

362     protected Context JavaDoc lookupSubContext( final Name JavaDoc name )
363         throws NamingException JavaDoc
364     {
365         final Name JavaDoc atom = name.getPrefix( 1 );
366         Object JavaDoc object = localLookup( atom );
367
368         if( 1 != name.size() )
369         {
370             if( !( object instanceof Context JavaDoc ) )
371             {
372                 throw new NotContextException JavaDoc( atom.toString() );
373             }
374
375             object = ( (Context JavaDoc)object ).lookup( name.getSuffix( 1 ) );
376         }
377
378         if( !( object instanceof Context JavaDoc ) )
379         {
380             throw new NotContextException JavaDoc( name.toString() );
381         }
382
383         //((Context)object).close();
384
return (Context JavaDoc)object;
385     }
386
387     /**
388      * Unbind a object from a name.
389      *
390      * @param name the name
391      * @throws javax.naming.NamingException if an error occurs
392      */

393     public void unbind( final Name JavaDoc name )
394         throws NamingException JavaDoc
395     {
396         if( isSelf( name ) )
397         {
398             throw new InvalidNameException JavaDoc( "Cannot unbind self" );
399         }
400         else if( 1 == name.size() )
401         {
402             doLocalUnbind( name );
403         }
404         else
405         {
406             final Context JavaDoc context = lookupSubContext( getPathName( name ) );
407             context.unbind( getLeafName( name ) );
408         }
409     }
410
411     /**
412      * Actually unbind raw entry in local context.
413      *
414      * @param name the name in local context (size() == 1)
415      * @throws javax.naming.NamingException if an error occurs
416      */

417     protected abstract void doLocalUnbind( Name JavaDoc name )
418         throws NamingException JavaDoc;
419 }
420
Popular Tags