001: /*
002:
003: This software is OSI Certified Open Source Software.
004: OSI Certified is a certification mark of the Open Source Initiative.
005:
006: The license (Mozilla version 1.0) can be read at the MMBase site.
007: See http://www.MMBase.org/license
008:
009: */
010: package org.mmbase.security;
011:
012: import org.mmbase.util.HashCodeUtil;
013: import org.mmbase.util.logging.Logger;
014: import org.mmbase.util.logging.Logging;
015: import java.util.*;
016:
017: /**
018: * This class defines Rank objects which are used in security implementation. Ranks can be
019: * associated with users. Every Rank has an unique integer 'height' (so every rank is higher or
020: * lower than any other rank) and a String which can be used to identify it.
021: *
022: * Possible Ranks are maintained by static methods in this class. Generally the 'anonymous', 'basic
023: * user' and 'adminstrator' ranks should always be available, and only be delete with good reason.
024: *
025: *
026: * @author Eduard Witteveen
027: * @author Pierre van Rooden
028: * @author Michiel Meeuwissen
029: * @version $Id: Rank.java,v 1.18 2008/01/21 15:25:28 michiel Exp $
030: */
031: public final class Rank implements Comparable<Rank>,
032: java.io.Serializable {
033:
034: private static Logger log = Logging.getLoggerInstance(Rank.class);
035:
036: private static final int serialVersionUID = 1; // increase this if object chages.
037:
038: /** int value for the anonymous Rank*/
039: public final static int ANONYMOUS_INT = 0;
040:
041: /** int value for the basic user Rank*/
042: public final static int BASICUSER_INT = 100;
043:
044: /** int value for the anonymous Rank*/
045: public final static int ADMIN_INT = 73059;
046:
047: /** Identifier for anonymous rank*/
048: public final static Rank ANONYMOUS = new Rank(ANONYMOUS_INT,
049: "anonymous");
050:
051: /** Identifier for basic user rank*/
052: public final static Rank BASICUSER = new Rank(BASICUSER_INT,
053: "basic user");
054:
055: /** Identifier for admin rank*/
056: public final static Rank ADMIN = new Rank(ADMIN_INT,
057: "administrator");
058:
059: private static Map<String, Rank> ranks = new HashMap();
060:
061: static {
062: registerRank(ANONYMOUS);
063: registerRank(BASICUSER);
064: registerRank(ADMIN);
065: }
066:
067: /**
068: * constructor
069: */
070: protected Rank(int rank, String description) {
071: this .rank = rank;
072: this .description = description;
073: }
074:
075: /**
076: * This method gives back the internal int value of the rank
077: * which can be used in switch statements
078: * @return the internal int value
079: */
080: public int getInt() {
081: return rank;
082: }
083:
084: /**
085: * @return a string containing the description of the rank
086: */
087: public String toString() {
088: return description;
089: }
090:
091: /** the int value of the instance */
092: private int rank;
093:
094: /** the description of this rank */
095: private String description;
096:
097: public static Rank getRank(String rankDesc) {
098: return ranks.get(rankDesc);
099: }
100:
101: /**
102: * @since MMBase-1.6.4
103: */
104: protected static Rank registerRank(Rank rank) {
105: Rank prev = ranks.put(rank.toString(), rank);
106: if (prev == null) {
107: log.service("Registered rank " + rank);
108: } else {
109: log.service("Replaced rank " + rank);
110: }
111: return prev;
112: }
113:
114: /**
115: * Creates and adds a new Rank for the security system.
116: *
117: * @since MMBase-1.6.4
118: */
119:
120: public static Rank createRank(int rank, String rankDesc) {
121: Rank rankObject = new Rank(rank, rankDesc);
122: registerRank(rankObject);
123: return rankObject;
124: }
125:
126: /**
127: * Removes a rank from the security system.
128: * @since MMBase-1.6.4
129: */
130:
131: public static Rank deleteRank(String rankDesc) {
132: return ranks.remove(rankDesc);
133: }
134:
135: /**
136: * Returns all ranks currently known by the security implemetation. Default and to start with there
137: * are three ranks available: 'anonymous', 'basic user' and 'administrator'. You probably
138: * should never remove them.
139: * @since MMBase-1.6.4
140: */
141: public static SortedSet<Rank> getRanks() {
142: return new TreeSet<Rank>(ranks.values());
143: }
144:
145: /**
146: * @since MMBase-1.6.4
147: */
148: // see javadoc of Object
149: public boolean equals(Object o) {
150: if (o instanceof Rank) {
151: Rank r = (Rank) o;
152: return r.rank == rank && r.description.equals(description);
153: } else {
154: return false;
155: }
156: }
157:
158: /**
159: * @see java.lang.Object#hashCode()
160: */
161: public int hashCode() {
162: int result = 0;
163: result = HashCodeUtil.hashCode(result, rank);
164: result = HashCodeUtil.hashCode(result, description);
165: return result;
166: }
167:
168: /**
169: * @since MMBase-1.6.4
170: */
171: // see javadoc of Comparable
172: public int compareTo(Rank r) {
173: return rank - r.rank;
174: }
175: }
|