001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.security.impl.jacc;
023:
024: import javax.security.auth.Subject;
025: import java.security.Principal;
026: import java.security.acl.Group;
027: import java.util.ArrayList;
028: import java.util.Collections;
029: import java.util.Enumeration;
030: import java.util.HashSet;
031: import java.util.Iterator;
032: import java.util.Set;
033:
034: /**
035: * This principal purpose is to cache some computation necessary to make the portal JACC integration work.
036: *
037: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
038: * @version $Revision: 8784 $
039: */
040: public class JACCPortalPrincipal implements Principal {
041:
042: /** The set of principals that we use to give to jacc. */
043: private Principal[] principals;
044:
045: /** The set of roles that the subject owns. */
046: private Set roles;
047:
048: public JACCPortalPrincipal(Subject subject) {
049: this .roles = Collections.EMPTY_SET;
050:
051: // Get the principals and roles from the subject
052: ArrayList principals = new ArrayList();
053: for (Iterator i = subject.getPrincipals().iterator(); i
054: .hasNext();) {
055: Principal principal = (Principal) i.next();
056: if (principal instanceof Group) {
057: Group group = (Group) principal;
058: for (Enumeration e = group.members(); e
059: .hasMoreElements();) {
060: Principal nestedPrincipal = (Principal) e
061: .nextElement();
062: principals.add(nestedPrincipal);
063: }
064: if ("Roles".equals(group.getName())) {
065: roles = new HashSet();
066: for (Enumeration e = group.members(); e
067: .hasMoreElements();) {
068: Principal role = (Principal) e.nextElement();
069: roles.add(role);
070: }
071: }
072: } else {
073: principals.add(principal);
074: }
075: }
076: this .principals = (Principal[]) principals
077: .toArray(new Principal[principals.size()]);
078: }
079:
080: /**
081: * Return the set of roles.
082: *
083: * @return the roles
084: */
085: public Set getRoles() {
086: return roles;
087: }
088:
089: /**
090: * The principals owned.
091: *
092: * @return the principals
093: */
094: public Principal[] getPrincipals() {
095: return principals;
096: }
097:
098: public String getName() {
099: return "PortalPrincipal";
100: }
101: }
|