1 8 package org.jboss.security.jacc; 9 10 import java.security.Policy ; 11 import java.security.PrivilegedAction ; 12 import java.security.AccessController ; 13 import java.lang.reflect.Constructor ; 14 15 import javax.management.MBeanServer ; 16 import javax.management.ObjectName ; 17 import javax.security.jacc.PolicyContext ; 18 19 import org.jboss.logging.Logger; 20 21 30 public class SecurityService 31 { 32 33 private static final String JACC_POLICY_PROVIDER = "javax.security.jacc.policy.provider"; 34 private static final Logger log = Logger.getLogger(SecurityService.class); 35 36 37 private Policy oldPolicy; 38 39 private Policy jaccPolicy; 40 41 private ObjectName policyName; 42 43 44 private String policyAttributeName = "Policy"; 45 private MBeanServer server; 46 47 public ObjectName getPolicyName() 48 { 49 return policyName; 50 } 51 public void setPolicyName(ObjectName policyName) 52 { 53 this.policyName = policyName; 54 } 55 56 public String getPolicyAttributeName() 57 { 58 return policyAttributeName; 59 } 60 public void setPolicyAttributeName(String policyAttributeName) 61 { 62 this.policyAttributeName = policyAttributeName; 63 } 64 65 public MBeanServer getMBeanServer() 66 { 67 return server; 68 } 69 public void setMBeanServer(MBeanServer server) 70 { 71 this.server = server; 72 } 73 74 81 public void start() throws Exception 82 { 83 oldPolicy = Policy.getPolicy(); 85 86 if( server != null && policyName != null && server.isRegistered(policyName) ) 88 { 89 try 91 { 92 jaccPolicy = (Policy ) server.getAttribute(policyName, policyAttributeName); 93 } 94 catch(Exception e) 95 { 96 log.warn("Failed to get " + policyAttributeName 97 + " attribute from: " + policyName, e); 98 } 99 } 100 101 if( jaccPolicy == null ) 103 { 104 String provider = getProperty(JACC_POLICY_PROVIDER, 105 "org.jboss.security.jacc.DelegatingPolicy"); 106 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 107 Class providerClass = loader.loadClass(provider); 108 try 109 { 110 Class [] ctorSig = {Policy .class}; 112 Constructor ctor = providerClass.getConstructor(ctorSig); 113 Object [] ctorArgs = {oldPolicy}; 114 jaccPolicy = (Policy ) ctor.newInstance(ctorArgs); 115 } 116 catch(NoSuchMethodException e) 117 { 118 log.debug("Provider does not support ctor(Policy)"); 119 jaccPolicy = (Policy ) providerClass.newInstance(); 120 } 121 } 122 123 Policy.setPolicy(jaccPolicy); 125 126 jaccPolicy.refresh(); 128 129 SubjectPolicyContextHandler handler = new SubjectPolicyContextHandler(); 131 PolicyContext.registerHandler(SubjectPolicyContextHandler.SUBJECT_CONTEXT_KEY, 132 handler, true); 133 } 134 135 public void stop() throws Exception 136 { 137 if( jaccPolicy != null ) 139 Policy.setPolicy(oldPolicy); 140 } 141 142 static class PropertyAccessAction implements PrivilegedAction 143 { 144 private String name; 145 private String defaultValue; 146 PropertyAccessAction(String name, String defaultValue) 147 { 148 this.name = name; 149 this.defaultValue = defaultValue; 150 } 151 public Object run() 152 { 153 return System.getProperty(name, defaultValue); 154 } 155 } 156 157 static String getProperty(String name) 158 { 159 return getProperty(name, null); 160 } 161 162 static String getProperty(String name, String defaultValue) 163 { 164 PrivilegedAction action = new PropertyAccessAction(name, defaultValue); 165 String property = (String ) AccessController.doPrivileged(action); 166 return property; 167 } 168 } 169 | Popular Tags |