KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > security > plugins > SecurityConfig


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.security.plugins;
8
9 import java.io.InputStream JavaDoc;
10 import java.io.IOException JavaDoc;
11 import java.io.PrintWriter JavaDoc;
12 import java.io.StringWriter JavaDoc;
13 import java.net.URL JavaDoc;
14 import java.util.Stack JavaDoc;
15 import javax.management.JMException JavaDoc;
16 import javax.management.MalformedObjectNameException JavaDoc;
17 import javax.management.MBeanServer JavaDoc;
18 import javax.management.ObjectName JavaDoc;
19 import javax.security.auth.login.Configuration JavaDoc;
20
21 import org.jboss.system.ServiceMBeanSupport;
22
23 /** The SecurityConfigMBean implementation. This class needs the
24  javax.security.auth.AuthPermission("setLoginConfiguration") to install
25  the javax.security.auth.login.Configuration when running with a security
26  manager.
27  
28  @author Scott.Stark@jboss.org
29  @version $Revision: 1.4 $
30  */

31 public class SecurityConfig extends ServiceMBeanSupport
32    implements SecurityConfigMBean
33 {
34    // Constants -----------------------------------------------------
35

36    // Attributes ----------------------------------------------------
37
/** The default Configuration mbean name */
38    private String JavaDoc loginConfigName;
39    /** The stack of Configuration mbeans that are active */
40    private Stack JavaDoc loginConfigStack = new Stack JavaDoc();
41
42    static class ConfigInfo
43    {
44       ObjectName JavaDoc name;
45       Configuration JavaDoc config;
46       ConfigInfo(ObjectName JavaDoc name, Configuration JavaDoc config)
47       {
48          this.name = name;
49          this.config = config;
50       }
51    }
52
53    public SecurityConfig()
54    {
55    }
56    
57    public String JavaDoc getName()
58    {
59       return "SecurityIntialization";
60    }
61
62    /** Get the name of the mbean that provides the default JAAS login configuration
63     */

64    public String JavaDoc getLoginConfig()
65    {
66       return loginConfigName;
67    }
68
69    /** Set the name of the mbean that provides the default JAAS login configuration
70     */

71    public void setLoginConfig(String JavaDoc name) throws MalformedObjectNameException JavaDoc
72    {
73       this.loginConfigName = name;
74    }
75
76    /** Start the configuration service by pushing the mbean given by the
77     LoginConfig onto the configuration stack.
78     */

79    public void startService() throws Exception JavaDoc
80    {
81       pushLoginConfig(loginConfigName);
82    }
83
84    /** Start the configuration service by poping the top of the
85     configuration stack.
86     */

87    public void stopService() throws Exception JavaDoc
88    {
89       if( loginConfigStack.empty() == false )
90          popLoginConfig();
91    }
92
93    /** Push an mbean onto the login configuration stack and install its
94     Configuration as the current instance.
95     @see javax.security.auth.login.Configuration
96     */

97    public synchronized void pushLoginConfig(String JavaDoc objectName)
98       throws JMException JavaDoc, MalformedObjectNameException JavaDoc
99    {
100       ObjectName JavaDoc name = new ObjectName JavaDoc(objectName);
101       Configuration JavaDoc prevConfig = null;
102       if( loginConfigStack.empty() == false )
103       {
104          ConfigInfo prevInfo = (ConfigInfo) loginConfigStack.peek();
105          prevConfig = prevInfo.config;
106       }
107
108       ConfigInfo info = installConfig(name, prevConfig);
109       loginConfigStack.push(info);
110    }
111    /** Pop the current mbean from the login configuration stack and install
112     the previous Configuration as the current instance.
113     @see javax.security.auth.login.Configuration
114     */

115    public synchronized void popLoginConfig()
116       throws JMException JavaDoc
117    {
118       ConfigInfo info = (ConfigInfo) loginConfigStack.pop();
119       Configuration JavaDoc prevConfig = null;
120       if( loginConfigStack.empty() == false )
121       {
122          ConfigInfo prevInfo = (ConfigInfo) loginConfigStack.peek();
123          prevConfig = prevInfo.config;
124       }
125
126       installConfig(info.name, prevConfig);
127    }
128
129    /** Obtain the Configuration from the named mbean using its getConfiguration
130     operation and install it as the current Configuration.
131
132     @see Configuration.setConfiguration(javax.security.auth.login.Configuration)
133     */

134    private ConfigInfo installConfig(ObjectName JavaDoc name, Configuration JavaDoc prevConfig)
135       throws JMException JavaDoc
136    {
137       MBeanServer JavaDoc server = super.getServer();
138       Object JavaDoc[] args = {prevConfig};
139       String JavaDoc[] signature = {"javax.security.auth.login.Configuration"};
140       Configuration JavaDoc config = (Configuration JavaDoc) server.invoke(name,
141          "getConfiguration", args, signature);
142       Configuration.setConfiguration(config);
143       ConfigInfo info = new ConfigInfo(name, config);
144       log.debug("Installed JAAS Configuration service="+name+", config="+config);
145       return info;
146    }
147 }
148
Popular Tags