001    /**
002     *
003     * Copyright 2003-2004 The Apache Software Foundation
004     *
005     *  Licensed under the Apache License, Version 2.0 (the "License");
006     *  you may not use this file except in compliance with the License.
007     *  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    package org.apache.geronimo.security.jaas;
018    
019    import java.util.Properties;
020    
021    import org.apache.geronimo.gbean.GBeanInfo;
022    import org.apache.geronimo.gbean.GBeanInfoBuilder;
023    import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
024    import org.apache.geronimo.kernel.Kernel;
025    import org.apache.geronimo.security.jaas.server.JaasLoginServiceMBean;
026    import org.apache.geronimo.security.jaas.server.JaasLoginModuleConfiguration;
027    import org.apache.geronimo.security.jaas.client.JaasLoginCoordinator;
028    
029    
030    /**
031     * Creates a LoginModule configuration that will connect a server-side
032     * component to a security realm.  The same thing could be done with a
033     * LoginModuleGBean and a DirectConfigurationEntry, but this method saves some
034     * configuration effort.
035     *
036     * @version $Rev: 355877 $ $Date: 2005-12-10 18:48:27 -0800 (Sat, 10 Dec 2005) $
037     */
038    public class ServerRealmConfigurationEntry implements ConfigurationEntryFactory {
039        private final String applicationConfigName;
040        private final String realmName;
041        private final Kernel kernel;
042        private final JaasLoginServiceMBean loginService;
043        private boolean wrapPrincipals;
044    
045        public ServerRealmConfigurationEntry() {
046            this.applicationConfigName = null;
047            this.realmName = null;
048            this.kernel = null;
049            this.loginService = null;
050        }
051    
052        public ServerRealmConfigurationEntry(String applicationConfigName, String realmName, Kernel kernel, JaasLoginServiceMBean loginService) {
053            this.applicationConfigName = applicationConfigName;
054            this.realmName = realmName;
055            if (applicationConfigName == null || realmName == null) {
056                throw new IllegalArgumentException("applicationConfigName and realmName are required");
057            }
058            if (applicationConfigName.equals(realmName)) {
059                throw new IllegalArgumentException("applicationConfigName must be different than realmName (there's an automatic entry using the same name as the realm name, so you don't need a ServerRealmConfigurationEntry if you're just going to use that!)");
060            }
061            this.kernel = kernel;
062            this.loginService = loginService;
063        }
064    
065        public String getConfigurationName() {
066            return applicationConfigName;
067        }
068    
069        public boolean isWrapPrincipals() {
070            return wrapPrincipals;
071        }
072    
073        public void setWrapPrincipals(boolean wrapPrincipals) {
074            this.wrapPrincipals = wrapPrincipals;
075        }
076    
077        public JaasLoginModuleConfiguration generateConfiguration() {
078            Properties options = new Properties();
079            options.put(JaasLoginCoordinator.OPTION_REALM, realmName);
080            options.put(JaasLoginCoordinator.OPTION_KERNEL, kernel.getKernelName());
081            if (loginService != null) {
082                options.put(JaasLoginCoordinator.OPTION_SERVICENAME, loginService.getObjectName());
083            }
084    
085            options.put("realm", realmName);
086            options.put("kernel", kernel.getKernelName());
087    
088            return new JaasLoginModuleConfiguration(JaasLoginCoordinator.class.getName(), LoginModuleControlFlag.REQUIRED, options, true, applicationConfigName, wrapPrincipals, JaasLoginCoordinator.class.getClassLoader());
089        }
090    
091        public static final GBeanInfo GBEAN_INFO;
092    
093        static {
094            GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(ServerRealmConfigurationEntry.class, NameFactory.CONFIGURATION_ENTRY);
095            infoFactory.addInterface(ConfigurationEntryFactory.class);
096            infoFactory.addAttribute("applicationConfigName", String.class, true);
097            infoFactory.addAttribute("realmName", String.class, true);
098            infoFactory.addAttribute("kernel", Kernel.class, false);
099            infoFactory.addReference("LoginService", JaasLoginServiceMBean.class, "JaasLoginService");
100            infoFactory.addAttribute("wrapPrincipals", Boolean.TYPE, true);
101    
102            infoFactory.setConstructor(new String[]{"applicationConfigName", "realmName", "kernel", "LoginService"});
103            GBEAN_INFO = infoFactory.getBeanInfo();
104        }
105    
106        public static GBeanInfo getGBeanInfo() {
107            return GBEAN_INFO;
108        }
109    
110    }