1 23 package com.sun.enterprise.security; 24 25 import java.security.AccessController ; 26 import java.security.CodeSource ; 27 import java.security.Permission ; 28 import java.security.Policy ; 29 import java.security.PrivilegedExceptionAction ; 30 import java.security.PrivilegedActionException ; 31 import java.util.HashMap ; 32 import java.util.Iterator ; 33 import java.util.logging.Level ; 34 import java.util.logging.Logger ; 35 36 import javax.security.jacc.PolicyContext ; 37 import com.sun.enterprise.J2EESecurityManager; 38 39 import com.sun.logging.LogDomains; 40 41 45 46 public class PermissionCacheFactory { 47 48 private static HashMap cacheMap = new HashMap (); 49 private static int factoryKey = 0; 50 private static boolean supportsReuse = false; 51 52 private static Permission [] protoPerms = { 53 new java.net.SocketPermission ("localhost","connect"), 54 new java.util.PropertyPermission ("x","read") 55 }; 56 57 private static PermissionCache securityManagerCache = 58 createSecurityManagerCache(); 59 60 static { 61 try { 62 Policy policy = Policy.getPolicy(); 66 if (policy != null) { 67 policy.refresh(); 68 } 69 } catch(Exception pe) { 70 } 71 } 72 73 77 private static Integer getNextKey() { 78 79 Integer key = new Integer (factoryKey++); 80 81 while (cacheMap.get(key) != null) { 82 key = new Integer (factoryKey++); 83 } 84 85 return key; 86 } 87 88 private static synchronized PermissionCache createSecurityManagerCache() { 89 90 Integer key = getNextKey(); 91 92 PermissionCache cache = 93 94 new PermissionCache(key, null, null, protoPerms, null); 95 96 return registerPermissionCache(cache); 97 } 98 99 120 public static synchronized PermissionCache 121 createPermissionCache(String pcID, 122 CodeSource codesource, Permission [] perms, String name) { 123 124 if (!supportsReuse) { 125 return null; 126 } 127 128 Integer key = getNextKey(); 129 130 PermissionCache cache = 131 new PermissionCache(key, pcID, codesource, perms, name); 132 133 return registerPermissionCache(cache); 134 } 135 136 157 public static synchronized PermissionCache 158 createPermissionCache(String pcID, 159 CodeSource codesource, Class clazz, String name) { 160 161 if (!supportsReuse) { 162 return null; 163 } 164 165 Integer key = getNextKey(); 166 167 PermissionCache cache = 168 new PermissionCache(key, pcID, codesource, clazz, name); 169 170 return registerPermissionCache(cache); 171 } 172 173 179 private static PermissionCache 180 registerPermissionCache(PermissionCache cache) { 181 cacheMap.put(cache.getFactoryKey(),cache); 182 183 return cache; 184 } 185 186 public static synchronized PermissionCache 187 removePermissionCache(PermissionCache cache) { 188 189 PermissionCache rvalue = null; 190 191 if (cache != null) { 192 193 Object value = cacheMap.remove(cache.getFactoryKey()); 194 195 if (value != null && value instanceof PermissionCache) { 196 rvalue = (PermissionCache) value; 197 rvalue.reset(); 198 } 199 } 200 return rvalue; 201 } 202 203 206 public static synchronized void resetCaches() { 207 208 supportsReuse = true; 209 210 SecurityManager sm = System.getSecurityManager(); 211 if (sm != null && sm instanceof J2EESecurityManager) { 212 if (!((J2EESecurityManager)sm).cacheEnabled()) { 213 ((J2EESecurityManager)sm).enablePermissionCache 214 (securityManagerCache); 215 } 216 } 217 218 Iterator iter = cacheMap.values().iterator(); 219 while (iter.hasNext()) { 220 Object cache = iter.next(); 221 if (cache instanceof PermissionCache) { 222 ((PermissionCache) cache).reset(); 223 } 224 } 225 } 226 } 227 | Popular Tags |