1 7 package org.jboss.ejb3.security; 8 9 import java.security.SecurityPermission ; 10 import java.security.AccessController ; 11 import java.security.PrivilegedExceptionAction ; 12 import java.security.PrivilegedActionException ; 13 14 import javax.security.jacc.PolicyContextException ; 15 import javax.security.jacc.PolicyConfiguration ; 16 import javax.security.jacc.PolicyConfigurationFactory ; 17 18 import org.jboss.logging.Logger; 19 20 25 public abstract class Ejb3PolicyConfigurationFactory 26 { 27 private static final Logger log = Logger.getLogger(Ejb3PolicyConfigurationFactory.class); 28 29 32 private static final String FACTORY_PROP = 33 "javax.security.jacc.PolicyConfigurationFactory.provider"; 34 35 private static final String DEFAULT_FACTORY_NAME = 36 "org.jboss.security.jacc.JBossPolicyConfigurationFactory"; 37 38 private static PolicyConfigurationFactory factory; 39 40 55 public static PolicyConfigurationFactory getPolicyConfigurationFactory() 56 throws ClassNotFoundException , PolicyContextException 57 { 58 59 SecurityManager sm = System.getSecurityManager(); 61 if (sm != null) 62 sm.checkPermission(new SecurityPermission ("setPolicy")); 63 64 if (factory == null) 65 { 66 String factoryName = null; 67 Class clazz = null; 68 try 69 { 70 LoadAction action = new LoadAction(); 71 try 72 { 73 clazz = (Class ) AccessController.doPrivileged(action); 74 factoryName = action.getName(); 75 } 76 catch (PrivilegedActionException ex) 77 { 78 ex.printStackTrace(); 79 factoryName = action.getName(); 80 Exception e = ex.getException(); 81 if (e instanceof ClassNotFoundException ) 82 throw (ClassNotFoundException ) e; 83 else 84 throw new PolicyContextException ("Failure during load of class: "+action.getName(), e); 85 } 86 87 factory = (PolicyConfigurationFactory ) clazz.newInstance(); 88 } 89 catch (ClassNotFoundException e) 90 { 91 String msg = "Failed to find PolicyConfigurationFactory : " + factoryName; 92 throw new ClassNotFoundException (msg, e); 93 } 94 catch (IllegalAccessException e) 95 { 96 String msg = "Unable to access class : " + factoryName; 97 throw new PolicyContextException (msg, e); 98 } 99 catch (InstantiationException e) 100 { 101 String msg = "Failed to create instance of: " + factoryName; 102 throw new PolicyContextException (msg, e); 103 } 104 catch (ClassCastException e) 105 { 106 StringBuffer msg = new StringBuffer (factoryName + " Is not a PolicyConfigurationFactory, "); 107 msg.append("PCF.class.CL: "+Ejb3PolicyConfigurationFactory.class.getClassLoader()); 108 msg.append("\nPCF.class.CS: "+Ejb3PolicyConfigurationFactory.class.getProtectionDomain().getCodeSource()); 109 msg.append("\nPCF.class.hash: "+System.identityHashCode(Ejb3PolicyConfigurationFactory.class)); 110 msg.append("\nclazz.CL: "+clazz.getClassLoader()); 111 msg.append("\nclazz.CS: "+clazz.getProtectionDomain().getCodeSource()); 112 msg.append("\nclazz.super.CL: "+clazz.getSuperclass().getClassLoader()); 113 msg.append("\nclazz.super.CS: "+clazz.getSuperclass().getProtectionDomain().getCodeSource()); 114 msg.append("\nclazz.super.hash: "+System.identityHashCode(clazz.getSuperclass())); 115 ClassCastException cce = new ClassCastException (msg.toString()); 116 cce.initCause(e); 117 } 118 } 119 return factory; 120 } 121 122 155 public abstract PolicyConfiguration getPolicyConfiguration(String contextID, 156 boolean remove) 157 throws PolicyContextException ; 158 159 167 public abstract boolean inService(String contextID) 168 throws PolicyContextException ; 169 170 174 private static class LoadAction implements PrivilegedExceptionAction 175 { 176 private String name; 177 public String getName() 178 { 179 return name; 180 } 181 public Object run() 182 throws Exception 183 { 184 name = System.getProperty(FACTORY_PROP); 185 if( name == null ) 186 { 187 name = DEFAULT_FACTORY_NAME; 189 } 190 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 191 Class factoryClass = loader.loadClass(name); 192 return factoryClass; 193 } 194 } 195 } 196 | Popular Tags |