001: /**
002: * $Id: CMCPrincipal.java,v 1.5 2005/07/27 19:56:41 jtb Exp $
003: * Copyright 2005 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.community.mc;
014:
015: import java.util.StringTokenizer;
016:
017: import java.security.Principal;
018:
019: /**
020: * Unqiuely identifies a community.
021: */
022: public class CMCPrincipal implements Principal, Comparable {
023: // two underscores
024: private static final String SEPARATOR = "__";
025:
026: private String type = null;
027: private String name = null;
028:
029: /**
030: * Construct a new community principal based on the given type and name.
031: */
032: public CMCPrincipal(String type, String name) {
033: this .type = type;
034: this .name = name;
035: }
036:
037: /**
038: * Construct a new community principal based on the given string
039: * representation of the community.
040: * @param communityPrincipalString A <CODE>String</CODE> representation of a community principal.
041: * This string must be obtained from the <CODE>toString()</CODE> method of this class.
042: * @see #toString()
043: */
044: public CMCPrincipal(String communityPrincipalString) {
045: if (communityPrincipalString == null) {
046: throw new NullPointerException(
047: "null principal string not allowed");
048: }
049: String[] tokens = communityPrincipalString.split(SEPARATOR);
050: if (tokens.length != 2) {
051: throw new IllegalArgumentException(
052: "invalid principal string format: "
053: + communityPrincipalString);
054: }
055:
056: this .type = tokens[0];
057: this .name = tokens[1];
058: }
059:
060: public boolean equals(Object o) {
061: if (!(o instanceof CMCPrincipal)) {
062: return false;
063: }
064:
065: CMCPrincipal other = (CMCPrincipal) o;
066:
067: if (getName().equals(other.getName())
068: && getType().equals(other.getType())) {
069: return true;
070: }
071:
072: return false;
073: }
074:
075: public String getName() {
076: return name;
077: }
078:
079: public String getType() {
080: return type;
081: }
082:
083: public int hashCode() {
084: return toString().hashCode();
085: }
086:
087: /**
088: * Return the string representation of this community principal.
089: * <br><br>
090: * The string return value of this operation can be used to re-construct the
091: * equivalent <CODE>CommunityPrincipal</CODE>
092: * object via the single-string argument
093: * constructor.
094: */
095: public String toString() {
096: return type + SEPARATOR + name;
097: }
098:
099: public int compareTo(Object o) {
100: if (!(o instanceof CMCPrincipal)) {
101: throw new ClassCastException("object was not of this type");
102: }
103: CMCPrincipal other = (CMCPrincipal) o;
104: return toString().compareTo(other.toString());
105: }
106: }
|