001: /*
002: JSPWiki - a JSP-based WikiWiki clone.
003:
004: Copyright (C) 2001-2004 Janne Jalkanen (Janne.Jalkanen@iki.fi)
005:
006: This program is free software; you can redistribute it and/or modify
007: it under the terms of the GNU Lesser General Public License as published by
008: the Free Software Foundation; either version 2.1 of the License, or
009: (at your option) any later version.
010:
011: This program is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: GNU Lesser General Public License for more details.
015:
016: You should have received a copy of the GNU Lesser General Public License
017: along with this program; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020: package com.ecyrd.jspwiki.auth;
021:
022: import java.security.Principal;
023: import java.util.Arrays;
024: import java.util.Comparator;
025:
026: /**
027: * A lightweight, immutable Principal class. WikiPrincipals can be created with
028: * and optional "type" to denote what type of user profile Principal it represents
029: * (FULL_NAME, WIKI_NAME, LOGIN_NAME). Types are used to determine suitable
030: * user and login Principals in classes like WikiSession. However, the type
031: * property of a WikiPrincipal does not affect a WikiPrincipal's logical equality
032: * or hash code; two WikiPrincipals with the same name but different types are still
033: * considered equal.
034: *
035: * @author Janne Jalkanen
036: * @author Andrew Jaquith
037: * @since 2.2
038: */
039: public final class WikiPrincipal implements Principal {
040:
041: /**
042: * Represents an anonymous user. WikiPrincipals may be
043: * created with an optional type designator:
044: * LOGIN_NAME, WIKI_NAME, FULL_NAME or UNSPECIFIED.
045: */
046: public static final Principal GUEST = new WikiPrincipal("Guest");
047:
048: /** WikiPrincipal type denoting a user's full name. */
049: public static final String FULL_NAME = "fullName";
050:
051: /** WikiPrincipal type denoting a user's login name. */
052: public static final String LOGIN_NAME = "loginName";
053:
054: /** WikiPrincipal type denoting a user's wiki name. */
055: public static final String WIKI_NAME = "wikiName";
056:
057: /** Generic WikiPrincipal of unspecified type. */
058: public static final String UNSPECIFIED = "unspecified";
059:
060: /** Static instance of Comparator that allows Principals to be sorted. */
061: public static final Comparator COMPARATOR = new PrincipalComparator();
062:
063: private static final String[] VALID_TYPES;
064:
065: static {
066: VALID_TYPES = new String[] { FULL_NAME, LOGIN_NAME, WIKI_NAME,
067: UNSPECIFIED };
068: Arrays.sort(VALID_TYPES);
069: }
070:
071: private final String m_name;
072: private final String m_type;
073:
074: /**
075: * Constructs a new WikiPrincipal with a given name and a type of
076: * {@link #UNSPECIFIED}.
077: * @param name the name of the Principal
078: */
079: public WikiPrincipal(String name) {
080: m_name = name;
081: m_type = UNSPECIFIED;
082: }
083:
084: /**
085: * Constructs a new WikiPrincipal with a given name and optional type
086: * designator. If the supplied <code>type</code> parameter is not
087: * {@link #LOGIN_NAME}, {@link #FULL_NAME}, {@link #WIKI_NAME}
088: * or {@link #WIKI_NAME}, this method throws
089: * an {@link IllegalArgumentException}.
090: * @param name the name of the Principal
091: * @param type the type for this principal, which may be {@link #LOGIN_NAME},
092: * {@link #FULL_NAME}, {@link #WIKI_NAME} or {@link #WIKI_NAME}.
093: */
094: public WikiPrincipal(String name, String type) {
095: m_name = name;
096: if (Arrays.binarySearch(VALID_TYPES, type) < 0) {
097: throw new IllegalArgumentException("Principal type '"
098: + type + "' is invalid.");
099: }
100: m_type = type;
101: }
102:
103: /**
104: * Returns the wiki name of the Principal.
105: * @return the name
106: */
107: public final String getName() {
108: return m_name;
109: }
110:
111: /**
112: * Two <code>WikiPrincipal</code>s are considered equal if their
113: * names are equal (case-sensitive).
114: * @param obj the object to compare
115: * @return the result of the equality test
116: */
117: public final boolean equals(Object obj) {
118: if (obj == null || !(obj instanceof WikiPrincipal)) {
119: return false;
120: }
121: return m_name.equals(((WikiPrincipal) obj).getName());
122: }
123:
124: /**
125: * The hashCode() returned for the WikiPrincipal is the same as
126: * for its name.
127: * @return the hash code
128: */
129: public final int hashCode() {
130: return m_name.hashCode();
131: }
132:
133: /**
134: * Returns the Principal "type": {@link #LOGIN_NAME}, {@link #FULL_NAME},
135: * {@link #WIKI_NAME} or {@link #WIKI_NAME}
136: * @return the type
137: */
138: public final String getType() {
139: return m_type;
140: }
141:
142: /**
143: * Returns a human-readable representation of the object.
144: * @return the string representation
145: */
146: public final String toString() {
147: return "[WikiPrincipal (" + m_type + "): " + getName() + "]";
148: }
149:
150: }
|