KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > security > deployment > LoginConfigBuilder


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.deployment;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.HashSet JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Properties JavaDoc;
25 import java.util.Set JavaDoc;
26
27 import org.apache.geronimo.common.DeploymentException;
28 import org.apache.geronimo.deployment.DeploymentContext;
29 import org.apache.geronimo.deployment.service.SingleGBeanBuilder;
30 import org.apache.geronimo.deployment.service.XmlReferenceBuilder;
31 import org.apache.geronimo.deployment.xbeans.PatternType;
32 import org.apache.geronimo.gbean.AbstractName;
33 import org.apache.geronimo.gbean.AbstractNameQuery;
34 import org.apache.geronimo.gbean.GBeanData;
35 import org.apache.geronimo.gbean.GBeanInfo;
36 import org.apache.geronimo.gbean.GBeanInfoBuilder;
37 import org.apache.geronimo.gbean.GReferenceInfo;
38 import org.apache.geronimo.gbean.ReferencePatterns;
39 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
40 import org.apache.geronimo.kernel.GBeanAlreadyExistsException;
41 import org.apache.geronimo.kernel.Naming;
42 import org.apache.geronimo.kernel.Kernel;
43 import org.apache.geronimo.security.jaas.JaasLoginModuleUse;
44 import org.apache.geronimo.security.jaas.LoginModuleGBean;
45 import org.apache.geronimo.xbeans.geronimo.loginconfig.GerAbstractLoginModuleType;
46 import org.apache.geronimo.xbeans.geronimo.loginconfig.GerLoginConfigType;
47 import org.apache.geronimo.xbeans.geronimo.loginconfig.GerLoginModuleRefType;
48 import org.apache.geronimo.xbeans.geronimo.loginconfig.GerLoginModuleType;
49 import org.apache.geronimo.xbeans.geronimo.loginconfig.GerOptionType;
50 import org.apache.geronimo.xbeans.geronimo.loginconfig.GerLoginConfigDocument;
51 import org.apache.xmlbeans.XmlCursor;
52 import org.apache.xmlbeans.XmlObject;
53 import org.apache.xmlbeans.XmlOptions;
54
55
56 /**
57  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
58  */

