001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.metadata;
023:
024: import java.util.HashMap;
025: import java.util.HashSet;
026: import java.util.Iterator;
027: import java.util.Map;
028: import java.util.Set;
029:
030: import org.jboss.security.SecurityRoleMetaData;
031:
032: /**
033: * The meta data object for the assembly-descriptor element.
034: * This implementation only contains the security-role meta data
035: *
036: * @author Thomas.Diesler@jboss.org
037: * @author Anil.Saldhana@jboss.org
038: * @version $Revision: 60857 $
039: */
040: public class AssemblyDescriptorMetaData extends MetaData {
041: /** The assembly-descriptor/security-roles */
042: private HashMap securityRoles = new HashMap();
043:
044: /** The message destinations */
045: private HashMap messageDestinations = new HashMap();
046:
047: public void addSecurityRoleMetaData(SecurityRoleMetaData srMetaData) {
048: securityRoles.put(srMetaData.getRoleName(), srMetaData);
049: }
050:
051: public Map getSecurityRoles() {
052: return new HashMap(securityRoles);
053: }
054:
055: /**
056: * Merge the security role/principal mapping defined in jboss.xml
057: * with the one defined at jboss-app.xml.
058: */
059: public void mergeSecurityRoles(Map applRoles) {
060: Iterator it = applRoles.entrySet().iterator();
061: while (it.hasNext()) {
062: Map.Entry entry = (Map.Entry) it.next();
063: String roleName = (String) entry.getKey();
064: SecurityRoleMetaData appRole = (SecurityRoleMetaData) entry
065: .getValue();
066: SecurityRoleMetaData srMetaData = (SecurityRoleMetaData) securityRoles
067: .get(roleName);
068: if (srMetaData != null) {
069: Set principalNames = appRole.getPrincipals();
070: srMetaData.addPrincipalNames(principalNames);
071: } else {
072: securityRoles.put(roleName, entry.getValue());
073: }
074: }
075: }
076:
077: public SecurityRoleMetaData getSecurityRoleByName(String roleName) {
078: return (SecurityRoleMetaData) securityRoles.get(roleName);
079: }
080:
081: public Set getSecurityRoleNamesByPrincipal(String userName) {
082: HashSet roleNames = new HashSet();
083: Iterator it = securityRoles.values().iterator();
084: while (it.hasNext()) {
085: SecurityRoleMetaData srMetaData = (SecurityRoleMetaData) it
086: .next();
087: if (srMetaData.getPrincipals().contains(userName))
088: roleNames.add(srMetaData.getRoleName());
089: }
090: return roleNames;
091: }
092:
093: /**
094: * Generate a Map of Principal keyed against a set of role names
095: * @return Map<Princpal,Set>
096: */
097: public Map<String, Set<String>> getPrincipalVersusRolesMap() {
098: Map<String, Set<String>> principalRolesMap = null;
099:
100: Iterator iter = securityRoles.keySet().iterator();
101: while (iter.hasNext()) {
102: if (principalRolesMap == null)
103: principalRolesMap = new HashMap<String, Set<String>>();
104: String rolename = (String) iter.next();
105: SecurityRoleMetaData srm = (SecurityRoleMetaData) securityRoles
106: .get(rolename);
107: Iterator principalIter = srm.getPrincipals().iterator();
108: while (principalIter.hasNext()) {
109: String pr = (String) principalIter.next();
110: Set<String> roleset = (Set<String>) principalRolesMap
111: .get(pr);
112: if (roleset == null)
113: roleset = new HashSet<String>();
114: if (!roleset.contains(rolename))
115: roleset.add(rolename);
116: principalRolesMap.put(pr, roleset);
117: }
118: }
119: return principalRolesMap;
120: }
121:
122: public void addMessageDestinationMetaData(
123: MessageDestinationMetaData metaData) {
124: messageDestinations.put(metaData.getName(), metaData);
125: }
126:
127: public MessageDestinationMetaData getMessageDestinationMetaData(
128: String name) {
129: return (MessageDestinationMetaData) messageDestinations
130: .get(name);
131: }
132: }
|