1 16 17 package org.apache.naming.modules.java; 18 19 import java.util.Hashtable ; 20 21 import javax.naming.Context ; 22 import javax.naming.NamingException ; 23 24 import org.apache.naming.core.ContextAccessController; 25 import org.apache.tomcat.util.res.StringManager; 26 27 29 38 public class ContextBindings { 39 private static org.apache.commons.logging.Log log= 40 org.apache.commons.logging.LogFactory.getLog( ContextBindings.class ); 41 42 44 45 48 private static Hashtable contextNameBindings = new Hashtable (); 49 50 51 54 private static Hashtable threadBindings = new Hashtable (); 55 56 57 60 private static Hashtable threadNameBindings = new Hashtable (); 61 62 63 66 private static Hashtable clBindings = new Hashtable (); 67 68 69 72 private static Hashtable clNameBindings = new Hashtable (); 73 74 75 78 protected static StringManager sm = 79 StringManager.getManager("org.apache.naming"); 80 81 82 84 85 91 public static void bindContext(Object name, Context context) { 92 bindContext(name, context, null); 93 } 94 95 96 103 public static void bindContext(Object name, Context context, 104 Object token) { 105 if (ContextAccessController.checkSecurityToken(name, token)) 106 contextNameBindings.put(name, context); 107 } 108 109 110 115 public static void unbindContext(Object name) { 116 unbindContext(name, null); 117 } 118 119 120 126 public static void unbindContext(Object name, Object token) { 127 if (ContextAccessController.checkSecurityToken(name, token)) 128 contextNameBindings.remove(name); 129 } 130 131 132 137 static Context getContext(Object name) { 138 return (Context ) contextNameBindings.get(name); 139 } 140 141 142 147 public static void bindThread(Object name) 148 throws NamingException { 149 bindThread(name, null); 150 } 151 152 153 159 public static void bindThread(Object name, Object token) 160 throws NamingException { 161 if (ContextAccessController.checkSecurityToken(name, token)) { 163 Context context = (Context ) contextNameBindings.get(name); 164 if (context == null) 165 throw new NamingException  166 (sm.getString("contextBindings.unknownContext", name)); 167 threadBindings.put(Thread.currentThread(), context); 168 threadNameBindings.put(Thread.currentThread(), name); 169 } 170 } 171 172 173 178 public static void unbindThread(Object name) { 179 unbindThread(name, null); 180 } 181 182 183 189 public static void unbindThread(Object name, Object token) { 190 if (ContextAccessController.checkSecurityToken(name, token)) { 191 threadBindings.remove(Thread.currentThread()); 192 threadNameBindings.remove(Thread.currentThread()); 193 } 194 } 195 196 197 200 public static Context getThread() 201 throws NamingException { 202 Context context = 203 (Context ) threadBindings.get(Thread.currentThread()); 204 log.info( "Context=getThread: " + context ); 205 if (context == null) 206 throw new NamingException  207 (sm.getString("contextBindings.noContextBoundToThread")); 208 return context; 209 } 210 211 212 215 static Object getThreadName() 216 throws NamingException { 217 Object name = threadNameBindings.get(Thread.currentThread()); 218 if (name == null) 219 throw new NamingException  220 (sm.getString("contextBindings.noContextBoundToThread")); 221 return name; 222 } 223 224 225 228 public static boolean isThreadBound() { 229 return (threadBindings.containsKey(Thread.currentThread())); 230 } 231 232 233 238 public static void bindClassLoader(Object name) 239 throws NamingException { 240 bindClassLoader(name, null); 241 } 242 243 244 250 public static void bindClassLoader(Object name, Object token) 251 throws NamingException { 252 bindClassLoader 253 (name, token, Thread.currentThread().getContextClassLoader()); 254 } 255 256 257 263 public static void bindClassLoader(Object name, Object token, 264 ClassLoader classLoader) 265 throws NamingException { 266 if (ContextAccessController.checkSecurityToken(name, token)) { 267 Context context = (Context ) contextNameBindings.get(name); 268 if (context == null) 269 throw new NamingException  270 (sm.getString("contextBindings.unknownContext", name)); 271 clBindings.put(classLoader, context); 272 clNameBindings.put(classLoader, name); 273 } 274 } 275 276 277 282 public static void unbindClassLoader(Object name) { 283 unbindClassLoader(name, null); 284 } 285 286 287 293 public static void unbindClassLoader(Object name, Object token) { 294 unbindClassLoader(name, token, 295 Thread.currentThread().getContextClassLoader()); 296 } 297 298 299 305 public static void unbindClassLoader(Object name, Object token, 306 ClassLoader classLoader) { 307 if (ContextAccessController.checkSecurityToken(name, token)) { 308 Object n = clNameBindings.get(classLoader); 309 if (!(n.equals(name))) { 310 return; 311 } 312 clBindings.remove(classLoader); 313 clNameBindings.remove(classLoader); 314 } 315 } 316 317 318 321 public static Context getClassLoader() 322 throws NamingException { 323 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 324 Context context = null; 325 do { 326 context = (Context ) clBindings.get(cl); 327 log.info( "Context=getClassLoader: " + context + " " + cl ); 328 if (context != null) { 329 return context; 330 } 331 } while ((cl = cl.getParent()) != null); 332 throw new NamingException  333 (sm.getString("contextBindings.noContextBoundToCL")); 334 } 335 336 337 340 static Object getClassLoaderName() 341 throws NamingException { 342 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 343 Object name = null; 344 do { 345 name = clNameBindings.get(cl); 346 if (name != null) { 347 return name; 348 } 349 } while ((cl = cl.getParent()) != null); 350 throw new NamingException  351 (sm.getString("contextBindings.noContextBoundToCL")); 352 } 353 354 355 358 public static boolean isClassLoaderBound() { 359 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 360 do { 361 if (clBindings.containsKey(cl)) { 362 return true; 363 } 364 } while ((cl = cl.getParent()) != null); 365 return false; 366 } 367 368 369 } 370 371
| Popular Tags
|