001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.security.service;
023:
024: import java.net.MalformedURLException;
025: import java.net.URL;
026: import java.util.Hashtable;
027: import javax.management.MBeanServer;
028: import javax.management.ObjectName;
029: import javax.security.auth.login.Configuration;
030:
031: import org.jboss.security.auth.login.XMLLoginConfig;
032: import org.jboss.system.ServiceMBeanSupport;
033:
034: /** A security config mbean that loads an xml login configuration and
035: pushes a XMLLoginConfig instance onto the the config stack managed by
036: the SecurityConfigName mbean(default=jboss.security:name=SecurityConfig).
037:
038: @author Scott.Stark@jboss.org
039: @version $Revision: 57211 $
040: */
041: public class SecurityConfig extends ServiceMBeanSupport implements
042: SecurityConfigMBean {
043: // Constants -----------------------------------------------------
044:
045: // Attributes ----------------------------------------------------
046: private String authConf = "login-config.xml";
047: private XMLLoginConfig config = null;
048: private ObjectName mainSecurityConfig;
049:
050: // Static --------------------------------------------------------
051:
052: // Constructors --------------------------------------------------
053: public SecurityConfig() {
054: setSecurityConfigName("jboss.security:name=SecurityConfig");
055: }
056:
057: public String getName() {
058: return "JAAS Login Config";
059: }
060:
061: public String getSecurityConfigName() {
062: return mainSecurityConfig.toString();
063: }
064:
065: public void setSecurityConfigName(String objectName) {
066: try {
067: mainSecurityConfig = new ObjectName(objectName);
068: } catch (Exception e) {
069: log.error("Failed to create ObjectName", e);
070: }
071: }
072:
073: /** Get the resource path to the JAAS login configuration file to use.
074: */
075: public String getAuthConfig() {
076: return authConf;
077: }
078:
079: /** Set the resource path to the JAAS login configuration file to use.
080: The default is "login-config.xml".
081: */
082: public void setAuthConfig(String authConf) {
083: this .authConf = authConf;
084: }
085:
086: // Public --------------------------------------------------------
087: /** Start the service. This entails
088: */
089: protected void startService() throws Exception {
090: // Look for the authConf as resource
091: ClassLoader loader = Thread.currentThread()
092: .getContextClassLoader();
093: URL loginConfig = loader.getResource(authConf);
094: if (loginConfig != null) {
095: log.info("Using JAAS AuthConfig: "
096: + loginConfig.toExternalForm());
097: config = new XMLLoginConfig();
098: config.setConfigURL(loginConfig);
099: config.start();
100: MBeanServer server = super .getServer();
101: ObjectName name = super .getServiceName();
102: Hashtable props = name.getKeyPropertyList();
103: props.put("testConfig", "XMLLoginConfig");
104: name = new ObjectName(name.getDomain(), props);
105: server.registerMBean(config, name);
106: Object[] args = { name.toString() };
107: String[] sig = { String.class.getName() };
108: server.invoke(mainSecurityConfig, "pushLoginConfig", args,
109: sig);
110: } else {
111: log.warn("No AuthConfig resource found");
112: }
113: }
114:
115: protected void stopService() throws Exception {
116: MBeanServer server = super .getServer();
117: ObjectName name = super .getServiceName();
118: Hashtable props = name.getKeyPropertyList();
119: props.put("testConfig", "XMLLoginConfig");
120: name = new ObjectName(name.getDomain(), props);
121: Object[] args = {};
122: String[] sig = {};
123: server.invoke(mainSecurityConfig, "popLoginConfig", args, sig);
124: server.unregisterMBean(name);
125: }
126: }
|