KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > security > auth > login > DynamicLoginConfig


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.auth.login;
8
9 import java.net.URL JavaDoc;
10 import javax.management.MBeanServer JavaDoc;
11 import javax.management.ObjectName JavaDoc;
12
13 import org.jboss.system.ServiceMBeanSupport;
14 import org.jboss.deployment.DeploymentException;
15
16 /** A security config mbean that loads an xml login configuration using the
17  XMLLoginConfig.loadConfig(URL config) operation on start, and unloads
18  the contained login module configurations on stop.
19
20  <server>
21    <mbean code="org.jboss.security.auth.login.DynamicLoginConfig"
22       name="...">
23       <attribute name="AuthConfig">login-config.xml</attribute>
24       <!-- The service which supports dynamic processing of login-config.xml
25          configurations.
26       -->
27       <depends optional-attribute-name="LoginConfigService">
28          jboss.security:service=XMLLoginConfig
29       </depends>
30       <!-- Optionally specify the security mgr service to use when
31          this service is stopped to flush the auth caches of the domains
32          registered by this service.
33       -->
34       <depends optional-attribute-name="SecurityManagerService">
35          jboss.security:service=JaasSecurityManager
36       </depends>
37    </mbean>
38  </server>
39  
40  @see org.jboss.security.auth.login.XMLLoginConfig
41
42  @author Scott.Stark@jboss.org
43  @version $Revision: 1.4 $
44  */

45 public class DynamicLoginConfig extends ServiceMBeanSupport
46    implements DynamicLoginConfigMBean
47 {
48    /** The JAAS login config file resource to load */
49    private String JavaDoc authConf = "login-config.xml";
50    /** The name of the XMLLoginConfig to use to load the login configs */
51    private ObjectName JavaDoc loginConfigService;
52    /** The name of the SecurityMgrService to use for cache flushes */
53    private ObjectName JavaDoc securityMgrService;
54    /** The names of the login module configs loaded during start */
55    private String JavaDoc[] configNames;
56
57    public DynamicLoginConfig()
58    {
59    }
60
61    public String JavaDoc getName()
62    {
63       return "Dynamic JAAS Login Config";
64    }
65
66    public ObjectName JavaDoc getLoginConfigService()
67    {
68       return loginConfigService;
69    }
70    /** Get the XMLLoginConfig service to use for loading. This service must
71     * support a String[] loadConfig(URL) operation to load the configurations.
72     *
73     * @param serviceName - the XMLLoginConfig service name.
74     */

75    public void setLoginConfigService(ObjectName JavaDoc serviceName)
76    {
77       this.loginConfigService = serviceName;
78    }
79
80    public ObjectName JavaDoc getSecurityManagerService()
81    {
82       return securityMgrService;
83    }
84    /** Set the SecurityManagerService used to flush the registered security
85     * domains. This service must support an flushAuthenticationCache(String)
86     * operation to flush the case for the argument security domain. Setting
87     * this triggers the flush of the authentication caches when the service
88     * is stopped.
89     * @param serviceName - the SecurityManagerService service name.
90     */

91    public void setSecurityManagerService(ObjectName JavaDoc serviceName)
92    {
93       this.securityMgrService = serviceName;
94    }
95
96    /** Get the resource path to the JAAS login configuration file to use.
97     */

98    public String JavaDoc getAuthConfig()
99    {
100       return authConf;
101    }
102
103    /** Set the resource path to the JAAS login configuration file to use.
104     The default is "login-config.xml".
105     */

106    public void setAuthConfig(String JavaDoc authConf)
107    {
108       this.authConf = authConf;
109    }
110
111    /** Go through the registered login config names and flush the auth
112     * caches if there is a registered SecurityManagerService.
113     *
114     * @throws Exception
115     */

116    public void flushAuthenticationCaches() throws Exception JavaDoc
117    {
118       if( this.securityMgrService != null && server.isRegistered(securityMgrService))
119       {
120          int count = configNames == null ? 0 : configNames.length;
121          String JavaDoc[] sig = {String JavaDoc.class.getName()};
122          for(int n = 0; n < count; n ++)
123          {
124             Object JavaDoc[] args = {configNames[n]};
125             server.invoke(securityMgrService, "flushAuthenticationCache", args, sig);
126             log.debug("Flushed domain: "+configNames[n]);
127          }
128       }
129    }
130
131    /** Start the service. This entails loading the AuthConf file contents
132     * using the LoginConfigService.
133     */

134    protected void startService() throws Exception JavaDoc
135    {
136       // Look for the authConf as resource
137
ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
138       URL JavaDoc loginConfig = loader.getResource(authConf);
139       if( loginConfig != null )
140       {
141          log.debug("Using JAAS AuthConfig: "+loginConfig.toExternalForm());
142          MBeanServer JavaDoc server = super.getServer();
143          Object JavaDoc[] args = {loginConfig};
144          String JavaDoc[] sig = {URL JavaDoc.class.getName()};
145          configNames = (String JavaDoc[]) server.invoke(loginConfigService,
146             "loadConfig", args, sig);
147          int count = configNames == null ? 0 : configNames.length;
148          for(int n = 0; n < count; n ++)
149          {
150             log.debug("Loaded config: "+configNames[n]);
151          }
152       }
153       else
154       {
155          throw new DeploymentException("Failed to find authConf as resource: "+authConf);
156       }
157    }
158
159    /** Start the service. This entails unloading the AuthConf file contents
160     * using the LoginConfigService.
161     */

162    protected void stopService() throws Exception JavaDoc
163    {
164       MBeanServer JavaDoc server = super.getServer();
165       flushAuthenticationCaches();
166       if( configNames != null && configNames.length > 0 )
167       {
168          Object JavaDoc[] args = {configNames};
169          String JavaDoc[] sig = {configNames.getClass().getName()};
170          server.invoke(loginConfigService, "removeConfigs", args, sig);
171       }
172    }
173 }
174
Popular Tags