1 16 17 package org.apache.commons.beanutils; 18 19 import java.util.Map ; 20 import java.util.WeakHashMap ; 21 22 33 public class ContextClassLoaderLocal { 34 private Map valueByClassLoader = new WeakHashMap (); 35 private boolean globalValueInitialized = false; 36 private Object globalValue; 37 38 public ContextClassLoaderLocal() { 39 super(); 40 } 41 42 55 protected Object initialValue() { 56 return null; 57 } 58 59 65 public synchronized Object get() { 66 70 valueByClassLoader.isEmpty(); 72 try { 73 74 ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); 75 if (contextClassLoader != null) { 76 77 Object value = valueByClassLoader.get(contextClassLoader); 78 if ((value == null) 79 && !valueByClassLoader.containsKey(contextClassLoader)) { 80 value = initialValue(); 81 valueByClassLoader.put(contextClassLoader, value); 82 } 83 return value; 84 85 } 86 87 } catch (SecurityException e) { } 88 89 if (!globalValueInitialized) { 91 globalValue = initialValue(); 92 globalValueInitialized = true; 93 } return globalValue; 95 } 96 97 103 public synchronized void set(Object value) { 104 107 valueByClassLoader.isEmpty(); 109 try { 110 111 ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); 112 if (contextClassLoader != null) { 113 valueByClassLoader.put(contextClassLoader, value); 114 return; 115 } 116 117 } catch (SecurityException e) { } 118 119 globalValue = value; 121 globalValueInitialized = true; 122 } 123 124 127 public synchronized void unset() { 128 try { 129 130 ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); 131 unset(contextClassLoader); 132 133 } catch (SecurityException e) { } 134 } 135 136 139 public synchronized void unset(ClassLoader classLoader) { 140 valueByClassLoader.remove(classLoader); 141 } 142 } | Popular Tags |