KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.IOException JavaDoc;
11 import java.io.Serializable JavaDoc;
12 import java.rmi.MarshalledObject JavaDoc;
13 import java.util.Hashtable JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import javax.naming.Binding JavaDoc;
16 import javax.naming.CommunicationException JavaDoc;
17 import javax.naming.ConfigurationException JavaDoc;
18 import javax.naming.Context JavaDoc;
19 import javax.naming.InvalidNameException JavaDoc;
20 import javax.naming.Name JavaDoc;
21 import javax.naming.NameClassPair JavaDoc;
22 import javax.naming.NameParser JavaDoc;
23 import javax.naming.NamingEnumeration JavaDoc;
24 import javax.naming.NamingException JavaDoc;
25 import javax.naming.Reference JavaDoc;
26 import javax.naming.Referenceable JavaDoc;
27
28 /**
29  * Context that hooks up to a remote source.
30  *
31  * @author Peter Donald
32  * @version $Revision: 1.2 $
33  */

34 public class RemoteContext
35     extends AbstractContext
36     implements Serializable JavaDoc
37 {
38     public static final String JavaDoc NAMESPACE_NAME = "org.codehaus.spice.jndikit.Namespace/NAME";
39     public static final String JavaDoc NAMESPACE = "org.codehaus.spice.jndikit.Namespace";
40     public static final String JavaDoc NAMING_PROVIDER = "org.codehaus.spice.jndikit.NamingProvider";
41
42     private transient NamingProvider m_provider;
43     private transient NameParser JavaDoc m_nameParser;
44     private transient Namespace m_namespace;
45
46     private Name JavaDoc m_baseName;
47
48     //for deserialisation
49
public RemoteContext()
50     {
51     }
52
53     public RemoteContext( final Hashtable JavaDoc environment, final Name JavaDoc baseName )
54         throws NamingException JavaDoc
55     {
56         super( environment );
57         m_baseName = baseName;
58     }
59
60     /**
61      * Helper method to bind
62      */

63     protected void bind( final Name JavaDoc name, Object JavaDoc object, final boolean rebind )
64         throws NamingException JavaDoc
65     {
66         if( isSelf( name ) )
67         {
68             throw new InvalidNameException JavaDoc( "Failed to bind self" );
69         }
70
71         String JavaDoc className = null;
72
73         object = getNamespace().getStateToBind( object, name, this, getRawEnvironment() );
74
75         if( object instanceof Reference JavaDoc )
76         {
77             className = ( (Reference JavaDoc)object ).getClassName();
78         }
79         else if( object instanceof Referenceable JavaDoc )
80         {
81             object = ( (Referenceable JavaDoc)object ).getReference();
82             className = ( (Reference JavaDoc)object ).getClassName();
83         }
84         else
85         {
86             className = object.getClass().getName();
87
88             try
89             {
90                 object = new MarshalledObject JavaDoc( object );
91             }
92             catch( final IOException JavaDoc ioe )
93             {
94                 throw new NamingException JavaDoc( "Only Reference, Referenceables and " +
95                                            "Serializable objects can be bound " +
96                                            "to context" );
97             }
98         }
99
100         try
101         {
102             if( rebind )
103             {
104                 getProvider().rebind( getAbsoluteName( name ), className, object );
105             }
106             else
107             {
108                 getProvider().bind( getAbsoluteName( name ), className, object );
109             }
110         }
111         catch( final Exception JavaDoc e )
112         {
113             throw handleException( e );
114         }
115     }
116
117     /**
118      * Release resources associated with context.
119      */

120     public void close()
121     {
122         super.close();
123         m_namespace = null;
124         m_provider = null;
125     }
126
127     /**
128      * Create a Subcontext.
129      *
130      * @param name the name of subcontext
131      * @return the created context
132      * @throws NamingException if an error occurs
133      * (ie context exists, badly formated name etc)
134      */

135     public Context JavaDoc createSubcontext( final Name JavaDoc name )
136         throws NamingException JavaDoc
137     {
138         if( isSelf( name ) )
139         {
140             throw new InvalidNameException JavaDoc( "Failed to create null subcontext" );
141         }
142
143         Context JavaDoc result = null;
144         try
145         {
146             result = getProvider().createSubcontext( getAbsoluteName( name ) );
147         }
148         catch( final Exception JavaDoc e )
149         {
150             throw handleException( e );
151         }
152
153         fillInContext( result );
154
155         return result;
156     }
157
158     public void destroySubcontext( final Name JavaDoc name )
159         throws NamingException JavaDoc
160     {
161         if( isSelf( name ) )
162         {
163             throw new InvalidNameException JavaDoc( "Failed to destroy self" );
164         }
165
166         try
167         {
168             getProvider().destroySubcontext( getAbsoluteName( name ) );
169         }
170         catch( final Exception JavaDoc e )
171         {
172             throw handleException( e );
173         }
174     }
175
176     public String JavaDoc getNameInNamespace()
177         throws NamingException JavaDoc
178     {
179         return getAbsoluteName( getNameParser().parse( "" ) ).toString();
180     }
181
182     /**
183      * Enumerates the names bound in the named context.
184      *
185      * @param name the name of the context
186      * @return the enumeration
187      * @throws javax.naming.NamingException if an error occurs
188      */

189     public NamingEnumeration JavaDoc list( final Name JavaDoc name )
190         throws NamingException JavaDoc
191     {
192         try
193         {
194             final NameClassPair JavaDoc[] result = getProvider().list( getAbsoluteName( name ) );
195             return new ArrayNamingEnumeration( this, getNamespace(), result );
196         }
197         catch( final Exception JavaDoc e )
198         {
199             throw handleException( e );
200         }
201     }
202
203     /**
204      * Enumerates the names bound in the named context, along with the objects bound to them.
205      *
206      * @param name the name of the context
207      * @return the enumeration
208      * @throws javax.naming.NamingException if an error occurs
209      */

210     public NamingEnumeration JavaDoc listBindings( final Name JavaDoc name )
211         throws NamingException JavaDoc
212     {
213         try
214         {
215             final Binding JavaDoc[] result = getProvider().listBindings( getAbsoluteName( name ) );
216
217             for( int i = 0; i < result.length; i++ )
218             {
219                 final Object JavaDoc object = result[ i ].getObject();
220                 if( object instanceof Context JavaDoc )
221                 {
222                     fillInContext( (Context JavaDoc)object );
223                 } else if (object instanceof MarshalledObject JavaDoc) {
224                     result[i].setObject(( (MarshalledObject JavaDoc)object ).get());
225                 }
226             }
227
228             return new ArrayNamingEnumeration( this, getNamespace(), result );
229         }
230         catch( final Exception JavaDoc e )
231         {
232             throw handleException( e );
233         }
234     }
235
236     /**
237      * Get the object named.
238      *
239      * @param name the name
240      * @return the object
241      * @throws NamingException if an error occurs
242      * (ie object name is inavlid or unbound)
243      */

244     public Object JavaDoc lookup( final Name JavaDoc name )
245         throws NamingException JavaDoc
246     {
247         if( isSelf( name ) )
248         {
249             return new RemoteContext( getRawEnvironment(), m_baseName );
250         }
251
252         //actually do a real-lookup
253
Object JavaDoc object = null;
254         try
255         {
256             object = getProvider().lookup( getAbsoluteName( name ) );
257
258             if( object instanceof MarshalledObject JavaDoc )
259             {
260                 object = ( (MarshalledObject JavaDoc)object ).get();
261             }
262
263             object = getNamespace().getObjectInstance( object, name, this, getRawEnvironment() );
264
265             if( object instanceof Context JavaDoc )
266             {
267                 fillInContext( (Context JavaDoc)object );
268             }
269         }
270         catch( final Exception JavaDoc e )
271         {
272             throw handleException( e );
273         }
274
275         return object;
276     }
277
278     /**
279      * Unbind a object from a name.
280      *
281      * @param name the name
282      * @throws javax.naming.NamingException if an error occurs
283      */

284     public void unbind( final Name JavaDoc name )
285         throws NamingException JavaDoc
286     {
287         if( isSelf( name ) )
288         {
289             throw new InvalidNameException JavaDoc( "Failed to unbind self" );
290         }
291
292         try
293         {
294             getProvider().unbind( getAbsoluteName( name ) );
295         }
296         catch( final Exception JavaDoc e )
297         {
298             throw handleException( e );
299         }
300     }
301
302     protected void fillInContext( final Context JavaDoc object )
303         throws NamingException JavaDoc
304     {
305         final Hashtable JavaDoc environment = getRawEnvironment();
306         final Iterator JavaDoc keys = environment.keySet().iterator();
307
308         while( keys.hasNext() )
309         {
310             final String JavaDoc key = (String JavaDoc)keys.next();
311             final Object JavaDoc value = environment.get( key );
312             object.addToEnvironment( key, value );
313         }
314     }
315
316     protected Namespace getNamespace()
317         throws NamingException JavaDoc
318     {
319         if( null == m_namespace )
320         {
321             final Object JavaDoc object = getRawEnvironment().get( RemoteContext.NAMESPACE );
322
323             if( !( object instanceof Namespace ) || null == object )
324             {
325                 throw new ConfigurationException JavaDoc( "Context does not contain Namespace" );
326             }
327             else
328             {
329                 m_namespace = (Namespace)object;
330             }
331         }
332
333         return m_namespace;
334     }
335
336     protected NamingProvider getProvider()
337         throws NamingException JavaDoc
338     {
339         if( null == m_provider )
340         {
341             final Object JavaDoc object = getRawEnvironment().get( RemoteContext.NAMING_PROVIDER );
342
343             if( !( object instanceof NamingProvider ) || null == object )
344             {
345                 throw new ConfigurationException JavaDoc( "Context does not contain provider" );
346             }
347             else
348             {
349                 m_provider = (NamingProvider)object;
350             }
351         }
352
353         return m_provider;
354     }
355
356     protected NameParser JavaDoc getNameParser()
357         throws NamingException JavaDoc
358     {
359         if( null == m_nameParser )
360         {
361             //Make sure provider is valid and returns nameparser
362
try
363             {
364                 m_nameParser = getProvider().getNameParser();
365             }
366             catch( final Exception JavaDoc e )
367             {
368                 throw handleException( e );
369             }
370
371         }
372         return m_nameParser;
373     }
374
375     protected Name JavaDoc getAbsoluteName( final Name JavaDoc name )
376         throws NamingException JavaDoc
377     {
378         return composeName( name, m_baseName );
379     }
380
381     protected NamingException JavaDoc handleException( final Exception JavaDoc e )
382     {
383         if( e instanceof NamingException JavaDoc )
384         {
385             return (NamingException JavaDoc)e;
386         }
387         else
388         {
389             return new CommunicationException JavaDoc( e.toString() );
390         }
391     }
392 }
393
Popular Tags