1 17 18 package org.apache.geronimo.security; 19 20 import java.security.Policy ; 21 import javax.security.jacc.PolicyConfigurationFactory ; 22 import javax.security.jacc.PolicyContextException ; 23 import org.apache.commons.logging.Log; 24 import org.apache.commons.logging.LogFactory; 25 import org.apache.geronimo.gbean.GBeanInfo; 26 import org.apache.geronimo.gbean.GBeanInfoBuilder; 27 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory; 28 import org.apache.geronimo.security.jacc.PolicyContextHandlerContainerSubject; 29 import org.apache.geronimo.security.jacc.PolicyContextHandlerHttpServletRequest; 30 import org.apache.geronimo.security.jacc.PolicyContextHandlerSOAPMessage; 31 import org.apache.geronimo.security.util.ConfigurationUtil; 32 import org.apache.geronimo.system.serverinfo.ServerInfo; 33 34 35 40 public class SecurityServiceImpl implements SecurityService { 41 42 public static boolean POLICY_INSTALLED = false; 43 44 private final Log log = LogFactory.getLog(SecurityServiceImpl.class); 45 46 49 public static final GeronimoSecurityPermission CONFIGURE = new GeronimoSecurityPermission("configure"); 50 51 public SecurityServiceImpl(ClassLoader classLoader, ServerInfo serverInfo, String policyConfigurationFactory, 52 String policyProvider, String keyStore, String keyStorePassword, 53 String trustStore, String trustStorePassword) 54 throws PolicyContextException , ClassNotFoundException , IllegalAccessException , InstantiationException 55 { 56 57 60 ConfigurationUtil.registerPolicyContextHandler(new PolicyContextHandlerContainerSubject(), true); 61 ConfigurationUtil.registerPolicyContextHandler(new PolicyContextHandlerSOAPMessage(), true); 62 ConfigurationUtil.registerPolicyContextHandler(new PolicyContextHandlerHttpServletRequest(), true); 63 64 if (!POLICY_INSTALLED) { 65 policyProvider = sysOverRide(policyProvider, POLICY_PROVIDER); 66 67 if (policyProvider != null) { 68 Policy policy = (Policy ) classLoader.loadClass(policyProvider).newInstance(); 69 policy.refresh(); 70 Policy.setPolicy(policy); 71 } 72 73 POLICY_INSTALLED = true; 74 } 75 76 policyConfigurationFactory = sysOverRide(policyConfigurationFactory, POLICY_CONFIG_FACTORY); 77 if (policyConfigurationFactory != null) { 78 Thread currentThread = Thread.currentThread(); 79 ClassLoader oldClassLoader = currentThread.getContextClassLoader(); 80 currentThread.setContextClassLoader(classLoader); 81 try { 82 PolicyConfigurationFactory.getPolicyConfigurationFactory(); 83 } finally { 84 currentThread.setContextClassLoader(oldClassLoader); 85 } 86 } 87 if (keyStore != null) sysOverRide(serverInfo.resolveServerPath(keyStore), KEYSTORE); 88 if (keyStorePassword != null) sysOverRide(keyStorePassword, KEYSTORE_PASSWORD); 89 90 if (trustStore != null) sysOverRide(serverInfo.resolveServerPath(trustStore), TRUSTSTORE); 91 if (trustStorePassword != null) sysOverRide(trustStorePassword, TRUSTSTORE_PASSWORD); 92 93 log.debug(KEYSTORE + ": " + System.getProperty(KEYSTORE)); 94 log.debug(TRUSTSTORE + ": " + System.getProperty(TRUSTSTORE)); 95 96 log.debug("JACC factory registered"); 97 } 98 99 private String sysOverRide(String attribute, String sysVar) { 100 101 String sysValue = System.getProperty(sysVar); 102 103 106 if (sysValue != null) 107 return sysValue; 108 109 if (attribute != null) { 110 System.setProperty(sysVar, attribute); 111 } 112 113 return attribute; 114 115 } 116 117 public static final GBeanInfo GBEAN_INFO; 118 119 static { 120 GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(SecurityServiceImpl.class); 121 122 infoFactory.addAttribute("classLoader", ClassLoader .class, false); 123 infoFactory.addReference("ServerInfo", ServerInfo.class, NameFactory.GERONIMO_SERVICE); 124 infoFactory.addAttribute("policyConfigurationFactory", String .class, true); 125 infoFactory.addAttribute("policyProvider", String .class, true); 126 infoFactory.addAttribute("keyStore", String .class, true); 127 infoFactory.addAttribute("keyStorePassword", String .class, true); 128 infoFactory.addAttribute("trustStore", String .class, true); 129 infoFactory.addAttribute("trustStorePassword", String .class, true); 130 131 infoFactory.setConstructor(new String []{"classLoader", "ServerInfo", "policyConfigurationFactory", 132 "policyProvider", "keyStore", "keyStorePassword", "trustStore", 133 "trustStorePassword"}); 134 135 GBEAN_INFO = infoFactory.getBeanInfo(); 136 } 137 138 public static GBeanInfo getGBeanInfo() { 139 return GBEAN_INFO; 140 } 141 } 142 | Popular Tags |