1 22 package org.jboss.naming; 23 24 import java.util.Hashtable ; 25 import java.util.WeakHashMap ; 26 import java.security.PrivilegedAction ; 27 import java.security.AccessController ; 28 import javax.naming.Context ; 29 import javax.naming.Name ; 30 import javax.naming.spi.ObjectFactory ; 31 32 import org.jnp.server.NamingServer; 33 import org.jnp.interfaces.NamingContext; 34 35 43 public class ENCFactory 44 implements ObjectFactory 45 { 46 48 private static WeakHashMap encs = new WeakHashMap (); 50 private static ClassLoader topLoader; 51 52 public static void setTopClassLoader(ClassLoader topLoader) 54 { 55 ENCFactory.topLoader = topLoader; 56 } 57 public static ClassLoader getTopClassLoader() 58 { 59 return ENCFactory.topLoader; 60 } 61 62 63 65 67 public Object getObjectInstance(Object obj, Name name, Context nameCtx, 69 Hashtable environment) 70 throws Exception 71 { 72 ClassLoader ctxClassLoader = GetTCLAction.getContextClassLoader(); 74 synchronized (encs) 75 { 76 Context compCtx = (Context ) encs.get(ctxClassLoader); 77 78 81 if (compCtx == null) 82 { 83 ClassLoader loader = ctxClassLoader; 84 GetParentAction action = new GetParentAction(ctxClassLoader); 85 while( loader != null && loader != topLoader && compCtx == null ) 86 { 87 compCtx = (Context ) encs.get(loader); 88 loader = action.getParent(); 89 } 90 if( compCtx == null ) 92 { 93 NamingServer srv = new NamingServer(); 94 compCtx = new NamingContext(environment, null, srv); 95 encs.put(ctxClassLoader, compCtx); 96 } 97 } 98 return compCtx; 99 } 100 } 101 102 private static class GetTCLAction implements PrivilegedAction 103 { 104 static PrivilegedAction ACTION = new GetTCLAction(); 105 public Object run() 106 { 107 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 108 return loader; 109 } 110 static ClassLoader getContextClassLoader() 111 { 112 ClassLoader loader = (ClassLoader ) AccessController.doPrivileged(ACTION); 113 return loader; 114 } 115 } 116 117 private static class GetParentAction implements PrivilegedAction 118 { 119 ClassLoader loader; 120 GetParentAction(ClassLoader loader) 121 { 122 this.loader = loader; 123 } 124 public Object run() 125 { 126 ClassLoader parent = null; 127 if( loader != null ) 128 { 129 parent = loader.getParent(); 130 loader = parent; 131 } 132 return parent; 133 } 134 ClassLoader getParent() 135 { 136 ClassLoader parent = (ClassLoader ) AccessController.doPrivileged(this); 137 return parent; 138 } 139 } 140 } 141 | Popular Tags |