KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > security > service > SecurityConfig


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.security.service;
23
24 import java.net.MalformedURLException JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.Hashtable JavaDoc;
27 import javax.management.MBeanServer JavaDoc;
28 import javax.management.ObjectName JavaDoc;
29 import javax.security.auth.login.Configuration JavaDoc;
30
31 import org.jboss.security.auth.login.XMLLoginConfig;
32 import org.jboss.system.ServiceMBeanSupport;
33
34 /** A security config mbean that loads an xml login configuration and
35  pushes a XMLLoginConfig instance onto the the config stack managed by
36  the SecurityConfigName mbean(default=jboss.security:name=SecurityConfig).
37  
38  @author Scott.Stark@jboss.org
39  @version $Revision: 37406 $
40  */

41 public class SecurityConfig extends ServiceMBeanSupport
42    implements SecurityConfigMBean
43 {
44    // Constants -----------------------------------------------------
45

46    // Attributes ----------------------------------------------------
47
private String JavaDoc authConf = "login-config.xml";
48    private XMLLoginConfig config = null;
49    private ObjectName JavaDoc mainSecurityConfig;
50
51    // Static --------------------------------------------------------
52

53    // Constructors --------------------------------------------------
54
public SecurityConfig()
55    {
56       setSecurityConfigName("jboss.security:name=SecurityConfig");
57    }
58
59    public String JavaDoc getName()
60    {
61       return "JAAS Login Config";
62    }
63    public String JavaDoc getSecurityConfigName()
64    {
65       return mainSecurityConfig.toString();
66    }
67    public void setSecurityConfigName(String JavaDoc objectName)
68    {
69       try
70       {
71          mainSecurityConfig = new ObjectName JavaDoc(objectName);
72       }
73       catch(Exception JavaDoc e)
74       {
75          log.error("Failed to create ObjectName", e);
76       }
77    }
78
79    /** Get the resource path to the JAAS login configuration file to use.
80     */

81    public String JavaDoc getAuthConfig()
82    {
83       return authConf;
84    }
85
86    /** Set the resource path to the JAAS login configuration file to use.
87     The default is "login-config.xml".
88     */

89    public void setAuthConfig(String JavaDoc authConf)
90    {
91       this.authConf = authConf;
92    }
93
94    // Public --------------------------------------------------------
95
/** Start the service. This entails
96     */

97    protected void startService() throws Exception JavaDoc
98    {
99       // Look for the authConf as resource
100
ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
101       URL JavaDoc loginConfig = loader.getResource(authConf);
102       if( loginConfig != null )
103       {
104          log.info("Using JAAS AuthConfig: "+loginConfig.toExternalForm());
105          config = new XMLLoginConfig();
106          config.setConfigURL(loginConfig);
107          config.start();
108          MBeanServer JavaDoc server = super.getServer();
109          ObjectName JavaDoc name = super.getServiceName();
110          Hashtable JavaDoc props = name.getKeyPropertyList();
111          props.put("testConfig", "XMLLoginConfig");
112          name = new ObjectName JavaDoc(name.getDomain(), props);
113          server.registerMBean(config, name);
114          Object JavaDoc[] args = {name.toString()};
115          String JavaDoc[] sig = {String JavaDoc.class.getName()};
116          server.invoke(mainSecurityConfig, "pushLoginConfig", args, sig);
117       }
118       else
119       {
120          log.warn("No AuthConfig resource found");
121       }
122    }
123
124    protected void stopService() throws Exception JavaDoc
125    {
126       MBeanServer JavaDoc server = super.getServer();
127       ObjectName JavaDoc name = super.getServiceName();
128       Hashtable JavaDoc props = name.getKeyPropertyList();
129       props.put("testConfig", "XMLLoginConfig");
130       name = new ObjectName JavaDoc(name.getDomain(), props);
131       Object JavaDoc[] args = {};
132       String JavaDoc[] sig = {};
133       server.invoke(mainSecurityConfig, "popLoginConfig", args, sig);
134       server.unregisterMBean(name);
135    }
136 }
137
Popular Tags