Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  * JBoss, Home of Professional Open Source
  * Copyright 2005, JBoss Inc., and individual contributors as indicated
  * by the @authors tag. See the copyright.txt in the distribution for a
  * full listing of individual contributors.
  * This is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as
  * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site:
 //$Id: 2 2010-02-09 17:42:37Z  $
An implementation of Group that allows that acts as a stack of Groups with a single Group member active at any time. When one adds a Group to a NestableGroup the Group is pushed onto the active Group stack and any of the Group methods operate as though the NestableGroup contains only the Group. When removing the Group that corresponds to the active Group, the active Group is popped from the stack and the new active Group is set to the new top of the stack. The typical usage of this class is when doing a JAAS LoginContext login to runAs a new Principal with a new set of roles that should be added without destroying the current identity and roles.

$Revision: 2 $
 public class NestableGroup extends SimplePrincipal implements GroupCloneable 
The serialVersionUID
    private static final long serialVersionUID = 1752783303935807441L;
The stack of the Groups. Elements are pushed/poped by inserting/removing element 0.
     private LinkedList<PrincipalrolesStack;

Creates new NestableGroup with the given name
     public NestableGroup(String name)
          = new LinkedList<Principal>();
 // --- Begin Group interface methods
Returns an enumeration that contains the single active Principal.

an Enumeration of the single active Principal.
     public Enumeration<Principalmembers()
         return new IndexEnumeration<Principal>();

Removes the first occurence of user from the Principal stack.

user the principal to remove from this group.
true if the principal was removed, or false if the principal was not a member.
     public boolean removeMember(Principal user)
         return .remove(user);

Pushes the group onto the Group stack and makes it the active Group.

group the instance of Group that contains the roles to set as the active Group.
true always.
java.lang.IllegalArgumentException thrown if group is not an instance of Group.
     public boolean addMember(Principal groupthrows IllegalArgumentException
         if( (group instanceof Group) == false )
             throw new IllegalArgumentException("The addMember argument must be a Group");
         return true;

Returns true if the passed principal is a member of the active group. This method does a recursive search, so if a principal belongs to a group which is a member of this group, true is returned.

member the principal whose membership is to be checked.
true if the principal is a member of this group, false otherwise.
    public boolean isMember(Principal member)
        if.size() == 0 )
            return false;
        Group activeGroup = (Group.getFirst();
        boolean isMember = activeGroup.isMember(member);
        return isMember;
   public String toString()
      StringBuffer tmp = new StringBuffer(getName());
      Enumeration<Principaliter = members();
      whileiter.hasMoreElements() )
      tmp.setCharAt(tmp.length()-1, ')');
      return tmp.toString();
   public synchronized Object clone() throws CloneNotSupportedException   
      NestableGroup clone = (NestableGroupsuper.clone(); 
      if(clone != null
        clone.rolesStack = (LinkedList<Principal>)this..clone();  
      return clone
// --- End Group interface methods
    private class IndexEnumeration<T extends Principalimplements Enumeration<Principal>
        private Enumeration<? extends Principaliter;
            if.size() > 0 )
                Group grp = (Group.get(0);
                 = grp.members();
        public boolean hasMoreElements()
            boolean hasMore =  != null && .hasMoreElements();
            return hasMore;
        public Principal nextElement()
            Principal next = null;
            if != null )
                next = .nextElement();
            return next;
New to GrepCode? Check out our FAQ X