1 /*
2 * JBoss, the OpenSource EJB server
3 *
4 * Distributable under LGPL license.
5 * See terms of license at gnu.org.
6 */
7
8 package org.jboss.security.plugins;
9
10 import java.io.FileNotFoundException;
11 import java.net.URL;
12 import javax.naming.InitialContext;
13 import javax.naming.Reference;
14 import javax.naming.StringRefAddr;
15 import javax.security.auth.Policy;
16 import javax.security.auth.login.Configuration;
17
18 import org.jboss.naming.NonSerializableFactory;
19 import org.jboss.security.SecurityPolicy;
20 import org.jboss.security.SecurityPolicyParser;
21 import org.jboss.system.ServiceMBeanSupport;
22
23 /** The implementation class for the JMX SecurityPolicyServiceMBean. This
24 service creates a SecurityPolicy instance using a xml based policy store.
25
26 @author Scott.Stark@jboss.org
27 @version $Revision: 1.4 $
28 */
29 public class SecurityPolicyService extends ServiceMBeanSupport implements SecurityPolicyServiceMBean
30 {
31 private String jndiName = "DefaultSecurityPolicy";
32 private SecurityPolicy securityPolicy;
33 private SecurityPolicyParser policySource;
34 private String policyFile;
35
36 /** Get the jndi name under which the SRPServerInterface proxy should be bound
37 */
38 public String getJndiName()
39 {
40 return jndiName;
41 }
42 /** Set the jndi name under which the SRPServerInterface proxy should be bound
43 */
44 public void setJndiName(String jndiName)
45 {
46 this.jndiName = jndiName;
47 }
48
49 public String getPolicyFile()
50 {
51 return policyFile;
52 }
53 public void setPolicyFile(String policyFile)
54 {
55 this.policyFile = policyFile;
56 }
57
58 public String getName()
59 {
60 return "SecurityPolicyService";
61 }
62
63 public void startService() throws Exception
64 {
65 ClassLoader loader = Thread.currentThread().getContextClassLoader();
66 URL policyURL = loader.getResource(policyFile);
67 if( policyURL == null )
68 throw new FileNotFoundException("Failed to find URL for policy resource: "+policyFile);
69 System.out.println("Loading policy file from: "+policyURL);
70 policySource = new SecurityPolicyParser(policyURL);
71 securityPolicy = new SecurityPolicy(policySource);
72 policySource.refresh();
73
74 InitialContext ctx = new InitialContext();
75 NonSerializableFactory.rebind(jndiName, securityPolicy);
76
77 // Bind a reference to securityPolicy using NonSerializableFactory as the ObjectFactory
78 String className = securityPolicy.getClass().getName();
79 String factory = NonSerializableFactory.class.getName();
80 StringRefAddr addr = new StringRefAddr("nns", jndiName);
81 Reference memoryRef = new Reference(className, addr, factory, null);
82 ctx.rebind(jndiName, memoryRef);
83
84 // Install securityPolicy as the JAAS Policy
85 Policy.setPolicy(securityPolicy);
86 // Install securityPolicy as the JAAS Configuration
87 Configuration.setConfiguration(securityPolicy.getLoginConfiguration());
88 }
89
90 }