KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.IOException JavaDoc;
53 import java.io.Serializable JavaDoc;
54 import java.rmi.MarshalledObject JavaDoc;
55 import java.util.Hashtable JavaDoc;
56 import java.util.Iterator JavaDoc;
57
58 import javax.naming.Binding JavaDoc;
59 import javax.naming.CommunicationException JavaDoc;
60 import javax.naming.ConfigurationException JavaDoc;
61 import javax.naming.Context JavaDoc;
62 import javax.naming.InvalidNameException JavaDoc;
63 import javax.naming.Name JavaDoc;
64 import javax.naming.NameClassPair JavaDoc;
65 import javax.naming.NameParser JavaDoc;
66 import javax.naming.NamingEnumeration JavaDoc;
67 import javax.naming.NamingException JavaDoc;
68 import javax.naming.Reference JavaDoc;
69 import javax.naming.Referenceable JavaDoc;
70
71 /**
72  * Context that hooks up to a remote source.
73  *
74  * @author <a HREF="mailto:peter@apache.org">Peter Donald</a>
75  * @version $Revision: 1.1 $
76  * @deprecated Toolkit deprecated and replaced by http://spice.sourceforge.net/jndikit/
77  */

78 public class RemoteContext
79     extends AbstractContext
80     implements Serializable JavaDoc
81 {
82     public static final String JavaDoc NAMESPACE_NAME = "org.apache.avalon.excalibur.naming.Namespace/NAME";
83     public static final String JavaDoc NAMESPACE = "org.apache.avalon.excalibur.naming.Namespace";
84     public static final String JavaDoc NAMING_PROVIDER = "org.apache.avalon.excalibur.naming.NamingProvider";
85
86     private transient NamingProvider m_provider;
87     private transient NameParser JavaDoc m_nameParser;
88     private transient Namespace m_namespace;
89
90     private Name JavaDoc m_baseName;
91
92     //for deserialisation
93
public RemoteContext()
94     {
95     }
96
97     public RemoteContext( final Hashtable JavaDoc environment, final Name JavaDoc baseName )
98         throws NamingException JavaDoc
99     {
100         super( environment );
101         m_baseName = baseName;
102     }
103
104     /**
105      * Helper method to bind
106      */

107     protected void bind( final Name JavaDoc name, Object JavaDoc object, final boolean rebind )
108         throws NamingException JavaDoc
109     {
110         if( isSelf( name ) )
111         {
112             throw new InvalidNameException JavaDoc( "Failed to bind self" );
113         }
114
115         String JavaDoc className = null;
116
117         object = getNamespace().getStateToBind( object, name, this, getRawEnvironment() );
118
119         if( object instanceof Reference JavaDoc )
120         {
121             className = ( (Reference JavaDoc)object ).getClassName();
122         }
123         else if( object instanceof Referenceable JavaDoc )
124         {
125             object = ( (Referenceable JavaDoc)object ).getReference();
126             className = ( (Reference JavaDoc)object ).getClassName();
127         }
128         else
129         {
130             className = object.getClass().getName();
131
132             try
133             {
134                 object = new MarshalledObject JavaDoc( object );
135             }
136             catch( final IOException JavaDoc ioe )
137             {
138                 throw new NamingException JavaDoc( "Only Reference, Referenceables and " +
139                                            "Serializable objects can be bound " +
140                                            "to context" );
141             }
142         }
143
144         try
145         {
146             if( rebind )
147             {
148                 getProvider().rebind( getAbsoluteName( name ), className, object );
149             }
150             else
151             {
152                 getProvider().bind( getAbsoluteName( name ), className, object );
153             }
154         }
155         catch( final Exception JavaDoc e )
156         {
157             throw handleException( e );
158         }
159     }
160
161     /**
162      * Release resources associated with context.
163      */

164     public void close()
165     {
166         super.close();
167         m_namespace = null;
168         m_provider = null;
169     }
170
171     /**
172      * Create a Subcontext.
173      *
174      * @param name the name of subcontext
175      * @return the created context
176      * @exception NamingException if an error occurs (ie context exists, badly formated name etc)
177      */

178     public Context JavaDoc createSubcontext( final Name JavaDoc name )
179         throws NamingException JavaDoc
180     {
181         if( isSelf( name ) )
182         {
183             throw new InvalidNameException JavaDoc( "Failed to create null subcontext" );
184         }
185
186         Context JavaDoc result = null;
187         try
188         {
189             result = getProvider().createSubcontext( getAbsoluteName( name ) );
190         }
191         catch( final Exception JavaDoc e )
192         {
193             throw handleException( e );
194         }
195
196         fillInContext( result );
197
198         return result;
199     }
200
201     public void destroySubcontext( final Name JavaDoc name )
202         throws NamingException JavaDoc
203     {
204         if( isSelf( name ) )
205         {
206             throw new InvalidNameException JavaDoc( "Failed to destroy self" );
207         }
208
209         try
210         {
211             getProvider().destroySubcontext( getAbsoluteName( name ) );
212         }
213         catch( final Exception JavaDoc e )
214         {
215             throw handleException( e );
216         }
217     }
218
219     public String JavaDoc getNameInNamespace()
220         throws NamingException JavaDoc
221     {
222         return getAbsoluteName( getNameParser().parse( "" ) ).toString();
223     }
224
225     /**
226      * Enumerates the names bound in the named context.
227      *
228      * @param name the name of the context
229      * @return the enumeration
230      * @exception NamingException if an error occurs
231      */

232     public NamingEnumeration JavaDoc list( final Name JavaDoc name )
233         throws NamingException JavaDoc
234     {
235         try
236         {
237             final NameClassPair JavaDoc[] result = getProvider().list( getAbsoluteName( name ) );
238             return new ArrayNamingEnumeration( this, m_namespace, result );
239         }
240         catch( final Exception JavaDoc e )
241         {
242             throw handleException( e );
243         }
244     }
245
246     /**
247      * Enumerates the names bound in the named context, along with the objects bound to them.
248      *
249      * @param name the name of the context
250      * @return the enumeration
251      * @exception NamingException if an error occurs
252      */

253     public NamingEnumeration JavaDoc listBindings( final Name JavaDoc name )
254         throws NamingException JavaDoc
255     {
256         try
257         {
258             final Binding JavaDoc[] result = getProvider().listBindings( getAbsoluteName( name ) );
259
260             for( int i = 0; i < result.length; i++ )
261             {
262                 final Object JavaDoc object = result[ i ].getObject();
263                 if( object instanceof Context JavaDoc )
264                 {
265                     fillInContext( (Context JavaDoc)object );
266                 }
267             }
268
269             return new ArrayNamingEnumeration( this, m_namespace, result );
270         }
271         catch( final Exception JavaDoc e )
272         {
273             throw handleException( e );
274         }
275     }
276
277     /**
278      * Get the object named.
279      *
280      * @param name the name
281      * @return the object
282      * @exception NamingException if an error occurs (ie object name is inavlid or unbound)
283      */

284     public Object JavaDoc lookup( final Name JavaDoc name )
285         throws NamingException JavaDoc
286     {
287         if( isSelf( name ) )
288         {
289             return new RemoteContext( getRawEnvironment(), m_baseName );
290         }
291
292         //TODO: actually do a real-lookup
293
Object JavaDoc object = null;
294         try
295         {
296             object = getProvider().lookup( getAbsoluteName( name ) );
297
298             if( object instanceof MarshalledObject JavaDoc )
299             {
300                 object = ( (MarshalledObject JavaDoc)object ).get();
301             }
302
303             object = getNamespace().getObjectInstance( object, name, this, getRawEnvironment() );
304
305             if( object instanceof Context JavaDoc )
306             {
307                 fillInContext( (Context JavaDoc)object );
308             }
309         }
310         catch( final Exception JavaDoc e )
311         {
312             throw handleException( e );
313         }
314
315         return object;
316     }
317
318     /**
319      * Unbind a object from a name.
320      *
321      * @param name the name
322      * @exception NamingException if an error occurs
323      */

324     public void unbind( final Name JavaDoc name )
325         throws NamingException JavaDoc
326     {
327         if( isSelf( name ) )
328         {
329             throw new InvalidNameException JavaDoc( "Failed to unbind self" );
330         }
331
332         try
333         {
334             getProvider().unbind( getAbsoluteName( name ) );
335         }
336         catch( final Exception JavaDoc e )
337         {
338             throw handleException( e );
339         }
340     }
341
342     protected void fillInContext( final Context JavaDoc object )
343         throws NamingException JavaDoc
344     {
345         final Hashtable JavaDoc environment = getRawEnvironment();
346         final Iterator JavaDoc keys = environment.keySet().iterator();
347
348         while( keys.hasNext() )
349         {
350             final String JavaDoc key = (String JavaDoc)keys.next();
351             final Object JavaDoc value = environment.get( key );
352             object.addToEnvironment( key, value );
353         }
354     }
355
356     protected Namespace getNamespace()
357         throws NamingException JavaDoc
358     {
359         if( null == m_namespace )
360         {
361             final Object JavaDoc object = getRawEnvironment().get( RemoteContext.NAMESPACE );
362
363             if( !( object instanceof Namespace ) || null == object )
364             {
365                 throw new ConfigurationException JavaDoc( "Context does not contain Namespace" );
366             }
367             else
368             {
369                 m_namespace = (Namespace)object;
370             }
371         }
372
373         return m_namespace;
374     }
375
376     protected NamingProvider getProvider()
377         throws NamingException JavaDoc
378     {
379         if( null == m_provider )
380         {
381             final Object JavaDoc object = getRawEnvironment().get( RemoteContext.NAMING_PROVIDER );
382
383             if( !( object instanceof NamingProvider ) || null == object )
384             {
385                 throw new ConfigurationException JavaDoc( "Context does not contain provider" );
386             }
387             else
388             {
389                 m_provider = (NamingProvider)object;
390             }
391         }
392
393         return m_provider;
394     }
395
396     protected NameParser JavaDoc getNameParser()
397         throws NamingException JavaDoc
398     {
399         if( null == m_nameParser )
400         {
401             //Make sure provider is valid and returns nameparser
402
try
403             {
404                 m_nameParser = getProvider().getNameParser();
405             }
406             catch( final Exception JavaDoc e )
407             {
408                 throw handleException( e );
409             }
410
411         }
412         return m_nameParser;
413     }
414
415     protected Name JavaDoc getAbsoluteName( final Name JavaDoc name )
416         throws NamingException JavaDoc
417     {
418         return composeName( name, m_baseName );
419     }
420
421     protected NamingException JavaDoc handleException( final Exception JavaDoc e )
422     {
423         if( e instanceof NamingException JavaDoc )
424         {
425             return (NamingException JavaDoc)e;
426         }
427         else
428         {
429             return new CommunicationException JavaDoc( e.toString() );
430         }
431     }
432 }
433
Popular Tags