All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ow2.util.security.struct.JGroup Maven / Gradle / Ivy

The newest version!
/**
 * Copyright 2006-2012 Bull S.A.S.
 *
 * 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.ow2.util.security.struct;

import java.io.Serializable;
import java.security.Principal;
import java.security.acl.Group;
import java.util.Enumeration;
import java.util.Vector;

/**
 * This class represents all the roles of a principal (group of principals).
 * These roles are added to the Subject
 * @author Florent Benoit
 */
public class JGroup implements Group, Serializable {

    /**
     * UID for serialization.
     */
    private static final long serialVersionUID = 7035827226889396034L;

    /**
     * Name of this group.
     */
    private String groupName = null;

    /**
     * Members of this group.
     */
    private Vector members = null;

    /**
     * Build a new group with the following name.
     * @param groupName name of the group
     */
    public JGroup(final String groupName) {
        this.groupName = groupName;
        this.members = new Vector();
    }

    /**
     * Compares this principal to the specified object. Returns true if the
     * object passed in matches the principal represented by the implementation
     * of this interface.
     * @param another principal to compare with.
     * @return true if the principal passed in is the same as that encapsulated
     *         by this principal, and false otherwise.
     */
    @Override
    public boolean equals(final Object another) {
        if (!(another instanceof Group)) {
            return false;
        }
        // else
        return groupName.equals(((Group) another).getName());
    }

    /**
     * Returns a string representation of this principal.
     * @return a string representation of this principal.
     */
    @Override
    public String toString() {
        return "Principal[" + groupName + "]";
    }

    /**
     * Returns a hashcode for this principal.
     * @return a hashcode for this principal.
     */
    @Override
    public int hashCode() {
        return groupName.hashCode();
    }

    /**
     * Returns the name of this principal.
     * @return the name of this principal.
     */
    public String getName() {
        return groupName;
    }

    /**
     * Adds the specified member to the group.
     * @param user the principal to add to this group.
     * @return true if the member was successfully added, false if the principal
     *         was already a member.
     */
    public boolean addMember(final Principal user) {
        if (isMember(user)) {
            return false;
        }
        // else
        members.add(user);
        return true;
    }

    /**
     * Removes the specified member from the group.
     * @param user the principal to remove from this group.
     * @return true if the principal was removed, or false if the principal was
     *         not a member.
     */
    public boolean removeMember(final Principal user) {
        if (!isMember(user)) {
            return false;
        }
        // else
        members.remove(user);
        return true;
    }

    /**
     * Returns true if the passed principal is a member of the 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.
     * @param member the principal whose membership is to be checked.
     * @return true if the principal is a member of this group, false otherwise.
     */
    public boolean isMember(final Principal member) {
        return members.contains(member);
    }

    /**
     * Returns an enumeration of the members in the group. The returned objects
     * can be instances of either Principal or Group (which is a subclass of
     * Principal).
     * @return an enumeration of the group members.
     */
    public Enumeration members() {
        return members.elements();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy