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 InheritableClassContextLocal to also try to set values in the parent
9    classloader. This is only done in the following cirkumstances: the current classloader value is null, any of the parent class loaders values in NOT null.
10  
11   <p>Basically it tries to set a value for a classloader that already have a value, if this does not succed, the set the local one.
12   *
13   *
14   * @author Peter Antman
15   * @version $Revision: 1.1.1.1 $
16   */
17  
18  public class FindClassContextLocal extends InheritableClassContextLocal {
19      
20      public FindClassContextLocal() {
21  	
22      }
23  
24      /***
25       * Set value in current context class loader or parent classloader if a value was already set. Otherwise set in current context class loader.
26       *
27       * @param object save object in current classloader.
28       */
29      public void set(Object object) {
30  	ClassLoader cl = Thread.currentThread().getContextClassLoader();
31  	if (!set(cl,object))
32  	    map.put(cl,object);
33      }
34      
35      
36      protected boolean set(ClassLoader cl, Object object) {
37  	if (cl != null) {
38  	    Object o = map.get(cl);
39  	    if ( o != null) {
40  		map.put(cl,object);//Owerwrite old value
41  		return true;
42  	    }
43  	    else
44  		return set(cl.getParent(), object);
45  	} else {
46  	    return false;
47  	}
48      }
49  } // FindClassContextLocal
50  
51  
52  
53  
54  
55