KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > security > Rank


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.security;
11
12 import org.mmbase.util.HashCodeUtil;
13 import org.mmbase.util.logging.Logger;
14 import org.mmbase.util.logging.Logging;
15 import java.util.*;
16
17 /**
18  * This class defines Rank objects which are used in security implementation. Ranks can be
19  * associated with users. Every Rank has an unique integer 'height' (so every rank is higher or
20  * lower than any other rank) and a String which can be used to identify it.
21  *
22  * Possible Ranks are maintained by static methods in this class. Generally the 'anonymous', 'basic
23  * user' and 'adminstrator' ranks should always be available, and only be delete with good reason.
24  *
25  *
26  * @author Eduard Witteveen
27  * @author Pierre van Rooden
28  * @author Michiel Meeuwissen
29  * @version $Id: Rank.java,v 1.16 2005/04/25 14:25:18 michiel Exp $
30  */

31 public final class Rank implements Comparable JavaDoc, java.io.Serializable JavaDoc {
32
33     private static Logger log = Logging.getLoggerInstance(Rank.class);
34
35     private static final int serialVersionUID = 1; // increase this if object chages.
36

37     /** int value for the anonymous Rank*/
38     public final static int ANONYMOUS_INT = 0;
39
40     /** int value for the basic user Rank*/
41     public final static int BASICUSER_INT = 100;
42
43     /** int value for the anonymous Rank*/
44     public final static int ADMIN_INT = 73059;
45
46     /** Identifier for anonymous rank*/
47     public final static Rank ANONYMOUS = new Rank(ANONYMOUS_INT, "anonymous");
48
49     /** Identifier for basic user rank*/
50     public final static Rank BASICUSER = new Rank(BASICUSER_INT, "basic user");
51
52     /** Identifier for admin rank*/
53     public final static Rank ADMIN = new Rank(ADMIN_INT, "administrator");
54
55     private static Map ranks = new HashMap();
56
57
58     static {
59         registerRank(ANONYMOUS);
60         registerRank(BASICUSER);
61         registerRank(ADMIN);
62     }
63
64     /**
65      * constructor
66      */

67     protected Rank(int rank, String JavaDoc description) {
68         this.rank = rank;
69         this.description = description;
70     }
71
72     /**
73      * This method gives back the internal int value of the rank
74      * which can be used in switch statements
75      * @return the internal int value
76      */

77     public int getInt(){
78         return rank;
79     }
80
81     /**
82      * @return a string containing the description of the rank
83      */

84     public String JavaDoc toString() {
85         return description;
86     }
87
88     /** the int value of the instance */
89     private int rank;
90
91     /** the description of this rank */
92     private String JavaDoc description;
93
94     public static Rank getRank(String JavaDoc rankDesc) {
95         return (Rank) ranks.get(rankDesc);
96     }
97
98     /**
99      * @since MMBase-1.6.4
100      */

101     protected static Rank registerRank(Rank rank) {
102         Rank prev = (Rank) ranks.put(rank.toString(), rank);
103         if (prev == null) {
104             log.service("Registered rank " + rank);
105         } else {
106             log.service("Replaced rank " + rank);
107         }
108         return prev;
109     }
110
111     /**
112      * Creates and adds a new Rank for the security system.
113      *
114      * @since MMBase-1.6.4
115      */

116
117     public static Rank createRank(int rank, String JavaDoc rankDesc) {
118         Rank rankObject = new Rank(rank, rankDesc);
119         registerRank(rankObject);
120         return rankObject;
121     }
122
123     /**
124      * Removes a rank from the security system.
125      * @since MMBase-1.6.4
126      */

127
128     public static Rank deleteRank(String JavaDoc rankDesc) {
129         return (Rank) ranks.remove(rankDesc);
130     }
131
132     /**
133      * Returns all ranks currently known by the security implemetation. Default and to start with there
134      * are three ranks available: 'anonymous', 'basic user' and 'administrator'. You probably
135      * should never remove them.
136      * @since MMBase-1.6.4
137      */

138     public static SortedSet getRanks() {
139         return new TreeSet(ranks.values());
140     }
141
142     /**
143      * @since MMBase-1.6.4
144      */

145     // see javadoc of Object
146
public boolean equals(Object JavaDoc o) {
147         if (o instanceof Rank) {
148             Rank r = (Rank) o;
149             return r.rank == rank && r.description.equals(description);
150         } else {
151             return false;
152         }
153     }
154
155     
156     /**
157      * @see java.lang.Object#hashCode()
158      */

159     public int hashCode() {
160         int result = 0;
161         result = HashCodeUtil.hashCode(result, rank);
162         result = HashCodeUtil.hashCode(result, description);
163         return result;
164     }
165     
166     /**
167      * @since MMBase-1.6.4
168      */

169     // see javadoc of Comparable
170
public int compareTo (Object JavaDoc o) {
171         Rank r = (Rank) o;
172         return rank - r.rank;
173     }
174 }
175
Popular Tags