source: java/net/deterlab/abac/Role.java @ 238717d

abac0-leakabac0-meicompt_changesgec13mei-idmei-rt0-nmei_rt0mei_rt2mei_rt2_fix_1meiyap-rt1meiyap1rt2tvf-new-xml
Last change on this file since 238717d was 238717d, checked in by Ted Faber <faber@…>, 13 years ago

Auto-load the BouncyCastle? provider. (Makes jnlp work)

  • Property mode set to 100644
File size: 7.9 KB
Line 
1package net.deterlab.abac;
2
3import java.util.*;
4
5/**
6 * Represents a role, which is a vertex in a Credential graph.
7 * @author <a href="http://abac.deterlab.net">ISI ABAC team</a>
8 * @version 1.3
9 */
10public class Role implements Comparable {
11    /** The role represnetation */
12    protected String m_string;
13    /** The role broken into parts between dots */
14    protected String[] m_parts;
15    /** The linking role from a linking Role */
16    protected String m_A_r1;
17    /** The linked role from a linked Role */
18    protected String m_r2;
19    /** A prefix of the role */
20    protected String m_prefix;
21    /** Prerequisite roles for an intersection role. */
22    protected Role[] m_prereqs;
23
24    /**
25     * Create a role from a string. A single role must be of the format "A",
26     * "A.r1", or "A.r1.r2", where A is a principal and r1 and r2 are role
27     * names. This constructor also supports intersection roles: a sequence of
28     * two or more roles separated by "&amp;". The whitespace surrounding &amp;
29     * is arbitrary.
30     *
31     * If the string does not have this format, the constructor throws a
32     * RuntimeException.
33     *
34     * @param s a String with the role name
35     * @throws RuntimeException if the string is badly formatted
36     */
37    public Role(String s) {
38        m_string = s;
39
40        // intersection roles have at least two roles separated by "&"
41        String[] isect_roles = s.split("&");
42
43        // ordinary role
44        if (isect_roles.length == 1)
45            single_role();
46
47        // intersection role: make a list of prereqs
48        else {
49            m_prereqs = new Role[isect_roles.length];
50
51            // trim() handles arbitrary whitespace
52            for (int i = 0; i < isect_roles.length; ++i)
53                m_prereqs[i] = new Role(isect_roles[i].trim());
54
55            // this make is_principal etc. work properly
56            m_parts = new String[0];
57        }
58    }
59
60    /**
61     * Create a role from the given string, converted from mnemonic strings to
62     * key IDs that are known from the Context.  This is a jabac extension.
63     * @param s the String containing the rile name.
64     * @param c the Context in which to expand mnemonics
65     * @throws RuntimeException if the string is badly formatted.
66     */
67    public Role(String s, Context c) {
68        this(c.expandKeyID(s));
69    }
70
71    /**
72     * Copy an existing role.
73     * @param r the Role to copy
74     */
75    public Role(Role r) {
76        m_string = r.m_string;
77        m_A_r1 = r.m_A_r1;
78        m_r2 = r.m_r2;
79        m_prefix = r.m_prefix;
80        m_parts = new String[r.m_parts.length];
81        for (int i = 0; i < r.m_parts.length; i++) 
82            m_parts[i] = r.m_parts[i];
83        m_prereqs = new Role[m_prereqs.length];
84        for (int i = 0; i < r.m_prereqs.length; i++) 
85            m_prereqs[i] = new Role(r.m_prereqs[i]);
86    }
87
88    /**
89     * Initialize a single non-intersection role. See constructor for details
90     * of role format. Will throw RuntimeException if the role is invalid.
91     * @throws RuntimeException if a role is invalid.
92     */
93    private void single_role() {
94        m_parts = m_string.split("\\.");
95        if (m_parts.length > 3)
96            throw new RuntimeException("Not a valid role: " + m_string);
97
98        // linking role: prefix is A.r1 from A.r1.r2
99        if (is_linking()) {
100            m_A_r1 = m_parts[0] + "." + m_parts[1];
101            m_r2 = m_parts[2];
102            m_prefix = m_A_r1;
103        }
104
105        // role: prefix is A from A.r1
106        else if (is_role())
107            m_prefix = m_parts[0];
108
109        // principal: prefix is the whole thing
110        else
111            m_prefix = m_string;
112    }
113
114    /**
115     * Returns true iff the role is a principal.
116     * @return true iff the role is a principal.
117     */
118    public boolean is_principal() { return m_parts.length == 1; }
119
120    /**
121     * Returns true iff the role is a role (i.e., A.r1).
122     * @return true iff the role is a role (i.e., A.r1).
123     */
124    public boolean is_role() { return m_parts.length == 2; }
125
126    /**
127     * Returns true iff the role is a linking role (i.e., A.r1.r2).
128     * @return true iff the role is a linking role (i.e., A.r1.r2).
129     */
130    public boolean is_linking() { return m_parts.length == 3; }
131
132    /**
133     * Returns true iff the role represents an intersection role.
134     * @return true iff the role represents an intersection role.
135     */
136    public boolean is_intersection() { return m_prereqs != null; }
137
138    /**
139     * Returns the first two elements of a linking role's name. This typically
140     * refers to another role in the graph. This will throw a runtime
141     * exception if the node is not a linking role.
142     * @return the first two elements of a linking role's name.
143     * @throws RuntimeException if the role is not linking
144     */
145    String A_r1() throws RuntimeException {
146        if (!is_linking())
147            throw new RuntimeException("Not a linking role");
148        return m_A_r1;
149    }
150
151    /**
152     * Return the last element of a linking role's name. This will throw a
153     * runtime exception if the node is not a linking role.
154     * @return the last element of a linking role's name.
155     * @throws RuntimeException if the node is not a linking role.
156     */
157    String r2() throws RuntimeException {
158        if (!is_linking())
159            throw new RuntimeException("Not a linking role");
160        return m_r2;
161    }
162
163    /**
164     * Returns the principal part of a role or principal. This is everything
165     * except the last element of the name.  Used by Query.
166     * @return the principal part of a role or principal.
167     */
168    String principal_part() {
169        return m_prefix;
170    }
171
172    /**
173     * Return the principal
174     * @return the principal
175     */
176    public String principal() { return m_parts.length > 0 ? m_parts[0] : null; }
177    /**
178     * Return the role name after the last dot
179     * @return the role name
180     */
181    public String role_name() { return m_parts[m_parts.length-1]; }
182    /**
183     * Return the linked role (first two parts of a linking role)
184     * @return the linked role
185     */
186    public String linked_role() { return A_r1(); }
187
188    /**
189     * Get the roles that form the prerequisites to this intersection. Throws
190     * a runtime exception if this is not an intersection role.
191     * @return a Role[] of prerequisites
192     * @throws RuntimeException if this is not an intersection role
193     */
194    public Role[] prereqs() throws RuntimeException {
195        if (!is_intersection())
196            throw new RuntimeException("Not an intersection role.");
197
198        return m_prereqs;
199    }
200
201    /**
202     * Returns a string representation of the Role.
203     * @return a string representation of the Role.
204     */
205    public String toString() {
206        return m_string;
207    }
208
209    /**
210     * Returns a string representation of the Role with mnemonic names from the
211     * given Context.  A jabac extension.
212     * @param c A Context used to look up mnemonic names.
213     * @return a string representation of the Role with mnemonic names from the
214     * given Context.
215     */
216    public String simpleString(Context c) {
217        return c.expandNickname(m_string);
218    }
219
220    /**
221     * Equality test.  Two Roles are the same if their string representations
222     * are equal.
223     * @param v2 an Object to compare
224     * @return a boolean, true if the two Roles are equal.
225     */
226    public boolean equals(Object v2) {
227        if (v2 instanceof Role)
228            return m_string.equals(((Role)v2).m_string);
229        return false;
230    }
231
232    /**
233     * Partial order test.  Return a lexical comparison of the two Roles
234     * @param o an Object to compare against
235     * @return -1 if this Role is before, 0 if they are the same, and 1
236     *              if this Role is after the given object.
237     */
238    public int compareTo(Object o) {
239        if (o instanceof Role) 
240            return m_string.compareTo(((Role)o).m_string);
241        else return 1;
242    }
243
244    /**
245     * Returns a hash code value for the object.  It is the hash of the string
246     * representation.
247     * @return a hash code value for the object.
248     */
249    public int hashCode() {
250        return m_string.hashCode();
251    }
252
253}
Note: See TracBrowser for help on using the repository browser.