KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > naming > pkg > java > JavaURLContext


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: JavaURLContext.java 370 2006-04-21 08:19:20Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.easybeans.naming.pkg.java;
26
27 import java.util.Hashtable JavaDoc;
28
29 import javax.naming.Binding JavaDoc;
30 import javax.naming.Context JavaDoc;
31 import javax.naming.Name JavaDoc;
32 import javax.naming.NameClassPair JavaDoc;
33 import javax.naming.NameNotFoundException JavaDoc;
34 import javax.naming.NameParser JavaDoc;
35 import javax.naming.NamingEnumeration JavaDoc;
36 import javax.naming.NamingException JavaDoc;
37
38 import org.objectweb.easybeans.log.JLog;
39 import org.objectweb.easybeans.log.JLogFactory;
40 import org.objectweb.easybeans.naming.NamingManager;
41
42 /**
43  * Context implementation for the "java:" namespace. <br>
44  * @author Florent Benoit
45  */

46 public class JavaURLContext implements Context JavaDoc {
47
48     /**
49      * java: prefix.
50      */

51     private static final String JavaDoc JAVA_PREFIX = "java:";
52
53     /**
54      * Reference on the naming manager.
55      */

56     private static NamingManager namingManager = null;
57
58     /**
59      * Logger.
60      */

61     private static JLog logger = JLogFactory.getLog(JavaURLContext.class);
62
63     /**
64      * Constructor.
65      * @throws NamingException if naming manager is not found.
66      */

67     public JavaURLContext() throws NamingException JavaDoc {
68         if (namingManager == null) {
69             namingManager = NamingManager.getInstance();
70         }
71     }
72
73     /**
74      * Get name without the url prefix.
75      * @param name the absolute name.
76      * @return the relative name (without prefix).
77      * @throws NamingException if the naming failed.
78      */

79     private String JavaDoc getRelativeName(final String JavaDoc name) throws NamingException JavaDoc {
80         String JavaDoc newName = name;
81         // We suppose that all names must be prefixed as this
82
if (!name.startsWith(JAVA_PREFIX)) {
83             logger.error("relative name! {0}" + name);
84             throw new NameNotFoundException JavaDoc("Invalid name:" + name);
85         }
86         if (name.endsWith("/")) {
87             newName = name.substring(JAVA_PREFIX.length() + 1);
88         } else {
89             newName = name.substring(JAVA_PREFIX.length());
90         }
91
92         return newName;
93     }
94
95     /**
96      * Get name without the url prefix.
97      * @param name the absolute name.
98      * @return the relative name (without prefix).
99      * @throws NamingException if the naming failed.
100      */

101     private Name JavaDoc getRelativeName(final Name JavaDoc name) throws NamingException JavaDoc {
102         if (name.get(0).equals(JAVA_PREFIX)) {
103             return (name.getSuffix(1));
104         }
105         throw new NameNotFoundException JavaDoc("Invalid name:" + name);
106     }
107
108     /**
109      * Retrieves the named object.
110      * @param name the name of the object to look up
111      * @return the object bound to name
112      * @throws NamingException if a naming exception is encountered
113      */

114     public Object JavaDoc lookup(final Name JavaDoc name) throws NamingException JavaDoc {
115         return findContext().lookup(getRelativeName(name));
116     }
117
118     /**
119      * Retrieves the named object.
120      * @param name the name of the object to look up
121      * @return the object bound to name
122      * @throws NamingException if a naming exception is encountered
123      */

124     public Object JavaDoc lookup(final String JavaDoc name) throws NamingException JavaDoc {
125         return findContext().lookup(getRelativeName(name));
126     }
127
128     /**
129      * Binds a name to an object.
130      * @param name the name to bind; may not be empty
131      * @param obj the object to bind; possibly null
132      * @throws NamingException if a naming exception is encountered
133      */

134     public void bind(final Name JavaDoc name, final Object JavaDoc obj) throws NamingException JavaDoc {
135         findContext().bind(getRelativeName(name), obj);
136     }
137
138     /**
139      * Binds a name to an object. All intermediate contexts and the target
140      * context (that named by all but terminal atomic component of the name)
141      * must already exist.
142      * @param name the name to bind; may not be empty
143      * @param obj the object to bind; possibly null
144      * @throws NamingException if a naming exception is encountered
145      */

146     public void bind(final String JavaDoc name, final Object JavaDoc obj) throws NamingException JavaDoc {
147         findContext().bind(getRelativeName(name), obj);
148     }
149
150     /**
151      * Binds a name to an object, overwriting any existing binding. All
152      * intermediate contexts and the target context (that named by all but
153      * terminal atomic component of the name) must already exist. If the object
154      * is a DirContext, any existing attributes associated with the name are
155      * replaced with those of the object. Otherwise, any existing attributes
156      * associated with the name remain unchanged.
157      * @param name the name to bind; may not be empty
158      * @param obj the object to bind; possibly null
159      * @throws NamingException if a naming exception is encountered
160      */

161     public void rebind(final Name JavaDoc name, final Object JavaDoc obj) throws NamingException JavaDoc {
162         findContext().rebind(getRelativeName(name), obj);
163     }
164
165     /**
166      * Binds a name to an object, overwriting any existing binding. See
167      * {@link #rebind(Name, Object)}for details.
168      * @param name the name to bind; may not be empty
169      * @param obj the object to bind; possibly null
170      * @throws NamingException if a naming exception is encountered
171      */

172     public void rebind(final String JavaDoc name, final Object JavaDoc obj) throws NamingException JavaDoc {
173         findContext().rebind(getRelativeName(name), obj);
174     }
175
176     /**
177      * Unbinds the named object. Removes the terminal atomic name in name from
178      * the target context--that named by all but the terminal atomic part of
179      * name. This method is idempotent. It succeeds even if the terminal atomic
180      * name is not bound in the target context, but throws NameNotFoundException
181      * if any of the intermediate contexts do not exist. Any attributes
182      * associated with the name are removed. Intermediate contexts are not
183      * changed.
184      * @param name the name to unbind; may not be empty
185      * @throws NamingException if a naming exception is encountered
186      * @see #unbind(String)
187      */

188     public void unbind(final Name JavaDoc name) throws NamingException JavaDoc {
189         findContext().unbind(getRelativeName(name));
190     }
191
192     /**
193      * Unbinds the named object. See {@link #unbind(Name)}for details.
194      * @param name the name to unbind; may not be empty
195      * @throws NamingException if a naming exception is encountered
196      */

197     public void unbind(final String JavaDoc name) throws NamingException JavaDoc {
198         findContext().unbind(getRelativeName(name));
199     }
200
201     /**
202      * Binds a new name to the object bound to an old name, and unbinds the old
203      * name. This operation is not supported (read only env.)
204      * @param oldName the name of the existing binding; may not be empty
205      * @param newName the name of the new binding; may not be empty
206      * @throws NamingException if a naming exception is encountered
207      */

208     public void rename(final Name JavaDoc oldName, final Name JavaDoc newName) throws NamingException JavaDoc {
209         findContext().rename(getRelativeName(oldName), getRelativeName(newName));
210     }
211
212     /**
213      * Binds a new name to the object bound to an old name, and unbinds the old
214      * name. Not supported.
215      * @param oldName the name of the existing binding; may not be empty
216      * @param newName the name of the new binding; may not be empty
217      * @throws NamingException if a naming exception is encountered
218      */

219     public void rename(final String JavaDoc oldName, final String JavaDoc newName) throws NamingException JavaDoc {
220         findContext().rename(getRelativeName(oldName), getRelativeName(newName));
221     }
222
223     /**
224      * Enumerates the names bound in the named context, along with the class
225      * names of objects bound to them. The contents of any subcontexts are not
226      * included. If a binding is added to or removed from this context, its
227      * effect on an enumeration previously returned is undefined.
228      * @param name the name of the context to list
229      * @return an enumeration of the names and class names of the bindings in
230      * this context. Each element of the enumeration is of type
231      * NameClassPair.
232      * @throws NamingException if a naming exception is encountered
233      */

234     public NamingEnumeration JavaDoc<NameClassPair JavaDoc> list(final Name JavaDoc name) throws NamingException JavaDoc {
235         return findContext().list(getRelativeName(name));
236     }
237
238     /**
239      * Enumerates the names bound in the named context, along with the class
240      * names of objects bound to them. See {@link #list(Name)}for details.
241      * @param name the name of the context to list
242      * @return an enumeration of the names and class names of the bindings in
243      * this context. Each element of the enumeration is of type
244      * NameClassPair.
245      * @throws NamingException if a naming exception is encountered
246      */

247     public NamingEnumeration JavaDoc<NameClassPair JavaDoc> list(final String JavaDoc name) throws NamingException JavaDoc {
248         return findContext().list(getRelativeName(name));
249     }
250
251     /**
252      * Enumerates the names bound in the named context, along with the objects
253      * bound to them. The contents of any subcontexts are not included. If a
254      * binding is added to or removed from this context, its effect on an
255      * enumeration previously returned is undefined.
256      * @param name the name of the context to list
257      * @return an enumeration of the bindings in this context. Each element of
258      * the enumeration is of type Binding.
259      * @throws NamingException if a naming exception is encountered
260      */

261     public NamingEnumeration JavaDoc<Binding JavaDoc> listBindings(final Name JavaDoc name) throws NamingException JavaDoc {
262         return findContext().listBindings(getRelativeName(name));
263     }
264
265     /**
266      * Enumerates the names bound in the named context, along with the objects
267      * bound to them. See {@link #listBindings(Name)}for details.
268      * @param name the name of the context to list
269      * @return an enumeration of the bindings in this context. Each element of
270      * the enumeration is of type Binding.
271      * @throws NamingException if a naming exception is encountered
272      */

273     public NamingEnumeration JavaDoc<Binding JavaDoc> listBindings(final String JavaDoc name) throws NamingException JavaDoc {
274         return findContext().listBindings(getRelativeName(name));
275     }
276
277     /**
278      * Destroys the named context and removes it from the namespace. Any
279      * attributes associated with the name are also removed. Intermediate
280      * contexts are not destroyed. This method is idempotent. It succeeds even
281      * if the terminal atomic name is not bound in the target context, but
282      * throws NameNotFoundException if any of the intermediate contexts do not
283      * exist. In a federated naming system, a context from one naming system may
284      * be bound to a name in another. One can subsequently look up and perform
285      * operations on the foreign context using a composite name. However, an
286      * attempt destroy the context using this composite name will fail with
287      * NotContextException, because the foreign context is not a "subcontext" of
288      * the context in which it is bound. Instead, use unbind() to remove the
289      * binding of the foreign context. Destroying the foreign context requires
290      * that the destroySubcontext() be performed on a context from the foreign
291      * context's "native" naming system.
292      * @param name the name of the context to be destroyed; may not be empty
293      * @throws NamingException if a naming exception is encountered
294      */

295     public void destroySubcontext(final Name JavaDoc name) throws NamingException JavaDoc {
296         findContext().destroySubcontext(getRelativeName(name));
297     }
298
299     /**
300      * Destroys the named context and removes it from the namespace. See
301      * {@link #destroySubcontext(Name)}for details.
302      * @param name the name of the context to be destroyed; may not be empty
303      * @throws NamingException if a naming exception is encountered
304      */

305     public void destroySubcontext(final String JavaDoc name) throws NamingException JavaDoc {
306         findContext().destroySubcontext(getRelativeName(name));
307     }
308
309     /**
310      * Creates and binds a new context. Creates a new context with the given
311      * name and binds it in the target context (that named by all but terminal
312      * atomic component of the name). All intermediate contexts and the target
313      * context must already exist.
314      * @param name the name of the context to create; may not be empty
315      * @return the newly created context
316      * @throws NamingException if a naming exception is encountered
317      */

318     public Context JavaDoc createSubcontext(final Name JavaDoc name) throws NamingException JavaDoc {
319         return findContext().createSubcontext(getRelativeName(name));
320     }
321
322     /**
323      * Creates and binds a new context. See {@link #createSubcontext(Name)}for
324      * details.
325      * @param name the name of the context to create; may not be empty
326      * @return the newly created context
327      * @throws NamingException if a naming exception is encountered
328      */

329     public Context JavaDoc createSubcontext(final String JavaDoc name) throws NamingException JavaDoc {
330         return findContext().createSubcontext(getRelativeName(name));
331     }
332
333     /**
334      * Retrieves the named object, following links except for the terminal
335      * atomic component of the name. If the object bound to name is not a link,
336      * returns the object itself.
337      * @param name the name of the object to look up
338      * @return the object bound to name, not following the terminal link (if
339      * any).
340      * @throws NamingException if a naming exception is encountered
341      */

342     public Object JavaDoc lookupLink(final Name JavaDoc name) throws NamingException JavaDoc {
343         return findContext().lookupLink(getRelativeName(name));
344     }
345
346     /**
347      * Retrieves the named object, following links except for the terminal
348      * atomic component of the name. See {@link #lookupLink(Name)}for details.
349      * @param name the name of the object to look up
350      * @return the object bound to name, not following the terminal link (if
351      * any)
352      * @throws NamingException if a naming exception is encountered
353      */

354     public Object JavaDoc lookupLink(final String JavaDoc name) throws NamingException JavaDoc {
355         return findContext().lookupLink(getRelativeName(name));
356     }
357
358     /**
359      * Retrieves the parser associated with the named context. In a federation
360      * of namespaces, different naming systems will parse names differently.
361      * This method allows an application to get a parser for parsing names into
362      * their atomic components using the naming convention of a particular
363      * naming system. Within any single naming system, NameParser objects
364      * returned by this method must be equal (using the equals() test).
365      * @param name the name of the context from which to get the parser
366      * @return a name parser that can parse compound names into their atomic
367      * components
368      * @throws NamingException if a naming exception is encountered
369      */

370     public NameParser JavaDoc getNameParser(final Name JavaDoc name) throws NamingException JavaDoc {
371         return findContext().getNameParser(getRelativeName(name));
372     }
373
374     /**
375      * Retrieves the parser associated with the named context. See
376      * {@link #getNameParser(Name)}for details.
377      * @param name the name of the context from which to get the parser
378      * @return a name parser that can parse compound names into their atomic
379      * components
380      * @throws NamingException if a naming exception is encountered
381      */

382     public NameParser JavaDoc getNameParser(final String JavaDoc name) throws NamingException JavaDoc {
383         return findContext().getNameParser(getRelativeName(name));
384     }
385
386     /**
387      * Composes the name of this context with a name relative to this context.
388      * @param name a name relative to this context
389      * @param prefix the name of this context relative to one of its ancestors
390      * @return the composition of prefix and name
391      * @throws NamingException if a naming exception is encountered
392      */

393     public Name JavaDoc composeName(final Name JavaDoc name, final Name JavaDoc prefix) throws NamingException JavaDoc {
394         Name JavaDoc newPrefix = (Name JavaDoc) name.clone();
395         return newPrefix.addAll(name);
396     }
397
398     /**
399      * Composes the name of this context with a name relative to this context.
400      * @param name a name relative to this context
401      * @param prefix the name of this context relative to one of its ancestors
402      * @return the composition of prefix and name
403      * @throws NamingException if a naming exception is encountered
404      */

405     public String JavaDoc composeName(final String JavaDoc name, final String JavaDoc prefix) throws NamingException JavaDoc {
406         return prefix + "/" + name;
407     }
408
409     /**
410      * Adds a new environment property to the environment of this context. If
411      * the property already exists, its value is overwritten. See class
412      * description for more details on environment properties.
413      * @param propName the name of the environment property to add; may not be
414      * null
415      * @param propVal the value of the property to add; may not be null
416      * @return the previous value of the property, or null if the property was
417      * not in the environment before
418      * @throws NamingException if a naming exception is encountered
419      */

420     public Object JavaDoc addToEnvironment(final String JavaDoc propName, final Object JavaDoc propVal) throws NamingException JavaDoc {
421         return findContext().addToEnvironment(propName, propVal);
422     }
423
424     /**
425      * Removes an environment property from the environment of this context. See
426      * class description for more details on environment properties.
427      * @param propName the name of the environment property to remove; may not
428      * be null
429      * @return the previous value of the property, or null if the property was
430      * not in the environment
431      * @throws NamingException if a naming exception is encountered
432      */

433     public Object JavaDoc removeFromEnvironment(final String JavaDoc propName) throws NamingException JavaDoc {
434         return findContext().removeFromEnvironment(propName);
435     }
436
437     /**
438      * Retrieves the environment in effect for this context. See class
439      * description for more details on environment properties. The caller should
440      * not make any changes to the object returned: their effect on the context
441      * is undefined. The environment of this context may be changed using
442      * addToEnvironment() and removeFromEnvironment().
443      * @return the environment of this context; never null
444      * @throws NamingException if a naming exception is encountered
445      */

446     public Hashtable JavaDoc<?, ?> getEnvironment() throws NamingException JavaDoc {
447         return findContext().getEnvironment();
448     }
449
450     /**
451      * Closes this context. This method releases this context's resources
452      * immediately, instead of waiting for them to be released automatically by
453      * the garbage collector. This method is idempotent: invoking it on a
454      * context that has already been closed has no effect. Invoking any other
455      * method on a closed context is not allowed, and results in undefined
456      * behaviour.
457      * @throws NamingException if a naming exception is encountered
458      */

459     public void close() throws NamingException JavaDoc {
460         findContext().close();
461     }
462
463     /**
464      * Retrieves the full name of this context within its own namespace.
465      * @return this context's name in its own namespace; never null
466      * @throws NamingException if a naming exception is encountered
467      */

468     public String JavaDoc getNameInNamespace() throws NamingException JavaDoc {
469         return JAVA_PREFIX;
470     }
471
472     /**
473      * @return the Context associated with the current thread.
474      * @throws NamingException if no context is found.
475      */

476     public Context JavaDoc findContext() throws NamingException JavaDoc {
477       return namingManager.getComponentContext();
478     }
479
480
481 }
482
Popular Tags