001/* 
002    Licensed to the Apache Software Foundation (ASF) under one
003    or more contributor license agreements.  See the NOTICE file
004    distributed with this work for additional information
005    regarding copyright ownership.  The ASF licenses this file
006    to you under the Apache License, Version 2.0 (the
007    "License"); you may not use this file except in compliance
008    with the License.  You may obtain a copy of the License at
009
010       http://www.apache.org/licenses/LICENSE-2.0
011
012    Unless required by applicable law or agreed to in writing,
013    software distributed under the License is distributed on an
014    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015    KIND, either express or implied.  See the License for the
016    specific language governing permissions and limitations
017    under the License.  
018 */
019package org.apache.wiki.auth;
020
021import java.io.Serializable;
022import java.security.Principal;
023
024/**
025 * Immutable Principal that represents a Group. GroupPrincipals are injected
026 * into a Subject's principal list at the time of authentication (login), and
027 * serve as proxies for Group objects for the purposes of making Java 2 security
028 * policy decisions. We add GroupPrincipals instead of the actual Groups because
029 * calling classes should never be able to obtain a mutable object (Group
030 * memberships can be changed by callers). Administrators who wish to grant
031 * privileges to specific wiki groups via the security policy file should always specify
032 * principals of type GroupPrincipal.
033 * @see org.apache.wiki.auth.authorize.Group
034 * @since 2.3.79
035 */
036public final class GroupPrincipal implements Principal, Serializable
037{
038    private static final long serialVersionUID = 123123123123L;
039    private final String m_name;
040
041    /**
042     * Constructs a new GroupPrincipal object with a supplied name.
043     *
044     * @param group the wiki group; cannot be <code>null</code>
045     */
046    public GroupPrincipal(final String group )
047    {
048        if ( group == null )
049        {
050            throw new IllegalArgumentException( "Group parameter cannot be null." );
051        }
052        m_name = group;
053    }
054    
055    /**
056     * Returns the name of the group principal.
057     * @return the name
058     * @see java.security.Principal#getName()
059     */
060    public String getName()
061    {
062        return m_name;
063    }
064
065    /**
066     * Two GroupPrincipals are equal if their names are equal.
067     * @param obj the object to compare
068     * @return the result of the equality test
069     * @see java.lang.Object#equals(java.lang.Object)
070     */
071    public boolean equals(final Object obj )
072    {
073        if ( !( obj instanceof GroupPrincipal ) )
074        {
075            return false;
076        }
077        final GroupPrincipal p = (GroupPrincipal)obj;
078        return p.m_name.equals( m_name );
079    }
080
081    /**
082     * Returns the hashcode for this object.
083     * @return the hash code
084     * @see java.lang.Object#hashCode()
085     */
086    public int hashCode()
087    {
088        return m_name.hashCode();
089    }
090
091    /**
092     * Returns a string representation of this object.
093     * @return the string
094     * @see java.lang.Object#toString()
095     */
096    public String toString()
097    {
098        return "[GroupPrincipal " + m_name + "]";
099    }
100
101}