KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > component > smartclient > spi > SmartContext


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:$
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.component.smartclient.spi;
27
28 import java.util.Hashtable JavaDoc;
29
30 import javax.naming.Binding JavaDoc;
31 import javax.naming.Context JavaDoc;
32 import javax.naming.Name JavaDoc;
33 import javax.naming.NameClassPair JavaDoc;
34 import javax.naming.NameParser JavaDoc;
35 import javax.naming.NamingEnumeration JavaDoc;
36 import javax.naming.NamingException JavaDoc;
37
38 /**
39  * Context that use a given classloader before calling every methods.
40  * @author Florent Benoit
41  */

42 public class SmartContext implements Context JavaDoc {
43
44     /**
45      * Context that is wrapped.
46      */

47     private Context JavaDoc wrapped;
48
49     /**
50      * Classloader to use for all methods.
51      */

52     private ClassLoader JavaDoc classLoader;
53
54     /**
55      * Creates a context with the given wrapped context and the given
56      * classloader.
57      * @param wrapped the context to wrap
58      * @param classLoader the classloader to use.
59      */

60     public SmartContext(final Context JavaDoc wrapped, final ClassLoader JavaDoc classLoader) {
61         this.wrapped = wrapped;
62         this.classLoader = classLoader;
63     }
64
65     /**
66      * Adds a new environment property to the environment of this context. If
67      * the property already exists, its value is overwritten.
68      * @param propName the name of the environment property to add; may not be
69      * null
70      * @param propVal the value of the property to add; may not be null
71      * @return the previous value of the property, or null if the property was
72      * not in the environment before
73      * @throws NamingException if a naming exception is encountered
74      */

75     public Object JavaDoc addToEnvironment(final String JavaDoc propName, final Object JavaDoc propVal) throws NamingException JavaDoc {
76         ClassLoader JavaDoc old = Thread.currentThread().getContextClassLoader();
77         Thread.currentThread().setContextClassLoader(classLoader);
78         try {
79             return wrapped.addToEnvironment(propName, propVal);
80         } finally {
81             Thread.currentThread().setContextClassLoader(old);
82         }
83     }
84
85     /**
86      * Binds a name to an object. Delegate to the String version.
87      * @param name the name to bind; may not be empty
88      * @param obj the object to bind; possibly null
89      * @throws NamingException if a naming exception is encountered
90      * @see javax.naming.NameAlreadyBoundException
91      */

92     public void bind(final Name JavaDoc name, final Object JavaDoc obj) throws NamingException JavaDoc {
93         ClassLoader JavaDoc old = Thread.currentThread().getContextClassLoader();
94         Thread.currentThread().setContextClassLoader(classLoader);
95         try {
96             wrapped.bind(name, obj);
97         } finally {
98             Thread.currentThread().setContextClassLoader(old);
99         }
100     }
101
102     /**
103      * Binds a name to an object.
104      * @param name the name to bind; may not be empty
105      * @param obj the object to bind; possibly null
106      * @throws NamingException if a naming exception is encountered
107      * @see javax.naming.NameAlreadyBoundException
108      */

109     public void bind(final String JavaDoc name, final Object JavaDoc obj) throws NamingException JavaDoc {
110         ClassLoader JavaDoc old = Thread.currentThread().getContextClassLoader();
111         Thread.currentThread().setContextClassLoader(classLoader);
112         try {
113             wrapped.bind(name, obj);
114         } finally {
115             Thread.currentThread().setContextClassLoader(old);
116         }
117     }
118
119     /**
120      * Closes this context.
121      * @throws NamingException if a naming exception is encountered
122      */

123     public void close() throws NamingException JavaDoc {
124         ClassLoader JavaDoc old = Thread.currentThread().getContextClassLoader();
125         Thread.currentThread().setContextClassLoader(classLoader);
126         try {
127             wrapped.close();
128         } finally {
129             Thread.currentThread().setContextClassLoader(old);
130         }
131     }
132
133     /**
134      * Composes the name of this context with a name relative to this context.
135      * @param name a name relative to this context
136      * @param prefix the name of this context relative to one of its ancestors
137      * @return the composition of prefix and name
138      * @throws NamingException if a naming exception is encountered
139      */

140     public Name JavaDoc composeName(final Name JavaDoc name, final Name JavaDoc prefix) throws NamingException JavaDoc {
141         ClassLoader JavaDoc old = Thread.currentThread().getContextClassLoader();
142         Thread.currentThread().setContextClassLoader(classLoader);
143         try {
144             return wrapped.composeName(name, prefix);
145         } finally {
146             Thread.currentThread().setContextClassLoader(old);
147         }
148     }
149
150     /**
151      * Composes the name of this context with a name relative to this context:
152      * Not supported.
153      * @param name a name relative to this context
154      * @param prefix the name of this context relative to one of its ancestors
155      * @return the composition of prefix and name
156      * @throws NamingException if a naming exception is encountered
157      */

158     public String JavaDoc composeName(final String JavaDoc name, final String JavaDoc prefix) throws NamingException JavaDoc {
159         ClassLoader JavaDoc old = Thread.currentThread().getContextClassLoader();
160         Thread.currentThread().setContextClassLoader(classLoader);
161         try {
162             return wrapped.composeName(name, prefix);
163         } finally {
164             Thread.currentThread().setContextClassLoader(old);
165         }
166     }
167
168     /**
169      * Creates and binds a new context. Creates a new context with the given
170      * name and binds it in the target context.
171      * @param name the name of the context to create; may not be empty
172      * @return the newly created context
173      * @throws NamingException if a naming exception is encountered
174      * @see javax.naming.NameAlreadyBoundException
175      */

176     public Context JavaDoc createSubcontext(final Name JavaDoc name) throws NamingException JavaDoc {
177         ClassLoader JavaDoc old = Thread.currentThread().getContextClassLoader();
178         Thread.currentThread().setContextClassLoader(classLoader);
179         try {
180             return wrapped.createSubcontext(name);
181         } finally {
182             Thread.currentThread().setContextClassLoader(old);
183         }
184     }
185
186     /**
187      * Creates and binds a new context.
188      * @param name the name of the context to create; may not be empty
189      * @return the newly created context
190      * @throws NamingException if a naming exception is encountered
191      * @see javax.naming.NameAlreadyBoundException
192      */

193     public Context JavaDoc createSubcontext(final String JavaDoc name) throws NamingException JavaDoc {
194         ClassLoader JavaDoc old = Thread.currentThread().getContextClassLoader();
195         Thread.currentThread().setContextClassLoader(classLoader);
196         try {
197             return wrapped.createSubcontext(name);
198         } finally {
199             Thread.currentThread().setContextClassLoader(old);
200         }
201
202     }
203
204     /**
205      * Destroys the named context and removes it from the namespace. Not
206      * supported yet.
207      * @param name the name of the context to be destroyed; may not be empty
208      * @throws NamingException if a naming exception is encountered
209      */

210     public void destroySubcontext(final Name JavaDoc name) throws NamingException JavaDoc {
211         ClassLoader JavaDoc old = Thread.currentThread().getContextClassLoader();
212         Thread.currentThread().setContextClassLoader(classLoader);
213         try {
214             wrapped.destroySubcontext(name);
215         } finally {
216             Thread.currentThread().setContextClassLoader(old);
217         }
218     }
219
220     /**
221      * Destroys the named context and removes it from the namespace. Not
222      * supported yet.
223      * @param name the name of the context to be destroyed; may not be empty
224      * @throws NamingException if a naming exception is encountered
225      */

226     public void destroySubcontext(final String JavaDoc name) throws NamingException JavaDoc {
227         ClassLoader JavaDoc old = Thread.currentThread().getContextClassLoader();
228         Thread.currentThread().setContextClassLoader(classLoader);
229         try {
230             wrapped.destroySubcontext(name);
231         } finally {
232             Thread.currentThread().setContextClassLoader(old);
233         }
234     }
235
236     /**
237      * Retrieves the environment in effect for this context.
238      * @return the environment of this context; never null
239      * @throws NamingException if a naming exception is encountered
240      */

241     public Hashtable JavaDoc<?, ?> getEnvironment() throws NamingException JavaDoc {
242         ClassLoader JavaDoc old = Thread.currentThread().getContextClassLoader();
243         Thread.currentThread().setContextClassLoader(classLoader);
244         try {
245             return wrapped.getEnvironment();
246         } finally {
247             Thread.currentThread().setContextClassLoader(old);
248         }
249     }
250
251     /**
252      * Retrieves the full name of this context within its own namespace.
253      * @return this context's name in its own namespace; never null
254      * @throws NamingException if it fails.
255      */

256     public String JavaDoc getNameInNamespace() throws NamingException JavaDoc {
257         ClassLoader JavaDoc old = Thread.currentThread().getContextClassLoader();
258         Thread.currentThread().setContextClassLoader(classLoader);
259         try {
260             return wrapped.getNameInNamespace();
261         } finally {
262             Thread.currentThread().setContextClassLoader(old);
263         }
264     }
265
266     /**
267      * Retrieves the parser associated with the named context.
268      * @param name the name of the context from which to get the parser
269      * @return a name parser that can parse compound names into their atomic
270      * components
271      * @throws NamingException if a naming exception is encountered
272      */

273     public NameParser JavaDoc getNameParser(final Name JavaDoc name) throws NamingException JavaDoc {
274         ClassLoader JavaDoc old = Thread.currentThread().getContextClassLoader();
275         Thread.currentThread().setContextClassLoader(classLoader);
276         try {
277             return wrapped.getNameParser(name);
278         } finally {
279             Thread.currentThread().setContextClassLoader(old);
280         }
281     }
282
283     /**
284      * Retrieves the parser associated with the named context.
285      * @param name the name of the context from which to get the parser
286      * @return a name parser that can parse compound names into their atomic
287      * components
288      * @throws NamingException if a naming exception is encountered
289      */

290     public NameParser JavaDoc getNameParser(final String JavaDoc name) throws NamingException JavaDoc {
291         ClassLoader JavaDoc old = Thread.currentThread().getContextClassLoader();
292         Thread.currentThread().setContextClassLoader(classLoader);
293         try {
294             return wrapped.getNameParser(name);
295         } finally {
296             Thread.currentThread().setContextClassLoader(old);
297         }
298
299     }
300
301     /**
302      * Enumerates the names bound in the named context, along with the class
303      * names of objects bound to them. The contents of any subcontexts are not
304      * included.
305      * @param name the name of the context to list
306      * @return an enumeration of the names and class names of the bindings in
307      * this context. Each element of the enumeration is of type
308      * NameClassPair.
309      * @throws NamingException if a naming exception is encountered
310      */

311     public NamingEnumeration JavaDoc<NameClassPair JavaDoc> list(final Name JavaDoc name) throws NamingException JavaDoc {
312         ClassLoader JavaDoc old = Thread.currentThread().getContextClassLoader();
313         Thread.currentThread().setContextClassLoader(classLoader);
314         try {
315             return wrapped.list(name);
316         } finally {
317             Thread.currentThread().setContextClassLoader(old);
318         }
319
320     }
321
322     /**
323      * Enumerates the names bound in the named context, along with the class
324      * names of objects bound to them.
325      * @param name the name of the context to list
326      * @return an enumeration of the names and class names of the bindings in
327      * this context. Each element of the enumeration is of type
328      * NameClassPair.
329      * @throws NamingException if a naming exception is encountered
330      */

331     public NamingEnumeration JavaDoc<NameClassPair JavaDoc> list(final String JavaDoc name) throws NamingException JavaDoc {
332         ClassLoader JavaDoc old = Thread.currentThread().getContextClassLoader();
333         Thread.currentThread().setContextClassLoader(classLoader);
334         try {
335             return wrapped.list(name);
336         } finally {
337             Thread.currentThread().setContextClassLoader(old);
338         }
339     }
340
341     /**
342      * Enumerates the names bound in the named context, along with the objects
343      * bound to them. The contents of any subcontexts are not included. If a
344      * binding is added to or removed from this context, its effect on an
345      * enumeration previously returned is undefined.
346      * @param name the name of the context to list
347      * @return an enumeration of the bindings in this context. Each element of
348      * the enumeration is of type Binding.
349      * @throws NamingException if a naming exception is encountered
350      */

351     public NamingEnumeration JavaDoc<Binding JavaDoc> listBindings(final Name JavaDoc name) throws NamingException JavaDoc {
352         ClassLoader JavaDoc old = Thread.currentThread().getContextClassLoader();
353         Thread.currentThread().setContextClassLoader(classLoader);
354         try {
355             return wrapped.listBindings(name);
356         } finally {
357             Thread.currentThread().setContextClassLoader(old);
358         }
359     }
360
361     /**
362      * Enumerates the names bound in the named context, along with the objects
363      * bound to them.
364      * @param name the name of the context to list
365      * @return an enumeration of the bindings in this context. Each element of
366      * the enumeration is of type Binding.
367      * @throws NamingException if a naming exception is encountered
368      */

369     public NamingEnumeration JavaDoc<Binding JavaDoc> listBindings(final String JavaDoc name) throws NamingException JavaDoc {
370         ClassLoader JavaDoc old = Thread.currentThread().getContextClassLoader();
371         Thread.currentThread().setContextClassLoader(classLoader);
372         try {
373             return wrapped.listBindings(name);
374         } finally {
375             Thread.currentThread().setContextClassLoader(old);
376         }
377     }
378
379     /**
380      * Retrieves the named object.
381      * @param name the name of the object to look up
382      * @return the object bound to <tt>name</tt>
383      * @throws NamingException if a naming exception is encountered
384      */

385     public Object JavaDoc lookup(final Name JavaDoc name) throws NamingException JavaDoc {
386         ClassLoader JavaDoc old = Thread.currentThread().getContextClassLoader();
387         Thread.currentThread().setContextClassLoader(classLoader);
388         try {
389             return wrapped.lookup(name);
390         } finally {
391             Thread.currentThread().setContextClassLoader(old);
392         }
393     }
394
395     /**
396      * Retrieves the named object.
397      * @param name the name of the object to look up
398      * @return the object bound to name
399      * @throws NamingException if a naming exception is encountered
400      */

401     public Object JavaDoc lookup(final String JavaDoc name) throws NamingException JavaDoc {
402         ClassLoader JavaDoc old = Thread.currentThread().getContextClassLoader();
403         Thread.currentThread().setContextClassLoader(classLoader);
404         try {
405             return wrapped.lookup(name);
406         } finally {
407             Thread.currentThread().setContextClassLoader(old);
408         }
409     }
410
411     /**
412      * Retrieves the named object, following links except for the terminal
413      * atomic component of the name. If the object bound to name is not a link,
414      * returns the object itself.
415      * @param name the name of the object to look up
416      * @return the object bound to name, not following the terminal link (if
417      * any).
418      * @throws NamingException if a naming exception is encountered
419      */

420     public Object JavaDoc lookupLink(final Name JavaDoc name) throws NamingException JavaDoc {
421         ClassLoader JavaDoc old = Thread.currentThread().getContextClassLoader();
422         Thread.currentThread().setContextClassLoader(classLoader);
423         try {
424             return wrapped.lookupLink(name);
425         } finally {
426             Thread.currentThread().setContextClassLoader(old);
427         }
428     }
429
430     /**
431      * Retrieves the named object, following links except for the terminal
432      * atomic component of the name. If the object bound to name is not a link,
433      * returns the object itself.
434      * @param name the name of the object to look up
435      * @return the object bound to name, not following the terminal link (if
436      * any)
437      * @throws NamingException if a naming exception is encountered
438      */

439     public Object JavaDoc lookupLink(final String JavaDoc name) throws NamingException JavaDoc {
440         ClassLoader JavaDoc old = Thread.currentThread().getContextClassLoader();
441         Thread.currentThread().setContextClassLoader(classLoader);
442         try {
443             return wrapped.lookupLink(name);
444         } finally {
445             Thread.currentThread().setContextClassLoader(old);
446         }
447     }
448
449     /**
450      * Binds a name to an object, overwriting any existing binding.
451      * @param name the name to bind; may not be empty
452      * @param obj the object to bind; possibly null
453      * @throws NamingException if a naming exception is encountered
454      */

455     public void rebind(final Name JavaDoc name, final Object JavaDoc obj) throws NamingException JavaDoc {
456         ClassLoader JavaDoc old = Thread.currentThread().getContextClassLoader();
457         Thread.currentThread().setContextClassLoader(classLoader);
458         try {
459             wrapped.rebind(name, obj);
460         } finally {
461             Thread.currentThread().setContextClassLoader(old);
462         }
463
464     }
465
466     /**
467      * Binds a name to an object, overwriting any existing binding.
468      * @param name the name to bind; may not be empty
469      * @param obj the object to bind; possibly null
470      * @throws NamingException if a naming exception is encountered
471      * @see javax.naming.InvalidNameException
472      */

473     public void rebind(final String JavaDoc name, final Object JavaDoc obj) throws NamingException JavaDoc {
474         ClassLoader JavaDoc old = Thread.currentThread().getContextClassLoader();
475         Thread.currentThread().setContextClassLoader(classLoader);
476         try {
477             wrapped.rebind(name, obj);
478         } finally {
479             Thread.currentThread().setContextClassLoader(old);
480         }
481     }
482
483     /**
484      * Removes an environment property from the environment of this context.
485      * @param propName the name of the environment property to remove; may not
486      * be null
487      * @return the previous value of the property, or null if the property was
488      * not in the environment
489      * @throws NamingException if a naming exception is encountered
490      */

491     public Object JavaDoc removeFromEnvironment(final String JavaDoc propName) throws NamingException JavaDoc {
492         ClassLoader JavaDoc old = Thread.currentThread().getContextClassLoader();
493         Thread.currentThread().setContextClassLoader(classLoader);
494         try {
495             return wrapped.removeFromEnvironment(propName);
496         } finally {
497             Thread.currentThread().setContextClassLoader(old);
498         }
499
500     }
501
502     /**
503      * Binds a new name to the object bound to an old name, and unbinds the old
504      * name.
505      * @param oldName the name of the existing binding; may not be empty
506      * @param newName the name of the new binding; may not be empty
507      * @throws NamingException if a naming exception is encountered
508      */

509     public void rename(final Name JavaDoc oldName, final Name JavaDoc newName) throws NamingException JavaDoc {
510         ClassLoader JavaDoc old = Thread.currentThread().getContextClassLoader();
511         Thread.currentThread().setContextClassLoader(classLoader);
512         try {
513             wrapped.rename(oldName, newName);
514         } finally {
515             Thread.currentThread().setContextClassLoader(old);
516         }
517     }
518
519     /**
520      * Binds a new name to the object bound to an old name, and unbinds the old
521      * name.
522      * @param oldName the name of the existing binding; may not be empty
523      * @param newName the name of the new binding; may not be empty
524      * @throws NamingException if a naming exception is encountered
525      */

526     public void rename(final String JavaDoc oldName, final String JavaDoc newName) throws NamingException JavaDoc {
527         ClassLoader JavaDoc old = Thread.currentThread().getContextClassLoader();
528         Thread.currentThread().setContextClassLoader(classLoader);
529         try {
530             wrapped.rename(oldName, newName);
531         } finally {
532             Thread.currentThread().setContextClassLoader(old);
533         }
534     }
535
536     /**
537      * Unbinds the named object.
538      * @param name the name to unbind; may not be empty
539      * @throws NamingException if a naming exception is encountered
540      * @see javax.naming.NameNotFoundException
541      */

542     public void unbind(final Name JavaDoc name) throws NamingException JavaDoc {
543         ClassLoader JavaDoc old = Thread.currentThread().getContextClassLoader();
544         Thread.currentThread().setContextClassLoader(classLoader);
545         try {
546             wrapped.unbind(name);
547         } finally {
548             Thread.currentThread().setContextClassLoader(old);
549         }
550
551     }
552
553     /**
554      * Unbinds the named object.
555      * @param name the name to unbind; may not be empty
556      * @throws NamingException if a naming exception is encountered
557      * @see javax.naming.NameNotFoundException
558      * @see javax.naming.InvalidNameException
559      */

560     public void unbind(final String JavaDoc name) throws NamingException JavaDoc {
561         ClassLoader JavaDoc old = Thread.currentThread().getContextClassLoader();
562         Thread.currentThread().setContextClassLoader(classLoader);
563         try {
564             wrapped.unbind(name);
565         } finally {
566             Thread.currentThread().setContextClassLoader(old);
567         }
568
569     }
570
571 }
572
Popular Tags