001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: SmartContext.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.component.smartclient.spi;
025:
026: import java.util.Hashtable;
027:
028: import javax.naming.Binding;
029: import javax.naming.Context;
030: import javax.naming.Name;
031: import javax.naming.NameClassPair;
032: import javax.naming.NameParser;
033: import javax.naming.NamingEnumeration;
034: import javax.naming.NamingException;
035:
036: /**
037: * Context that use a given classloader before calling every methods.
038: * @author Florent Benoit
039: */
040: public class SmartContext implements Context {
041:
042: /**
043: * Context that is wrapped.
044: */
045: private Context wrapped;
046:
047: /**
048: * Classloader to use for all methods.
049: */
050: private ClassLoader classLoader;
051:
052: /**
053: * Creates a context with the given wrapped context and the given
054: * classloader.
055: * @param wrapped the context to wrap
056: * @param classLoader the classloader to use.
057: */
058: public SmartContext(final Context wrapped,
059: final ClassLoader classLoader) {
060: this .wrapped = wrapped;
061: this .classLoader = classLoader;
062: }
063:
064: /**
065: * Adds a new environment property to the environment of this context. If
066: * the property already exists, its value is overwritten.
067: * @param propName the name of the environment property to add; may not be
068: * null
069: * @param propVal the value of the property to add; may not be null
070: * @return the previous value of the property, or null if the property was
071: * not in the environment before
072: * @throws NamingException if a naming exception is encountered
073: */
074: public Object addToEnvironment(final String propName,
075: final Object propVal) throws NamingException {
076: ClassLoader old = Thread.currentThread()
077: .getContextClassLoader();
078: Thread.currentThread().setContextClassLoader(classLoader);
079: try {
080: return wrapped.addToEnvironment(propName, propVal);
081: } finally {
082: Thread.currentThread().setContextClassLoader(old);
083: }
084: }
085:
086: /**
087: * Binds a name to an object. Delegate to the String version.
088: * @param name the name to bind; may not be empty
089: * @param obj the object to bind; possibly null
090: * @throws NamingException if a naming exception is encountered
091: * @see javax.naming.NameAlreadyBoundException
092: */
093: public void bind(final Name name, final Object obj)
094: throws NamingException {
095: ClassLoader old = Thread.currentThread()
096: .getContextClassLoader();
097: Thread.currentThread().setContextClassLoader(classLoader);
098: try {
099: wrapped.bind(name, obj);
100: } finally {
101: Thread.currentThread().setContextClassLoader(old);
102: }
103: }
104:
105: /**
106: * Binds a name to an object.
107: * @param name the name to bind; may not be empty
108: * @param obj the object to bind; possibly null
109: * @throws NamingException if a naming exception is encountered
110: * @see javax.naming.NameAlreadyBoundException
111: */
112: public void bind(final String name, final Object obj)
113: throws NamingException {
114: ClassLoader old = Thread.currentThread()
115: .getContextClassLoader();
116: Thread.currentThread().setContextClassLoader(classLoader);
117: try {
118: wrapped.bind(name, obj);
119: } finally {
120: Thread.currentThread().setContextClassLoader(old);
121: }
122: }
123:
124: /**
125: * Closes this context.
126: * @throws NamingException if a naming exception is encountered
127: */
128: public void close() throws NamingException {
129: ClassLoader old = Thread.currentThread()
130: .getContextClassLoader();
131: Thread.currentThread().setContextClassLoader(classLoader);
132: try {
133: wrapped.close();
134: } finally {
135: Thread.currentThread().setContextClassLoader(old);
136: }
137: }
138:
139: /**
140: * Composes the name of this context with a name relative to this context.
141: * @param name a name relative to this context
142: * @param prefix the name of this context relative to one of its ancestors
143: * @return the composition of prefix and name
144: * @throws NamingException if a naming exception is encountered
145: */
146: public Name composeName(final Name name, final Name prefix)
147: throws NamingException {
148: ClassLoader old = Thread.currentThread()
149: .getContextClassLoader();
150: Thread.currentThread().setContextClassLoader(classLoader);
151: try {
152: return wrapped.composeName(name, prefix);
153: } finally {
154: Thread.currentThread().setContextClassLoader(old);
155: }
156: }
157:
158: /**
159: * Composes the name of this context with a name relative to this context:
160: * Not supported.
161: * @param name a name relative to this context
162: * @param prefix the name of this context relative to one of its ancestors
163: * @return the composition of prefix and name
164: * @throws NamingException if a naming exception is encountered
165: */
166: public String composeName(final String name, final String prefix)
167: throws NamingException {
168: ClassLoader old = Thread.currentThread()
169: .getContextClassLoader();
170: Thread.currentThread().setContextClassLoader(classLoader);
171: try {
172: return wrapped.composeName(name, prefix);
173: } finally {
174: Thread.currentThread().setContextClassLoader(old);
175: }
176: }
177:
178: /**
179: * Creates and binds a new context. Creates a new context with the given
180: * name and binds it in the target context.
181: * @param name the name of the context to create; may not be empty
182: * @return the newly created context
183: * @throws NamingException if a naming exception is encountered
184: * @see javax.naming.NameAlreadyBoundException
185: */
186: public Context createSubcontext(final Name name)
187: throws NamingException {
188: ClassLoader old = Thread.currentThread()
189: .getContextClassLoader();
190: Thread.currentThread().setContextClassLoader(classLoader);
191: try {
192: return wrapped.createSubcontext(name);
193: } finally {
194: Thread.currentThread().setContextClassLoader(old);
195: }
196: }
197:
198: /**
199: * Creates and binds a new context.
200: * @param name the name of the context to create; may not be empty
201: * @return the newly created context
202: * @throws NamingException if a naming exception is encountered
203: * @see javax.naming.NameAlreadyBoundException
204: */
205: public Context createSubcontext(final String name)
206: throws NamingException {
207: ClassLoader old = Thread.currentThread()
208: .getContextClassLoader();
209: Thread.currentThread().setContextClassLoader(classLoader);
210: try {
211: return wrapped.createSubcontext(name);
212: } finally {
213: Thread.currentThread().setContextClassLoader(old);
214: }
215:
216: }
217:
218: /**
219: * Destroys the named context and removes it from the namespace. Not
220: * supported yet.
221: * @param name the name of the context to be destroyed; may not be empty
222: * @throws NamingException if a naming exception is encountered
223: */
224: public void destroySubcontext(final Name name)
225: throws NamingException {
226: ClassLoader old = Thread.currentThread()
227: .getContextClassLoader();
228: Thread.currentThread().setContextClassLoader(classLoader);
229: try {
230: wrapped.destroySubcontext(name);
231: } finally {
232: Thread.currentThread().setContextClassLoader(old);
233: }
234: }
235:
236: /**
237: * Destroys the named context and removes it from the namespace. Not
238: * supported yet.
239: * @param name the name of the context to be destroyed; may not be empty
240: * @throws NamingException if a naming exception is encountered
241: */
242: public void destroySubcontext(final String name)
243: throws NamingException {
244: ClassLoader old = Thread.currentThread()
245: .getContextClassLoader();
246: Thread.currentThread().setContextClassLoader(classLoader);
247: try {
248: wrapped.destroySubcontext(name);
249: } finally {
250: Thread.currentThread().setContextClassLoader(old);
251: }
252: }
253:
254: /**
255: * Retrieves the environment in effect for this context.
256: * @return the environment of this context; never null
257: * @throws NamingException if a naming exception is encountered
258: */
259: public Hashtable<?, ?> getEnvironment() throws NamingException {
260: ClassLoader old = Thread.currentThread()
261: .getContextClassLoader();
262: Thread.currentThread().setContextClassLoader(classLoader);
263: try {
264: return wrapped.getEnvironment();
265: } finally {
266: Thread.currentThread().setContextClassLoader(old);
267: }
268: }
269:
270: /**
271: * Retrieves the full name of this context within its own namespace.
272: * @return this context's name in its own namespace; never null
273: * @throws NamingException if it fails.
274: */
275: public String getNameInNamespace() throws NamingException {
276: ClassLoader old = Thread.currentThread()
277: .getContextClassLoader();
278: Thread.currentThread().setContextClassLoader(classLoader);
279: try {
280: return wrapped.getNameInNamespace();
281: } finally {
282: Thread.currentThread().setContextClassLoader(old);
283: }
284: }
285:
286: /**
287: * Retrieves the parser associated with the named context.
288: * @param name the name of the context from which to get the parser
289: * @return a name parser that can parse compound names into their atomic
290: * components
291: * @throws NamingException if a naming exception is encountered
292: */
293: public NameParser getNameParser(final Name name)
294: throws NamingException {
295: ClassLoader old = Thread.currentThread()
296: .getContextClassLoader();
297: Thread.currentThread().setContextClassLoader(classLoader);
298: try {
299: return wrapped.getNameParser(name);
300: } finally {
301: Thread.currentThread().setContextClassLoader(old);
302: }
303: }
304:
305: /**
306: * Retrieves the parser associated with the named context.
307: * @param name the name of the context from which to get the parser
308: * @return a name parser that can parse compound names into their atomic
309: * components
310: * @throws NamingException if a naming exception is encountered
311: */
312: public NameParser getNameParser(final String name)
313: throws NamingException {
314: ClassLoader old = Thread.currentThread()
315: .getContextClassLoader();
316: Thread.currentThread().setContextClassLoader(classLoader);
317: try {
318: return wrapped.getNameParser(name);
319: } finally {
320: Thread.currentThread().setContextClassLoader(old);
321: }
322:
323: }
324:
325: /**
326: * Enumerates the names bound in the named context, along with the class
327: * names of objects bound to them. The contents of any subcontexts are not
328: * included.
329: * @param name the name of the context to list
330: * @return an enumeration of the names and class names of the bindings in
331: * this context. Each element of the enumeration is of type
332: * NameClassPair.
333: * @throws NamingException if a naming exception is encountered
334: */
335: public NamingEnumeration<NameClassPair> list(final Name name)
336: throws NamingException {
337: ClassLoader old = Thread.currentThread()
338: .getContextClassLoader();
339: Thread.currentThread().setContextClassLoader(classLoader);
340: try {
341: return wrapped.list(name);
342: } finally {
343: Thread.currentThread().setContextClassLoader(old);
344: }
345:
346: }
347:
348: /**
349: * Enumerates the names bound in the named context, along with the class
350: * names of objects bound to them.
351: * @param name the name of the context to list
352: * @return an enumeration of the names and class names of the bindings in
353: * this context. Each element of the enumeration is of type
354: * NameClassPair.
355: * @throws NamingException if a naming exception is encountered
356: */
357: public NamingEnumeration<NameClassPair> list(final String name)
358: throws NamingException {
359: ClassLoader old = Thread.currentThread()
360: .getContextClassLoader();
361: Thread.currentThread().setContextClassLoader(classLoader);
362: try {
363: return wrapped.list(name);
364: } finally {
365: Thread.currentThread().setContextClassLoader(old);
366: }
367: }
368:
369: /**
370: * Enumerates the names bound in the named context, along with the objects
371: * bound to them. The contents of any subcontexts are not included. If a
372: * binding is added to or removed from this context, its effect on an
373: * enumeration previously returned is undefined.
374: * @param name the name of the context to list
375: * @return an enumeration of the bindings in this context. Each element of
376: * the enumeration is of type Binding.
377: * @throws NamingException if a naming exception is encountered
378: */
379: public NamingEnumeration<Binding> listBindings(final Name name)
380: throws NamingException {
381: ClassLoader old = Thread.currentThread()
382: .getContextClassLoader();
383: Thread.currentThread().setContextClassLoader(classLoader);
384: try {
385: return wrapped.listBindings(name);
386: } finally {
387: Thread.currentThread().setContextClassLoader(old);
388: }
389: }
390:
391: /**
392: * Enumerates the names bound in the named context, along with the objects
393: * bound to them.
394: * @param name the name of the context to list
395: * @return an enumeration of the bindings in this context. Each element of
396: * the enumeration is of type Binding.
397: * @throws NamingException if a naming exception is encountered
398: */
399: public NamingEnumeration<Binding> listBindings(final String name)
400: throws NamingException {
401: ClassLoader old = Thread.currentThread()
402: .getContextClassLoader();
403: Thread.currentThread().setContextClassLoader(classLoader);
404: try {
405: return wrapped.listBindings(name);
406: } finally {
407: Thread.currentThread().setContextClassLoader(old);
408: }
409: }
410:
411: /**
412: * Retrieves the named object.
413: * @param name the name of the object to look up
414: * @return the object bound to <tt>name</tt>
415: * @throws NamingException if a naming exception is encountered
416: */
417: public Object lookup(final Name name) throws NamingException {
418: ClassLoader old = Thread.currentThread()
419: .getContextClassLoader();
420: Thread.currentThread().setContextClassLoader(classLoader);
421: try {
422: return wrapped.lookup(name);
423: } finally {
424: Thread.currentThread().setContextClassLoader(old);
425: }
426: }
427:
428: /**
429: * Retrieves the named object.
430: * @param name the name of the object to look up
431: * @return the object bound to name
432: * @throws NamingException if a naming exception is encountered
433: */
434: public Object lookup(final String name) throws NamingException {
435: ClassLoader old = Thread.currentThread()
436: .getContextClassLoader();
437: Thread.currentThread().setContextClassLoader(classLoader);
438: try {
439: return wrapped.lookup(name);
440: } finally {
441: Thread.currentThread().setContextClassLoader(old);
442: }
443: }
444:
445: /**
446: * Retrieves the named object, following links except for the terminal
447: * atomic component of the name. If the object bound to name is not a link,
448: * returns the object itself.
449: * @param name the name of the object to look up
450: * @return the object bound to name, not following the terminal link (if
451: * any).
452: * @throws NamingException if a naming exception is encountered
453: */
454: public Object lookupLink(final Name name) throws NamingException {
455: ClassLoader old = Thread.currentThread()
456: .getContextClassLoader();
457: Thread.currentThread().setContextClassLoader(classLoader);
458: try {
459: return wrapped.lookupLink(name);
460: } finally {
461: Thread.currentThread().setContextClassLoader(old);
462: }
463: }
464:
465: /**
466: * Retrieves the named object, following links except for the terminal
467: * atomic component of the name. If the object bound to name is not a link,
468: * returns the object itself.
469: * @param name the name of the object to look up
470: * @return the object bound to name, not following the terminal link (if
471: * any)
472: * @throws NamingException if a naming exception is encountered
473: */
474: public Object lookupLink(final String name) throws NamingException {
475: ClassLoader old = Thread.currentThread()
476: .getContextClassLoader();
477: Thread.currentThread().setContextClassLoader(classLoader);
478: try {
479: return wrapped.lookupLink(name);
480: } finally {
481: Thread.currentThread().setContextClassLoader(old);
482: }
483: }
484:
485: /**
486: * Binds a name to an object, overwriting any existing binding.
487: * @param name the name to bind; may not be empty
488: * @param obj the object to bind; possibly null
489: * @throws NamingException if a naming exception is encountered
490: */
491: public void rebind(final Name name, final Object obj)
492: throws NamingException {
493: ClassLoader old = Thread.currentThread()
494: .getContextClassLoader();
495: Thread.currentThread().setContextClassLoader(classLoader);
496: try {
497: wrapped.rebind(name, obj);
498: } finally {
499: Thread.currentThread().setContextClassLoader(old);
500: }
501:
502: }
503:
504: /**
505: * Binds a name to an object, overwriting any existing binding.
506: * @param name the name to bind; may not be empty
507: * @param obj the object to bind; possibly null
508: * @throws NamingException if a naming exception is encountered
509: * @see javax.naming.InvalidNameException
510: */
511: public void rebind(final String name, final Object obj)
512: throws NamingException {
513: ClassLoader old = Thread.currentThread()
514: .getContextClassLoader();
515: Thread.currentThread().setContextClassLoader(classLoader);
516: try {
517: wrapped.rebind(name, obj);
518: } finally {
519: Thread.currentThread().setContextClassLoader(old);
520: }
521: }
522:
523: /**
524: * Removes an environment property from the environment of this context.
525: * @param propName the name of the environment property to remove; may not
526: * be null
527: * @return the previous value of the property, or null if the property was
528: * not in the environment
529: * @throws NamingException if a naming exception is encountered
530: */
531: public Object removeFromEnvironment(final String propName)
532: throws NamingException {
533: ClassLoader old = Thread.currentThread()
534: .getContextClassLoader();
535: Thread.currentThread().setContextClassLoader(classLoader);
536: try {
537: return wrapped.removeFromEnvironment(propName);
538: } finally {
539: Thread.currentThread().setContextClassLoader(old);
540: }
541:
542: }
543:
544: /**
545: * Binds a new name to the object bound to an old name, and unbinds the old
546: * name.
547: * @param oldName the name of the existing binding; may not be empty
548: * @param newName the name of the new binding; may not be empty
549: * @throws NamingException if a naming exception is encountered
550: */
551: public void rename(final Name oldName, final Name newName)
552: throws NamingException {
553: ClassLoader old = Thread.currentThread()
554: .getContextClassLoader();
555: Thread.currentThread().setContextClassLoader(classLoader);
556: try {
557: wrapped.rename(oldName, newName);
558: } finally {
559: Thread.currentThread().setContextClassLoader(old);
560: }
561: }
562:
563: /**
564: * Binds a new name to the object bound to an old name, and unbinds the old
565: * name.
566: * @param oldName the name of the existing binding; may not be empty
567: * @param newName the name of the new binding; may not be empty
568: * @throws NamingException if a naming exception is encountered
569: */
570: public void rename(final String oldName, final String newName)
571: throws NamingException {
572: ClassLoader old = Thread.currentThread()
573: .getContextClassLoader();
574: Thread.currentThread().setContextClassLoader(classLoader);
575: try {
576: wrapped.rename(oldName, newName);
577: } finally {
578: Thread.currentThread().setContextClassLoader(old);
579: }
580: }
581:
582: /**
583: * Unbinds the named object.
584: * @param name the name to unbind; may not be empty
585: * @throws NamingException if a naming exception is encountered
586: * @see javax.naming.NameNotFoundException
587: */
588: public void unbind(final Name name) throws NamingException {
589: ClassLoader old = Thread.currentThread()
590: .getContextClassLoader();
591: Thread.currentThread().setContextClassLoader(classLoader);
592: try {
593: wrapped.unbind(name);
594: } finally {
595: Thread.currentThread().setContextClassLoader(old);
596: }
597:
598: }
599:
600: /**
601: * Unbinds the named object.
602: * @param name the name to unbind; may not be empty
603: * @throws NamingException if a naming exception is encountered
604: * @see javax.naming.NameNotFoundException
605: * @see javax.naming.InvalidNameException
606: */
607: public void unbind(final String name) throws NamingException {
608: ClassLoader old = Thread.currentThread()
609: .getContextClassLoader();
610: Thread.currentThread().setContextClassLoader(classLoader);
611: try {
612: wrapped.unbind(name);
613: } finally {
614: Thread.currentThread().setContextClassLoader(old);
615: }
616:
617: }
618:
619: }
|