org.backsource.utils.lang
Class ClassContextLocal

java.lang.Object
  extended byorg.backsource.utils.lang.ClassContextLocal
Direct Known Subclasses:
InheritableClassContextLocal

public class ClassContextLocal
extends java.lang.Object

The classloader equivalent of ThreadLocal.

This class provides class context variable. This means that it is possible to get a static variable to only be visable in the context class loader, which is verry usefull to get Singleton behaviour without the static variable beeing propagated to the complete JVM.

It is modelled after the ThreadLocal class, and the same limitations is valid: only threads executing in the same context classloader that set a value will be able to se the value. See InheritableClassContextLocal and FindClassContextLocal for other cinds of behaviour.

Example. You want a singleton, but it must only be valid (set) in a single component, but you can not guarantee that the singleton class will not be loaded i a parent (global) classloader.

If you however know that at least your component will be executing in its own context classloader you may use this class, much like using a ThreadLocal.

Just a demo, set singleton up. Will onlye be available in current context classloader, even thoug the instance variable is static

 class MySingleton {
 static ClassContextLocal instance = new ClassContextLocal();
 
 
 public static void configure(Properties prop) {
 MySingleton s = new MySingleton();
 s.configure(prop);
 instance.set(s);
 }
 
 public static MySingleton getInstance() {
 return instance.get();
 }
 
 protected MySingleton() {
 
 }
 
 protected void configure(Properties prop) {
 //Configure it here
 }
 
 }
 

Version:
$Revision: 1.1.1.1 $
Author:
Peter Antman

Field Summary
protected  java.util.Map map
           
 
Constructor Summary
ClassContextLocal()
           
 
Method Summary
 java.lang.Object get()
          Get value from current context class loader.
 void set(java.lang.Object object)
          Set value in current context class loader.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

map

protected java.util.Map map
Constructor Detail

ClassContextLocal

public ClassContextLocal()
Method Detail

get

public java.lang.Object get()
Get value from current context class loader.

Returns:
Object set in this classloader earlier, or null.

set

public void set(java.lang.Object object)
Set value in current context class loader.

Parameters:
object - save object in current classloader.


Copyright © 2000-2004 Backsource. All Rights Reserved.