1 28 29 package com.caucho.loader; 30 31 36 public class EnvironmentLocal<E> { 37 private static int _varCount; 38 39 private String _varName; 40 private E _globalValue; 41 42 46 public EnvironmentLocal() 47 { 48 _varName = "resin:var-" + _varCount++; 49 } 50 51 public EnvironmentLocal(String varName) 52 { 53 _varName = varName; 54 } 55 56 public String getVariable() 57 { 58 return _varName; 59 } 60 61 64 public E get() 65 { 66 Thread thread = Thread.currentThread(); 67 ClassLoader loader = thread.getContextClassLoader(); 68 69 Object value = null; 70 71 for (; loader != null; loader = loader.getParent()) { 72 if (loader instanceof EnvironmentClassLoader) { 73 EnvironmentClassLoader envLoader = (EnvironmentClassLoader) loader; 74 75 value = envLoader.getAttribute(_varName); 76 77 if (value != null) 78 return (E) value; 79 } 80 } 81 82 return _globalValue; 83 } 84 85 88 public E get(ClassLoader loader) 89 { 90 Object value = null; 91 92 for (; loader != null; loader = loader.getParent()) { 93 if (loader instanceof EnvironmentClassLoader) { 94 EnvironmentClassLoader envLoader = (EnvironmentClassLoader) loader; 95 96 value = envLoader.getAttribute(_varName); 97 98 if (value != null) 99 return (E) value; 100 } 101 } 102 103 return _globalValue; 104 } 105 106 109 public E getLevel() 110 { 111 Thread thread = Thread.currentThread(); 112 ClassLoader loader = thread.getContextClassLoader(); 113 114 for (; loader != null; loader = loader.getParent()) { 115 if (loader instanceof EnvironmentClassLoader) { 116 EnvironmentClassLoader envLoader = (EnvironmentClassLoader) loader; 117 118 return (E) envLoader.getAttribute(_varName); 119 } 120 } 121 122 return _globalValue; 123 } 124 125 128 public E getLevel(ClassLoader loader) 129 { 130 for (; loader != null; loader = loader.getParent()) { 131 if (loader instanceof EnvironmentClassLoader) { 132 EnvironmentClassLoader envLoader = (EnvironmentClassLoader) loader; 133 134 return (E) envLoader.getAttribute(_varName); 135 } 136 } 137 138 return _globalValue; 139 } 140 141 148 public final E set(E value) 149 { 150 Thread thread = Thread.currentThread(); 151 ClassLoader loader = thread.getContextClassLoader(); 152 153 for (; loader != null; loader = loader.getParent()) { 154 if (loader instanceof EnvironmentClassLoader) { 155 EnvironmentClassLoader envLoader = (EnvironmentClassLoader) loader; 156 157 return (E) envLoader.setAttribute(_varName, value); 158 } 159 } 160 161 return setGlobal(value); 162 } 163 164 171 public final E set(E value, ClassLoader loader) 172 { 173 for (; loader != null; loader = loader.getParent()) { 174 if (loader instanceof EnvironmentClassLoader) { 175 EnvironmentClassLoader envLoader = (EnvironmentClassLoader) loader; 176 177 return (E) envLoader.setAttribute(_varName, value); 178 } 179 } 180 181 return setGlobal(value); 182 } 183 184 191 public final E remove() 192 { 193 Thread thread = Thread.currentThread(); 194 ClassLoader loader = thread.getContextClassLoader(); 195 196 for (; loader != null; loader = loader.getParent()) { 197 if (loader instanceof EnvironmentClassLoader) { 198 EnvironmentClassLoader envLoader = (EnvironmentClassLoader) loader; 199 200 return (E) envLoader.removeAttribute(_varName); 201 } 202 } 203 204 return setGlobal(null); 205 } 206 207 214 public final E remove(ClassLoader loader) 215 { 216 for (; loader != null; loader = loader.getParent()) { 217 if (loader instanceof EnvironmentClassLoader) { 218 EnvironmentClassLoader envLoader = (EnvironmentClassLoader) loader; 219 220 return (E) envLoader.removeAttribute(_varName); 221 } 222 } 223 224 return setGlobal(null); 225 } 226 227 234 public E setGlobal(E value) 235 { 236 E oldValue = _globalValue; 237 238 _globalValue = value; 239 240 return oldValue; 241 } 242 243 246 public E getGlobal() 247 { 248 return _globalValue; 249 } 250 } 251 | Popular Tags |