59 public class LoginConfigBuilder implements XmlReferenceBuilder {
60     public static final String JavaDoc LOGIN_CONFIG_NAMESPACE = GerLoginConfigDocument.type.getDocumentElementName().getNamespaceURI();
61
62     private final Naming naming;
63
64     public LoginConfigBuilder(Kernel kernel) {
65         this.naming = kernel.getNaming();
66     }
67
68     public LoginConfigBuilder(Naming naming) {
69         this.naming = naming;
70     }
71
72     public String JavaDoc getNamespace() {
73         return LOGIN_CONFIG_NAMESPACE;
74     }
75
76     public ReferencePatterns getReferences(XmlObject xmlObject, DeploymentContext context, AbstractName parentName, ClassLoader JavaDoc classLoader) throws DeploymentException {
77         GerLoginConfigType loginConfig = (GerLoginConfigType) xmlObject.copy().changeType(GerLoginConfigType.type);
78         XmlOptions xmlOptions = new XmlOptions();
79         xmlOptions.setLoadLineNumbers();
80         Collection JavaDoc errors = new ArrayList JavaDoc();
81         xmlOptions.setErrorListener(errors);
82         if (!loginConfig.validate(xmlOptions)) {
83             throw new DeploymentException("Invalid login configuration:\n" + errors + "\nDescriptor: " + loginConfig.toString());
84         }
85         XmlCursor xmlCursor = loginConfig.newCursor();
86         List JavaDoc uses = new ArrayList JavaDoc();
87         Set JavaDoc loginModuleNames = new HashSet JavaDoc();
88         try {
89             boolean atStart = true;
90             while ((atStart && xmlCursor.toFirstChild()) || (!atStart && xmlCursor.toNextSibling())) {
91                 atStart = false;
92                 XmlObject child = xmlCursor.getObject();
93                 GerAbstractLoginModuleType abstractLoginModule = (GerAbstractLoginModuleType) child;
94                 String JavaDoc controlFlag = abstractLoginModule.getControlFlag().toString();
95                 boolean wrapPrincipals = (abstractLoginModule.isSetWrapPrincipals() && abstractLoginModule.getWrapPrincipals());
96                 ReferencePatterns loginModuleReferencePatterns;
97                 String JavaDoc name;
98                 if (abstractLoginModule instanceof GerLoginModuleRefType) {
99                     GerLoginModuleRefType loginModuleRef = (GerLoginModuleRefType) abstractLoginModule;
100                     PatternType patternType = loginModuleRef.getPattern();
101                     AbstractNameQuery loginModuleNameQuery = SingleGBeanBuilder.buildAbstractNameQuery(patternType, USE_REFERENCE_INFO);
102                     loginModuleReferencePatterns = new ReferencePatterns(loginModuleNameQuery);
103                     name = (String JavaDoc) loginModuleNameQuery.getName().get("name");
104                     if (name == null) {
105                         throw new DeploymentException("You must specify the name of the login module in the login module ref " + patternType);
106                     }
107 //TODO configid reinstate this check for duplicate domain names
108
// try
109
// {
110
// String loginDomain = (String) context.getAttribute(loginModuleName, "loginDomainName");
111
// if (!loginModuleNames.add(loginDomain))
112
// {
113
// throw new DeploymentException("Security realm contains two login domains called '" + loginDomain + "'");
114
// }
115
// }
116
// catch (DeploymentException e)
117
// {
118
// throw e;
119
// }
120
// catch (Exception e)
121
// {
122
// throw new DeploymentException("Unable to create reference to login module " + name, e);
123
// }
124
} else if (abstractLoginModule instanceof GerLoginModuleType) {
125                     //create the LoginModuleGBean also
126
AbstractName loginModuleName;
127
128                     GerLoginModuleType loginModule = (GerLoginModuleType) abstractLoginModule;
129                     name = trim(loginModule.getLoginDomainName());
130                     if (!loginModuleNames.add(name)) {
131                         throw new DeploymentException("Security realm contains two login domains called '" + name + "'");
132                     }
133                     String JavaDoc className = trim(loginModule.getLoginModuleClass());
134                     boolean serverSide = loginModule.getServerSide();
135                     Properties JavaDoc options = new Properties JavaDoc();
136                     GerOptionType[] optionArray = loginModule.getOptionArray();
137                     for (int j = 0; j < optionArray.length; j++) {
138                         GerOptionType gerOptionType = optionArray[j];
139                         String JavaDoc key = gerOptionType.getName();
140                         String JavaDoc value = trim(gerOptionType.getStringValue());
141                         options.setProperty(key, value);
142                     }
143                     loginModuleName = naming.createChildName(parentName, name, NameFactory.LOGIN_MODULE);
144                     loginModuleReferencePatterns = new ReferencePatterns(loginModuleName);
145                     GBeanData loginModuleGBeanData = new GBeanData(loginModuleName, LoginModuleGBean.GBEAN_INFO);
146                     loginModuleGBeanData.setAttribute("loginDomainName", name);
147                     loginModuleGBeanData.setAttribute("loginModuleClass", className);
148                     loginModuleGBeanData.setAttribute("options", options);
149                     loginModuleGBeanData.setAttribute("serverSide", Boolean.valueOf(serverSide));
150                     loginModuleGBeanData.setAttribute("wrapPrincipals", Boolean.valueOf(wrapPrincipals));
151
152                     context.addGBean(loginModuleGBeanData);
153                 } else {
154                     throw new DeploymentException("Unknown abstract login module type: " + abstractLoginModule.getClass());
155                 }
156                 AbstractName thisName;
157                 thisName = naming.createChildName(parentName, name, "LoginModuleUse");
158                 GBeanData loginModuleUseGBeanData = new GBeanData(thisName, JaasLoginModuleUse.GBEAN_INFO);
159                 loginModuleUseGBeanData.setAttribute("controlFlag", controlFlag);
160                 loginModuleUseGBeanData.setReferencePatterns("LoginModule", loginModuleReferencePatterns);
161                 uses.add(loginModuleUseGBeanData);
162             }
163             for (int i = uses.size() - 1; i >= 0; i--) {
164                 GBeanData data = (GBeanData) uses.get(i);
165                 if (i > 0) {
166                     ((GBeanData) uses.get(i - 1)).setReferencePattern("Next", data.getAbstractName());
167                 }
168                 context.addGBean(data);
169             }
170         }
171         catch (GBeanAlreadyExistsException e) {
172             throw new DeploymentException(e);
173         } finally {
174             xmlCursor.dispose();
175         }
176         return uses.size() == 0 ? null : new ReferencePatterns(((GBeanData) uses.get(0)).getAbstractName());
177     }
178
179     private String JavaDoc trim(String JavaDoc string) {
180         return string == null ? null : string.trim();
181     }
182
183     public static final GBeanInfo GBEAN_INFO;
184
185     private static final GReferenceInfo USE_REFERENCE_INFO;
186
187     static {
188         GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(LoginConfigBuilder.class, "XmlReferenceBuilder");
189         infoBuilder.addAttribute("kernel", Kernel.class, false, false);
190         infoBuilder.setConstructor(new String JavaDoc[] {"kernel"});
191         infoBuilder.addInterface(XmlReferenceBuilder.class);
192         GBEAN_INFO = infoBuilder.getBeanInfo();
193
194         Set JavaDoc referenceInfos = JaasLoginModuleUse.GBEAN_INFO.getReferences();
195         GReferenceInfo found = null;
196         for (Iterator JavaDoc iterator = referenceInfos.iterator(); iterator.hasNext();) {
197             GReferenceInfo testReferenceInfo = (GReferenceInfo) iterator.next();
198             String JavaDoc testRefName = testReferenceInfo.getName();
199             if (testRefName.equals("LoginModule")) {
200                 found = testReferenceInfo;
201                 break;
202             }
203         }
204         if (found == null) {
205             throw new RuntimeException JavaDoc("Someone changed the gbeaninfo on JaasLoginModuleUse");
206         }
207         USE_REFERENCE_INFO = found;
208
209     }
210
211     public static GBeanInfo getGBeanInfo() {
212         return GBEAN_INFO;
213     }
214 }
215
Popular Tags