KickJava   Java API By Example, From Geeks To Geeks.

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


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.CompositeName JavaDoc;
55 import javax.naming.Context JavaDoc;
56 import javax.naming.InvalidNameException JavaDoc;
57 import javax.naming.Name JavaDoc;
58 import javax.naming.NameParser JavaDoc;
59 import javax.naming.NamingEnumeration JavaDoc;
60 import javax.naming.NamingException JavaDoc;
61 import javax.naming.spi.ResolveResult JavaDoc;
62
63 /**
64  * Abstract JNDI Context that can be inherited from to
65  * provide a particular type of Context.
66  *
67  * @author <a HREF="mailto:peter@apache.org">Peter Donald</a>
68  * @version $Revision: 1.1 $
69  * @deprecated Toolkit deprecated and replaced by http://spice.sourceforge.net/jndikit/
70  */

71 public abstract class AbstractURLContext
72     extends AbstractContext
73     implements NameParser JavaDoc
74 {
75     protected final String JavaDoc m_scheme;
76
77     public AbstractURLContext( final String JavaDoc scheme, final Hashtable JavaDoc environment )
78     {
79         super( environment );
80         m_scheme = scheme;
81     }
82
83     public Name JavaDoc parse( final String JavaDoc name )
84         throws NamingException JavaDoc
85     {
86         return ( new CompositeName JavaDoc().add( name ) );
87     }
88
89     protected NameParser JavaDoc getNameParser()
90         throws NamingException JavaDoc
91     {
92         return this;
93     }
94
95     /**
96      * Helper method to bind
97      */

98     protected void bind( final Name JavaDoc name, final Object JavaDoc object, final boolean rebind )
99         throws NamingException JavaDoc
100     {
101         final ResolveResult JavaDoc resolveResult = getBaseURLContext( name, getRawEnvironment() );
102         final Context JavaDoc context = (Context JavaDoc)resolveResult.getResolvedObj();
103
104         try
105         {
106             if( rebind )
107             {
108                 context.rebind( resolveResult.getRemainingName(), object );
109             }
110             else
111             {
112                 context.bind( resolveResult.getRemainingName(), object );
113             }
114         }
115         finally
116         {
117             context.close();
118         }
119     }
120
121     /**
122      * Create a Subcontext.
123      *
124      * @param name the name of subcontext
125      * @return the created context
126      * @exception NamingException if an error occurs (ie context exists, badly formated name etc)
127      */

128     public Context JavaDoc createSubcontext( final Name JavaDoc name )
129         throws NamingException JavaDoc
130     {
131         final ResolveResult JavaDoc resolveResult = getBaseURLContext( name, getRawEnvironment() );
132         final Context JavaDoc context = (Context JavaDoc)resolveResult.getResolvedObj();
133
134         try
135         {
136             return context.createSubcontext( resolveResult.getRemainingName() );
137         }
138         finally
139         {
140             context.close();
141         }
142     }
143
144     public void destroySubcontext( final Name JavaDoc name )
145         throws NamingException JavaDoc
146     {
147         final ResolveResult JavaDoc resolveResult = getBaseURLContext( name, getRawEnvironment() );
148         final Context JavaDoc context = (Context JavaDoc)resolveResult.getResolvedObj();
149
150         try
151         {
152             context.destroySubcontext( resolveResult.getRemainingName() );
153         }
154         finally
155         {
156             context.close();
157         }
158     }
159
160     public String JavaDoc getNameInNamespace()
161         throws NamingException JavaDoc
162     {
163         return "";
164     }
165
166     /**
167      * Enumerates the names bound in the named context.
168      *
169      * @param name the name of the context
170      * @return the enumeration
171      * @exception NamingException if an error occurs
172      */

173     public NamingEnumeration JavaDoc list( final Name JavaDoc name )
174         throws NamingException JavaDoc
175     {
176         final ResolveResult JavaDoc resolveResult = getBaseURLContext( name, getRawEnvironment() );
177         final Context JavaDoc context = (Context JavaDoc)resolveResult.getResolvedObj();
178
179         try
180         {
181             return context.list( resolveResult.getRemainingName() );
182         }
183         finally
184         {
185             context.close();
186         }
187     }
188
189     /**
190      * Enumerates the names bound in the named context, along with the objects bound to them.
191      *
192      * @param name the name of the context
193      * @return the enumeration
194      * @exception NamingException if an error occurs
195      */

196     public NamingEnumeration JavaDoc listBindings( final Name JavaDoc name )
197         throws NamingException JavaDoc
198     {
199         final ResolveResult JavaDoc resolveResult = getBaseURLContext( name, getRawEnvironment() );
200         final Context JavaDoc context = (Context JavaDoc)resolveResult.getResolvedObj();
201
202         try
203         {
204             return context.listBindings( resolveResult.getRemainingName() );
205         }
206         finally
207         {
208             context.close();
209         }
210     }
211
212     /**
213      * Get the object named.
214      *
215      * @param name the name
216      * @return the object
217      * @exception NamingException if an error occurs (ie object name is inavlid or unbound)
218      */

219     public Object JavaDoc lookup( final Name JavaDoc name )
220         throws NamingException JavaDoc
221     {
222         final ResolveResult JavaDoc resolveResult = getBaseURLContext( name, getRawEnvironment() );
223         final Context JavaDoc context = (Context JavaDoc)resolveResult.getResolvedObj();
224
225         try
226         {
227             return context.lookup( resolveResult.getRemainingName() );
228         }
229         finally
230         {
231             context.close();
232         }
233     }
234
235     /**
236      * Unbind a object from a name.
237      *
238      * @param name the name
239      * @exception NamingException if an error occurs
240      */

241     public void unbind( final Name JavaDoc name )
242         throws NamingException JavaDoc
243     {
244         final ResolveResult JavaDoc resolveResult = getBaseURLContext( name, getRawEnvironment() );
245         final Context JavaDoc context = (Context JavaDoc)resolveResult.getResolvedObj();
246
247         try
248         {
249             context.unbind( resolveResult.getRemainingName() );
250         }
251         finally
252         {
253             context.close();
254         }
255     }
256
257     protected ResolveResult JavaDoc getBaseURLContext( final Name JavaDoc name, final Hashtable JavaDoc environment )
258         throws NamingException JavaDoc
259     {
260         if( name.isEmpty() )
261         {
262             throw new InvalidNameException JavaDoc( "Unable to locate URLContext will empty name" );
263         }
264
265         final String JavaDoc nameString = name.toString();
266         int index = nameString.indexOf( ':' );
267
268         if( -1 == index )
269         {
270             throw new InvalidNameException JavaDoc( "Unable to build URLContext as it does not specify scheme" );
271         }
272
273         final String JavaDoc scheme = nameString.substring( 0, index );
274         final int end = getEndIndexOfURLPart( nameString, index + 1 );
275         final String JavaDoc urlPart = nameString.substring( index + 1, end );
276         final String JavaDoc namePart = nameString.substring( end );
277
278         if( !m_scheme.equals( scheme ) )
279         {
280             throw new InvalidNameException JavaDoc( "Bad Scheme use to build URLContext (" + scheme + "). " +
281                                             "Expected " + m_scheme );
282         }
283
284         final Context JavaDoc context = newContext( urlPart );
285
286         return new ResolveResult JavaDoc( context, new CompositeName JavaDoc( namePart ) );
287     }
288
289     /**
290      * Find end index of url part in string.
291      * Default implementation looks for
292      * //.../[name-part]
293      * ///[name-part]
294      * //... (no name part)
295      * [name-part]
296      *
297      * @param name the name
298      * @param index the index where "scheme:" ends
299      * @return the index where url ends
300      * @exception NamingException if an error occurs
301      */

302     protected int getEndIndexOfURLPart( final String JavaDoc name, final int index )
303         throws NamingException JavaDoc
304     {
305         int result = 0;
306
307         //does it start with //
308
if( name.startsWith( "//", index ) )
309         {
310             //does it have .../ following ???
311
int end = name.indexOf( "/", index + 2 );
312
313             if( -1 != end )
314             {
315                 result = end;
316             }
317             else
318             {
319                 result = name.length();
320             }
321         }
322
323         return result;
324     }
325
326     /**
327      * Return a new instance of the base context for a URL.
328      * This must be implemented in particular URLContext.
329      *
330      * @param urlPart the part of url string not including "scheme:"
331      * @return a base URLContext for urlPart
332      * @exception NamingException if an error occurs
333      */

334     protected abstract Context JavaDoc newContext( String JavaDoc urlPart )
335         throws NamingException JavaDoc;
336 }
337
Popular Tags