KickJava   Java API By Example, From Geeks To Geeks.

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


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.CompositeName JavaDoc;
12 import javax.naming.Context JavaDoc;
13 import javax.naming.InvalidNameException JavaDoc;
14 import javax.naming.Name JavaDoc;
15 import javax.naming.NameParser JavaDoc;
16 import javax.naming.NamingEnumeration JavaDoc;
17 import javax.naming.NamingException JavaDoc;
18 import javax.naming.spi.ResolveResult JavaDoc;
19
20 /**
21  * Abstract JNDI Context that can be inherited from to
22  * provide a particular type of Context.
23  *
24  * @author Peter Donald
25  * @version $Revision: 1.1 $
26  */

27 public abstract class AbstractURLContext
28     extends AbstractContext
29     implements NameParser JavaDoc
30 {
31     private final String JavaDoc m_scheme;
32
33     public AbstractURLContext( final String JavaDoc scheme, final Hashtable JavaDoc environment )
34     {
35         super( environment );
36         m_scheme = scheme;
37     }
38
39     public Name JavaDoc parse( final String JavaDoc name )
40         throws NamingException JavaDoc
41     {
42         return ( new CompositeName JavaDoc().add( name ) );
43     }
44
45     protected NameParser JavaDoc getNameParser()
46         throws NamingException JavaDoc
47     {
48         return this;
49     }
50
51     /**
52      * Helper method to bind
53      */

54     protected void bind( final Name JavaDoc name, final Object JavaDoc object, final boolean rebind )
55         throws NamingException JavaDoc
56     {
57         final ResolveResult JavaDoc resolveResult = getBaseURLContext( name, getRawEnvironment() );
58         final Context JavaDoc context = (Context JavaDoc)resolveResult.getResolvedObj();
59
60         try
61         {
62             if( rebind )
63             {
64                 context.rebind( resolveResult.getRemainingName(), object );
65             }
66             else
67             {
68                 context.bind( resolveResult.getRemainingName(), object );
69             }
70         }
71         finally
72         {
73             context.close();
74         }
75     }
76
77     /**
78      * Create a Subcontext.
79      *
80      * @param name the name of subcontext
81      * @return the created context
82      * @throws NamingException if an error occurs (ie context
83      * exists, badly formated name etc)
84      */

85     public Context JavaDoc createSubcontext( final Name JavaDoc name )
86         throws NamingException JavaDoc
87     {
88         final ResolveResult JavaDoc resolveResult = getBaseURLContext( name, getRawEnvironment() );
89         final Context JavaDoc context = (Context JavaDoc)resolveResult.getResolvedObj();
90
91         try
92         {
93             return context.createSubcontext( resolveResult.getRemainingName() );
94         }
95         finally
96         {
97             context.close();
98         }
99     }
100
101     public void destroySubcontext( final Name JavaDoc name )
102         throws NamingException JavaDoc
103     {
104         final ResolveResult JavaDoc resolveResult = getBaseURLContext( name, getRawEnvironment() );
105         final Context JavaDoc context = (Context JavaDoc)resolveResult.getResolvedObj();
106
107         try
108         {
109             context.destroySubcontext( resolveResult.getRemainingName() );
110         }
111         finally
112         {
113             context.close();
114         }
115     }
116
117     public String JavaDoc getNameInNamespace()
118         throws NamingException JavaDoc
119     {
120         return "";
121     }
122
123     /**
124      * Enumerates the names bound in the named context.
125      *
126      * @param name the name of the context
127      * @return the enumeration
128      * @throws javax.naming.NamingException if an error occurs
129      */

130     public NamingEnumeration JavaDoc list( final Name JavaDoc name )
131         throws NamingException JavaDoc
132     {
133         final ResolveResult JavaDoc resolveResult = getBaseURLContext( name, getRawEnvironment() );
134         final Context JavaDoc context = (Context JavaDoc)resolveResult.getResolvedObj();
135
136         try
137         {
138             return context.list( resolveResult.getRemainingName() );
139         }
140         finally
141         {
142             context.close();
143         }
144     }
145
146     /**
147      * Enumerates the names bound in the named context, along with the objects bound to them.
148      *
149      * @param name the name of the context
150      * @return the enumeration
151      * @throws javax.naming.NamingException if an error occurs
152      */

153     public NamingEnumeration JavaDoc listBindings( final Name JavaDoc name )
154         throws NamingException JavaDoc
155     {
156         final ResolveResult JavaDoc resolveResult = getBaseURLContext( name, getRawEnvironment() );
157         final Context JavaDoc context = (Context JavaDoc)resolveResult.getResolvedObj();
158
159         try
160         {
161             return context.listBindings( resolveResult.getRemainingName() );
162         }
163         finally
164         {
165             context.close();
166         }
167     }
168
169     /**
170      * Get the object named.
171      *
172      * @param name the name
173      * @return the object
174      * @throws NamingException if an error occurs (ie object
175      * name is inavlid or unbound)
176      */

177     public Object JavaDoc lookup( final Name JavaDoc name )
178         throws NamingException JavaDoc
179     {
180         final ResolveResult JavaDoc resolveResult = getBaseURLContext( name, getRawEnvironment() );
181         final Context JavaDoc context = (Context JavaDoc)resolveResult.getResolvedObj();
182
183         try
184         {
185             return context.lookup( resolveResult.getRemainingName() );
186         }
187         finally
188         {
189             context.close();
190         }
191     }
192
193     /**
194      * Unbind a object from a name.
195      *
196      * @param name the name
197      * @throws javax.naming.NamingException if an error occurs
198      */

199     public void unbind( final Name JavaDoc name )
200         throws NamingException JavaDoc
201     {
202         final ResolveResult JavaDoc resolveResult = getBaseURLContext( name, getRawEnvironment() );
203         final Context JavaDoc context = (Context JavaDoc)resolveResult.getResolvedObj();
204
205         try
206         {
207             context.unbind( resolveResult.getRemainingName() );
208         }
209         finally
210         {
211             context.close();
212         }
213     }
214
215     protected ResolveResult JavaDoc getBaseURLContext( final Name JavaDoc name, final Hashtable JavaDoc environment )
216         throws NamingException JavaDoc
217     {
218         if( name.isEmpty() )
219         {
220             throw new InvalidNameException JavaDoc( "Unable to locate URLContext will empty name" );
221         }
222
223         final String JavaDoc nameString = name.get(0).toString();
224         int index = nameString.indexOf( ':' );
225
226         if( -1 == index )
227         {
228             final String JavaDoc explanation =
229                 "Unable to build URLContext as it does not specify scheme";
230             throw new InvalidNameException JavaDoc( explanation );
231         }
232
233         final String JavaDoc scheme = nameString.substring( 0, index );
234         final int end = getEndIndexOfURLPart( nameString, index + 1 );
235         final String JavaDoc urlPart = nameString.substring( index + 1, end );
236         final String JavaDoc namePart = nameString.substring( end );
237
238         if( !m_scheme.equals( scheme ) )
239         {
240             final String JavaDoc explanation =
241                 "Bad Scheme use to build URLContext (" +
242                 scheme + "). " + "Expected " + m_scheme;
243             throw new InvalidNameException JavaDoc( explanation );
244         }
245
246         final Context JavaDoc context = newContext( urlPart );
247
248         return new ResolveResult JavaDoc( context, new CompositeName JavaDoc( namePart ) );
249     }
250
251     /**
252      * Find end index of url part in string.
253      * Default implementation looks for
254      * //.../[name-part]
255      * ///[name-part]
256      * //... (no name part)
257      * [name-part]
258      *
259      * @param name the name
260      * @param index the index where "scheme:" ends
261      * @return the index where url ends
262      * @throws javax.naming.NamingException if an error occurs
263      */

264     protected int getEndIndexOfURLPart( final String JavaDoc name, final int index )
265         throws NamingException JavaDoc
266     {
267         int result = 0;
268
269         //does it start with //
270
if( name.startsWith( "//", index ) )
271         {
272             //does it have .../ following ???
273
int end = name.indexOf( "/", index + 2 );
274
275             if( -1 != end )
276             {
277                 result = end;
278             }
279             else
280             {
281                 result = name.length();
282             }
283         }
284
285         return result;
286     }
287
288     /**
289      * Return a new instance of the base context for a URL.
290      * This must be implemented in particular URLContext.
291      *
292      * @param urlPart the part of url string not including "scheme:"
293      * @return a base URLContext for urlPart
294      * @throws javax.naming.NamingException if an error occurs
295      */

296     protected abstract Context JavaDoc newContext( String JavaDoc urlPart )
297         throws NamingException JavaDoc;
298 }
299
Popular Tags