/**
*
* Copyright 2003-2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.geronimo.security.deployment;
import org.apache.geronimo.security.deploy.AutoMapAssistant;
import org.apache.geronimo.security.deploy.DefaultPrincipal;
import org.apache.geronimo.security.deploy.Principal;
import org.apache.geronimo.security.deploy.Realm;
import org.apache.geronimo.security.deploy.Role;
import org.apache.geronimo.security.deploy.Security;
import org.apache.geronimo.xbeans.geronimo.security.GerAutoMapRolesType;
import org.apache.geronimo.xbeans.geronimo.security.GerClassOverrideType;
import org.apache.geronimo.xbeans.geronimo.security.GerDefaultPrincipalType;
import org.apache.geronimo.xbeans.geronimo.security.GerPrincipalType;
import org.apache.geronimo.xbeans.geronimo.security.GerRealmType;
import org.apache.geronimo.xbeans.geronimo.security.GerRoleMappingsType;
import org.apache.geronimo.xbeans.geronimo.security.GerRoleType;
import org.apache.geronimo.xbeans.geronimo.security.GerSecurityType;
/**
* @version $Rev: $ $Date: $
*/
public class SecurityBuilder {
public static Security buildSecurityConfig(GerSecurityType securityType) {
Security security = null;
if (securityType != null) {
security = new Security();
security.setDoAsCurrentCaller(securityType.getDoasCurrentCaller());
security.setUseContextHandler(securityType.getUseContextHandler());
security.setDefaultRole(securityType.getDefaultRole());
GerDefaultPrincipalType defaultPrincipalType = securityType.getDefaultPrincipal();
DefaultPrincipal defaultPrincipal = new DefaultPrincipal();
defaultPrincipal.setRealmName(defaultPrincipalType.getRealmName());
defaultPrincipal.setPrincipal(buildPrincipal(defaultPrincipalType.getPrincipal()));
security.setDefaultPrincipal(defaultPrincipal);
GerRoleMappingsType roleMappingsType = securityType.getRoleMappings();
if (roleMappingsType != null) {
for (int i = 0; i < roleMappingsType.sizeOfRoleArray(); i++) {
GerRoleType roleType = roleMappingsType.getRoleArray(i);
Role role = new Role();
role.setRoleName(roleType.getRoleName());
for (int j = 0; j < roleType.sizeOfRealmArray(); j++) {
GerRealmType realmType = roleType.getRealmArray(j);
Realm realm = new Realm();
realm.setRealmName(realmType.getRealmName());
for (int k = 0; k < realmType.sizeOfPrincipalArray(); k++) {
realm.getPrincipals().add(buildPrincipal(realmType.getPrincipalArray(k)));
}
role.getRealms().add(realm);
}
security.getRoleMappings().add(role);
}
}
GerAutoMapRolesType autoMapRolesType = securityType.getAutoMapRoles();
if (autoMapRolesType != null) {
AutoMapAssistant assistant = new AutoMapAssistant();
assistant.setSecurityRealm(autoMapRolesType.getSecurityRealm());
GerClassOverrideType[] classOverrideArray = autoMapRolesType.getClassOverrideArray();
for (int i = 0; i < classOverrideArray.length; i++) {
assistant.getClassOverrides().add(classOverrideArray[i].getClass1());
}
security.setAssistant(assistant);
}
}
return security;
}
private static Principal buildPrincipal(GerPrincipalType principalType) {
Principal principal = new Principal();
principal.setClassName(principalType.getClass1());
principal.setPrincipalName(principalType.getName());
principal.setDesignatedRunAs(principalType.isSetDesignatedRunAs());
return principal;
}
}