View Javadoc

1   /*
2   *
3   * Distributable under LGPL license.
4   * See terms of license at gnu.org.
5   */
6   package org.backsource.utils.lang;
7   /***
8    * <p>Refines the ClassContextLocal to make set values possible to be
9    * "inherited" by child classladers. 
10   *
11   * <p>Setting a value will whoever only affect the current classloader.
12   *
13   *
14   * @author Peter Antman
15   * @version $Revision: 1.1.1.1 $
16   */
17  
18  public class InheritableClassContextLocal extends ClassContextLocal {
19      
20      public InheritableClassContextLocal() {
21  	
22      }
23      /***
24       * Get value from current context class loader or a parent classloader
25       * if no value is set.
26       *
27       * @return Object set in this classloader or a parent earlier, or null.
28       */
29      public Object get() {
30  	ClassLoader cl = Thread.currentThread().getContextClassLoader();
31  	return get(cl);
32      }
33  
34      protected Object get(ClassLoader cl) {
35  	if (cl != null) {
36  	    Object o = map.get(cl);
37  	    if ( o != null)
38  		return o;
39  	    else
40  		return get(cl.getParent());
41  	} else {
42  	    return null;
43  	}
44      }
45  } // InheritableClassContextLocal