source: java/net/deterlab/abac/Role.java @ bd183ba

abac0-leakabac0-meimei-idmei-rt0-nmei_rt0tvf-new-xml
Last change on this file since bd183ba was 4560b65, checked in by Ted Faber <faber@…>, 11 years ago

Bump version number

  • Property mode set to 100644
File size: 7.9 KB
RevLine 
[31b67d5]1package net.deterlab.abac;
2
[de63a31]3import java.util.*;
4
[31b67d5]5/**
6 * Represents a role, which is a vertex in a Credential graph.
[e36ea1d]7 * @author <a href="http://abac.deterlab.net">ISI ABAC team</a>
[4560b65]8 * @version 1.4
[31b67d5]9 */
[88e139a]10public class Role implements Comparable {
[e36ea1d]11    /** The role represnetation */
[d69593c]12    protected String m_string;
[e36ea1d]13    /** The role broken into parts between dots */
[d69593c]14    protected String[] m_parts;
[e36ea1d]15    /** The linking role from a linking Role */
[d69593c]16    protected String m_A_r1;
[e36ea1d]17    /** The linked role from a linked Role */
[d69593c]18    protected String m_r2;
[e36ea1d]19    /** A prefix of the role */
[d69593c]20    protected String m_prefix;
[e36ea1d]21    /** Prerequisite roles for an intersection role. */
[d69593c]22    protected Role[] m_prereqs;
[31b67d5]23
24    /**
[cac4c76]25     * Create a role from a string. A single role must be of the format "A",
[31b67d5]26     * "A.r1", or "A.r1.r2", where A is a principal and r1 and r2 are role
[cac4c76]27     * names. This constructor also supports intersection roles: a sequence of
[bcf7370]28     * two or more roles separated by "&amp;". The whitespace surrounding &amp;
29     * is arbitrary.
[cac4c76]30     *
31     * If the string does not have this format, the constructor throws a
32     * RuntimeException.
[e36ea1d]33     *
34     * @param s a String with the role name
35     * @throws RuntimeException if the string is badly formatted
[31b67d5]36     */
[e36ea1d]37    public Role(String s) {
[31b67d5]38        m_string = s;
39
[bcf7370]40        // intersection roles have at least two roles separated by "&"
41        String[] isect_roles = s.split("&");
[cac4c76]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];
[53f5c27]50
51            // trim() handles arbitrary whitespace
[cac4c76]52            for (int i = 0; i < isect_roles.length; ++i)
[53f5c27]53                m_prereqs[i] = new Role(isect_roles[i].trim());
[cac4c76]54
55            // this make is_principal etc. work properly
56            m_parts = new String[0];
57        }
58    }
59
[e36ea1d]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     */
[84f0e7a]67    public Role(String s, Context c) {
68        this(c.expandKeyID(s));
[de63a31]69    }
70
[e36ea1d]71    /**
72     * Copy an existing role.
73     * @param r the Role to copy
74     */
[d69593c]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
[cac4c76]88    /**
[e36ea1d]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.
[cac4c76]92     */
[e36ea1d]93    private void single_role() {
[cac4c76]94        m_parts = m_string.split("\\.");
[31b67d5]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.
[a5cfe93]116     * @return true iff the role is a principal.
[31b67d5]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).
[a5cfe93]122     * @return true iff the role is a role (i.e., A.r1).
[31b67d5]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).
[a5cfe93]128     * @return true iff the role is a linking role (i.e., A.r1.r2).
[31b67d5]129     */
130    public boolean is_linking() { return m_parts.length == 3; }
131
[cac4c76]132    /**
133     * Returns true iff the role represents an intersection role.
[a5cfe93]134     * @return true iff the role represents an intersection role.
[cac4c76]135     */
136    public boolean is_intersection() { return m_prereqs != null; }
137
[31b67d5]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.
[a5cfe93]142     * @return the first two elements of a linking role's name.
[e36ea1d]143     * @throws RuntimeException if the role is not linking
[31b67d5]144     */
[5129e3e]145    String A_r1() throws RuntimeException {
[31b67d5]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.
[e36ea1d]154     * @return the last element of a linking role's name.
155     * @throws RuntimeException if the node is not a linking role.
[31b67d5]156     */
[5129e3e]157    String r2() throws RuntimeException {
[31b67d5]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
[e36ea1d]165     * except the last element of the name.  Used by Query.
[a5cfe93]166     * @return the principal part of a role or principal.
[31b67d5]167     */
[e36ea1d]168    String principal_part() {
[31b67d5]169        return m_prefix;
170    }
171
[e36ea1d]172    /**
173     * Return the principal
174     * @return the principal
175     */
[d69593c]176    public String principal() { return m_parts.length > 0 ? m_parts[0] : null; }
[e36ea1d]177    /**
[3b38c41]178     * Return the role name after the last dot
[e36ea1d]179     * @return the role name
180     */
[3b38c41]181    public String role_name() { return m_parts[m_parts.length-1]; }
[e36ea1d]182    /**
183     * Return the linked role (first two parts of a linking role)
184     * @return the linked role
185     */
[d69593c]186    public String linked_role() { return A_r1(); }
[281158a]187
[cac4c76]188    /**
189     * Get the roles that form the prerequisites to this intersection. Throws
190     * a runtime exception if this is not an intersection role.
[e36ea1d]191     * @return a Role[] of prerequisites
192     * @throws RuntimeException if this is not an intersection role
[cac4c76]193     */
[3b38c41]194    public Role[] prereqs() throws RuntimeException {
[cac4c76]195        if (!is_intersection())
196            throw new RuntimeException("Not an intersection role.");
197
198        return m_prereqs;
199    }
200
[a5cfe93]201    /**
202     * Returns a string representation of the Role.
203     * @return a string representation of the Role.
204     */
[31b67d5]205    public String toString() {
206        return m_string;
207    }
208
[a5cfe93]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     */
[84f0e7a]216    public String simpleString(Context c) {
217        return c.expandNickname(m_string);
[de63a31]218    }
219
[a5cfe93]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     */
[31b67d5]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
[a5cfe93]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     */
[88e139a]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
[a5cfe93]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     */
[31b67d5]249    public int hashCode() {
250        return m_string.hashCode();
251    }
[de63a31]252
[31b67d5]253}
Note: See TracBrowser for help on using the repository browser.