View Javadoc

1   /**
2    *
3    * Copyright 2003-2004 The Apache Software Foundation
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  package org.apache.geronimo.security.jaas;
18  
19  import java.util.Properties;
20  
21  import org.apache.geronimo.gbean.GBeanInfo;
22  import org.apache.geronimo.gbean.GBeanInfoBuilder;
23  import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
24  import org.apache.geronimo.kernel.Kernel;
25  import org.apache.geronimo.security.jaas.server.JaasLoginServiceMBean;
26  import org.apache.geronimo.security.jaas.server.JaasLoginModuleConfiguration;
27  import org.apache.geronimo.security.jaas.client.JaasLoginCoordinator;
28  
29  
30  /**
31   * Creates a LoginModule configuration that will connect a server-side
32   * component to a security realm.  The same thing could be done with a
33   * LoginModuleGBean and a DirectConfigurationEntry, but this method saves some
34   * configuration effort.
35   *
36   * @version $Rev: 355877 $ $Date: 2005-12-10 18:48:27 -0800 (Sat, 10 Dec 2005) $
37   */
38  public class ServerRealmConfigurationEntry implements ConfigurationEntryFactory {
39      private final String applicationConfigName;
40      private final String realmName;
41      private final Kernel kernel;
42      private final JaasLoginServiceMBean loginService;
43      private boolean wrapPrincipals;
44  
45      public ServerRealmConfigurationEntry() {
46          this.applicationConfigName = null;
47          this.realmName = null;
48          this.kernel = null;
49          this.loginService = null;
50      }
51  
52      public ServerRealmConfigurationEntry(String applicationConfigName, String realmName, Kernel kernel, JaasLoginServiceMBean loginService) {
53          this.applicationConfigName = applicationConfigName;
54          this.realmName = realmName;
55          if (applicationConfigName == null || realmName == null) {
56              throw new IllegalArgumentException("applicationConfigName and realmName are required");
57          }
58          if (applicationConfigName.equals(realmName)) {
59              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!)");
60          }
61          this.kernel = kernel;
62          this.loginService = loginService;
63      }
64  
65      public String getConfigurationName() {
66          return applicationConfigName;
67      }
68  
69      public boolean isWrapPrincipals() {
70          return wrapPrincipals;
71      }
72  
73      public void setWrapPrincipals(boolean wrapPrincipals) {
74          this.wrapPrincipals = wrapPrincipals;
75      }
76  
77      public JaasLoginModuleConfiguration generateConfiguration() {
78          Properties options = new Properties();
79          options.put(JaasLoginCoordinator.OPTION_REALM, realmName);
80          options.put(JaasLoginCoordinator.OPTION_KERNEL, kernel.getKernelName());
81          if (loginService != null) {
82              options.put(JaasLoginCoordinator.OPTION_SERVICENAME, loginService.getObjectName());
83          }
84  
85          options.put("realm", realmName);
86          options.put("kernel", kernel.getKernelName());
87  
88          return new JaasLoginModuleConfiguration(JaasLoginCoordinator.class.getName(), LoginModuleControlFlag.REQUIRED, options, true, applicationConfigName, wrapPrincipals, JaasLoginCoordinator.class.getClassLoader());
89      }
90  
91      public static final GBeanInfo GBEAN_INFO;
92  
93      static {
94          GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(ServerRealmConfigurationEntry.class, NameFactory.CONFIGURATION_ENTRY);
95          infoFactory.addInterface(ConfigurationEntryFactory.class);
96          infoFactory.addAttribute("applicationConfigName", String.class, true);
97          infoFactory.addAttribute("realmName", String.class, true);
98          infoFactory.addAttribute("kernel", Kernel.class, false);
99          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 }