KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > security > jaas > JaasLoginModuleUse


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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.HashMap JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Set JavaDoc;
23
24 import org.apache.geronimo.gbean.GBeanInfo;
25 import org.apache.geronimo.gbean.GBeanInfoBuilder;
26 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
27 import org.apache.geronimo.kernel.Kernel;
28 import org.apache.geronimo.system.serverinfo.ServerInfo;
29 import org.apache.geronimo.security.jaas.server.JaasLoginModuleConfiguration;
30
31
32 /**
33  * Holds a reference to a login module and the control flag. A linked list of these forms the list of login modules
34  * in a GenericSecurityRealm.
35  *
36  * @version $Rev: 486195 $ $Date: 2006-12-12 10:42:02 -0500 (Tue, 12 Dec 2006) $
37  */

38 public class JaasLoginModuleUse implements JaasLoginModuleChain {
39     // See also http://java.sun.com/j2se/1.4.2/docs/guide/security/jaas/JAASLMDevGuide.html for more standard login module option keys
40
public final static String JavaDoc KERNEL_NAME_LM_OPTION = "org.apache.geronimo.security.realm.GenericSecurityRealm.KERNEL";
41     public final static String JavaDoc SERVERINFO_LM_OPTION = "org.apache.geronimo.security.realm.GenericSecurityRealm.SERVERINFO";
42     public final static String JavaDoc CLASSLOADER_LM_OPTION = "org.apache.geronimo.security.realm.GenericSecurityRealm.CLASSLOADER";
43
44     private final LoginModuleSettings loginModule;
45     private final JaasLoginModuleUse next;
46     private LoginModuleControlFlag controlFlag;
47     private final Kernel kernel;
48
49     //for reference.
50
public JaasLoginModuleUse() {
51         loginModule = null;
52         next = null;
53         controlFlag = null;
54         kernel = null;
55     }
56
57     public JaasLoginModuleUse(LoginModuleSettings loginModule, JaasLoginModuleUse next, String JavaDoc controlFlag, Kernel kernel) {
58         this.loginModule = loginModule;
59         this.next = next;
60         LoginModuleControlFlagEditor editor = new LoginModuleControlFlagEditor();
61         editor.setAsText(controlFlag);
62         this.controlFlag = (LoginModuleControlFlag) editor.getValue();
63         this.kernel = kernel;
64     }
65
66     public LoginModuleSettings getLoginModule() {
67         return loginModule;
68     }
69
70     public JaasLoginModuleChain getNext() {
71         return next;
72     }
73
74     public String JavaDoc getLoginModuleName() {
75         //TODO configId which is correct?
76
// return kernel.getAbstractNameFor(loginModule).getObjectName().getCanonicalName();
77
return kernel.getAbstractNameFor(loginModule).toURI().toString();
78     }
79
80     public String JavaDoc getNextName() {
81         if(next == null) {
82             return null;
83         }
84         //TODO configId which is correct?
85
// return kernel.getAbstractNameFor(next).getObjectName().getCanonicalName();
86
return kernel.getAbstractNameFor(next).toURI().toString();
87     }
88
89     public String JavaDoc getControlFlag() {
90         return controlFlag.toString();
91     }
92
93     public void setControlFlag(String JavaDoc controlFlag) {
94         LoginModuleControlFlagEditor ed = new LoginModuleControlFlagEditor();
95         ed.setAsText(controlFlag);
96         this.controlFlag = (LoginModuleControlFlag) ed.getValue();
97     }
98
99     public void configure(Set JavaDoc domainNames, List JavaDoc loginModuleConfigurations, Kernel kernel, ServerInfo serverInfo, ClassLoader JavaDoc classLoader) {
100         Map JavaDoc options = loginModule.getOptions();
101         if (options != null) {
102             options = new HashMap JavaDoc(options);
103         } else {
104             options = new HashMap JavaDoc();
105         }
106         if (kernel != null && !options.containsKey(KERNEL_NAME_LM_OPTION)) {
107             options.put(KERNEL_NAME_LM_OPTION, kernel.getKernelName());
108         }
109         if (serverInfo != null && !options.containsKey(SERVERINFO_LM_OPTION)) {
110             options.put(SERVERINFO_LM_OPTION, serverInfo);
111         }
112         if (classLoader != null && !options.containsKey(CLASSLOADER_LM_OPTION)) {
113             options.put(CLASSLOADER_LM_OPTION, classLoader);
114         }
115         if (loginModule.getLoginDomainName() != null) {
116             if (domainNames.contains(loginModule.getLoginDomainName())) {
117                 throw new IllegalStateException JavaDoc("Error in realm: one security realm cannot contain multiple login modules for the same login domain");
118             } else {
119                 domainNames.add(loginModule.getLoginDomainName());
120             }
121         }
122         JaasLoginModuleConfiguration config = new JaasLoginModuleConfiguration(loginModule.getLoginModuleClass(), controlFlag, options, loginModule.isServerSide(), loginModule.getLoginDomainName(), loginModule.isWrapPrincipals(), loginModule.getClassLoader());
123         loginModuleConfigurations.add(config);
124
125         if (next != null) {
126             next.configure(domainNames, loginModuleConfigurations, kernel, serverInfo, classLoader);
127         }
128     }
129
130     public static final GBeanInfo GBEAN_INFO;
131
132     static {
133         GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(JaasLoginModuleUse.class, "LoginModuleUse");
134         infoBuilder.addAttribute("controlFlag", String JavaDoc.class, true);
135         infoBuilder.addAttribute("kernel", Kernel.class, false, false);
136         infoBuilder.addReference("LoginModule", LoginModuleSettings.class, NameFactory.LOGIN_MODULE);
137         infoBuilder.addReference("Next", JaasLoginModuleUse.class);
138
139         infoBuilder.addOperation("configure", new Class JavaDoc[]{Set JavaDoc.class, List JavaDoc.class, Kernel.class, ServerInfo.class, ClassLoader JavaDoc.class});
140         infoBuilder.addInterface(JaasLoginModuleChain.class);
141         infoBuilder.setConstructor(new String JavaDoc[]{"LoginModule", "Next", "controlFlag", "kernel"});
142         GBEAN_INFO = infoBuilder.getBeanInfo();
143     }
144
145     public static GBeanInfo getGBeanInfo() {
146         return GBEAN_INFO;
147     }
148 }
149
Popular Tags