1 17 18 19 package org.apache.naming; 20 21 import java.util.Hashtable ; 22 import javax.naming.NamingException ; 23 import javax.naming.Context ; 24 25 35 36 public class ContextBindings { 37 38 39 41 42 45 private static Hashtable contextNameBindings = new Hashtable (); 46 47 48 51 private static Hashtable threadBindings = new Hashtable (); 52 53 54 57 private static Hashtable threadNameBindings = new Hashtable (); 58 59 60 63 private static Hashtable clBindings = new Hashtable (); 64 65 66 69 private static Hashtable clNameBindings = new Hashtable (); 70 71 72 75 protected static StringManager sm = 76 StringManager.getManager(Constants.Package); 77 78 79 81 82 88 public static void bindContext(Object name, Context context) { 89 bindContext(name, context, null); 90 } 91 92 93 100 public static void bindContext(Object name, Context context, 101 Object token) { 102 if (ContextAccessController.checkSecurityToken(name, token)) 103 contextNameBindings.put(name, context); 104 } 105 106 107 112 public static void unbindContext(Object name) { 113 unbindContext(name, null); 114 } 115 116 117 123 public static void unbindContext(Object name, Object token) { 124 if (ContextAccessController.checkSecurityToken(name, token)) 125 contextNameBindings.remove(name); 126 } 127 128 129 134 static Context getContext(Object name) { 135 return (Context ) contextNameBindings.get(name); 136 } 137 138 139 144 public static void bindThread(Object name) 145 throws NamingException { 146 bindThread(name, null); 147 } 148 149 150 156 public static void bindThread(Object name, Object token) 157 throws NamingException { 158 if (ContextAccessController.checkSecurityToken(name, token)) { 159 Context context = (Context ) contextNameBindings.get(name); 160 if (context == null) 161 throw new NamingException 162 (sm.getString("contextBindings.unknownContext", name)); 163 threadBindings.put(Thread.currentThread(), context); 164 threadNameBindings.put(Thread.currentThread(), name); 165 } 166 } 167 168 169 174 public static void unbindThread(Object name) { 175 unbindThread(name, null); 176 } 177 178 179 185 public static void unbindThread(Object name, Object token) { 186 if (ContextAccessController.checkSecurityToken(name, token)) { 187 threadBindings.remove(Thread.currentThread()); 188 threadNameBindings.remove(Thread.currentThread()); 189 } 190 } 191 192 193 196 public static Context getThread() 197 throws NamingException { 198 Context context = 199 (Context ) threadBindings.get(Thread.currentThread()); 200 if (context == null) 201 throw new NamingException 202 (sm.getString("contextBindings.noContextBoundToThread")); 203 return context; 204 } 205 206 207 210 static Object getThreadName() 211 throws NamingException { 212 Object name = threadNameBindings.get(Thread.currentThread()); 213 if (name == null) 214 throw new NamingException 215 (sm.getString("contextBindings.noContextBoundToThread")); 216 return name; 217 } 218 219 220 223 public static boolean isThreadBound() { 224 return (threadBindings.containsKey(Thread.currentThread())); 225 } 226 227 228 233 public static void bindClassLoader(Object name) 234 throws NamingException { 235 bindClassLoader(name, null); 236 } 237 238 239 245 public static void bindClassLoader(Object name, Object token) 246 throws NamingException { 247 bindClassLoader 248 (name, token, Thread.currentThread().getContextClassLoader()); 249 } 250 251 252 258 public static void bindClassLoader(Object name, Object token, 259 ClassLoader classLoader) 260 throws NamingException { 261 if (ContextAccessController.checkSecurityToken(name, token)) { 262 Context context = (Context ) contextNameBindings.get(name); 263 if (context == null) 264 throw new NamingException 265 (sm.getString("contextBindings.unknownContext", name)); 266 clBindings.put(classLoader, context); 267 clNameBindings.put(classLoader, name); 268 } 269 } 270 271 272 277 public static void unbindClassLoader(Object name) { 278 unbindClassLoader(name, null); 279 } 280 281 282 288 public static void unbindClassLoader(Object name, Object token) { 289 unbindClassLoader(name, token, 290 Thread.currentThread().getContextClassLoader()); 291 } 292 293 294 300 public static void unbindClassLoader(Object name, Object token, 301 ClassLoader classLoader) { 302 if (ContextAccessController.checkSecurityToken(name, token)) { 303 Object n = clNameBindings.get(classLoader); 304 if ((n==null) || !(n.equals(name))) { 305 return; 306 } 307 clBindings.remove(classLoader); 308 clNameBindings.remove(classLoader); 309 } 310 } 311 312 313 316 public static Context getClassLoader() 317 throws NamingException { 318 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 319 Context context = null; 320 do { 321 context = (Context ) clBindings.get(cl); 322 if (context != null) { 323 return context; 324 } 325 } while ((cl = cl.getParent()) != null); 326 throw new NamingException 327 (sm.getString("contextBindings.noContextBoundToCL")); 328 } 329 330 331 334 static Object getClassLoaderName() 335 throws NamingException { 336 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 337 Object name = null; 338 do { 339 name = clNameBindings.get(cl); 340 if (name != null) { 341 return name; 342 } 343 } while ((cl = cl.getParent()) != null); 344 throw new NamingException 345 (sm.getString("contextBindings.noContextBoundToCL")); 346 } 347 348 349 352 public static boolean isClassLoaderBound() { 353 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 354 do { 355 if (clBindings.containsKey(cl)) { 356 return true; 357 } 358 } while ((cl = cl.getParent()) != null); 359 return false; 360 } 361 362 363 } 364 | Popular Tags |