Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: jsdsi/AclEntry.java


1   /*
2    * Copyright 2002 Massachusetts Institute of Technology
3    *   
4    * Permission to use, copy, modify, and distribute this program for any
5    * purpose and without fee is hereby granted, provided that this
6    * copyright and permission notice appear on all copies and supporting
7    * documentation, the name of M.I.T. not be used in advertising or
8    * publicity pertaining to distribution of the program without specific
9    * prior permission, and notice be given in supporting documentation that
10   * copying and distribution is by permission of M.I.T.  M.I.T. makes no
11   * representations about the suitability of this software for any
12   * purpose.  It is provided "as is" without express or implied warranty.
13   */
14  package jsdsi;
15  
16  import java.util.ArrayList;
17  import java.util.Iterator;
18  import java.util.List;
19  
20  import jsdsi.sexp.*;
21  
22  /**
23   * An ACL entry that specifies a permission (the Tag) and a set of
24   * principals (the Subject) that may access the object protected by this
25   * entry's ACL.
26   * 
27   * @see Acl
28   * 
29   * @author Sameer Ajmani
30   * @version $Revision: 1.2 $ $Date: 2003/04/22 21:37:44 $
31   */
32  public class AclEntry extends Obj {
33    /**
34     * The subject of this <code>AclEntry</code>.
35     */
36    private final Subject subject;
37  
38    /**
39     * The permission of this ACL entry.
40     */
41    private final Auth auth;
42  
43    /**
44     * The validity of this <code>AclEntry</code>.
45     */
46    private final Validity validity;
47  
48    /**
49     * The comment of this <code>AclEntry</code>.
50     */
51    private final String comment;
52  
53    /**
54     * Creates a new <code>AclEntry</code> from a given subject, tag,
55     * delegation bit, validity, and comment.
56     * 
57     * @param  s subject of this <code>AclEntry</code>.
58     * @param  t tag of this <code>AclEntry</code>.
59     * @param  p delegation bit of this <code>AclEntry</code>.
60     * @param  v validity of this <code>AclEntry</code>.
61     * @param  c comment of this <code>AclEntry</code>.
62     */
63    public AclEntry(Subject s, Tag t, boolean p, Validity v, String c) {
64      assert(s != null) : "null subject";
65      assert(t != null) : "null tag";
66      subject = s;
67      auth = new Auth(t, p);
68      validity = v; // may be null
69      comment = c; // may be null
70    }
71  
72    /**
73     * Returns the subject of this <code>AclEntry</code>.
74     * 
75     * @return the subject of this <code>AclEntry</code>.
76     */
77    public Subject getSubject() {
78      return subject;
79    }
80  
81    /**
82     * Returns the tag of this <code>AclEntry</code>'s auth.
83     * 
84     * @return the tag of this <code>AclEntry</code>'s auth.
85     */
86    public Tag getTag() {
87      return auth.getTag();
88    }
89  
90    /**
91     * Returns the delegation bit of this <code>AclEntry</code>'s auth.
92     * 
93     * @return the delegation bit of this <code>AclEntry</code>' auth.
94     */
95    public boolean getPropagate() {
96      return auth.getPropagate();
97    }
98  
99    /**
100    * Returns the validity of this <code>AclEntry</code>
101    * (may be <code>null</code>).
102    * 
103    * @return the validity of this <code>AclEntry</code>.
104    */
105   public Validity getValidity() {
106     return validity;
107   }
108 
109   /**
110    * Returns the comment of this <code>AclEntry</code> 
111    * (may be <code>null</code>).
112    * 
113    * @return the comment of this <code>AclEntry</code>.
114    */
115   public String getComment() {
116     return comment;
117   }
118 
119   /**
120    * @see java.lang.Object#equals(Object)
121    */
122   public boolean equals(Object o) {
123     if (o instanceof AclEntry) {
124       AclEntry e = (AclEntry) o;
125       return subject.equals(e.subject)
126         && auth.equals(e.auth)
127         && Util.equals(validity, e.validity)
128         && Util.equals(comment, e.comment);
129     }
130     return false;
131   }
132 
133   /**
134    * @see java.lang.Object#hashCode()
135    */
136   public int hashCode() {
137     return subject.hashCode()
138       ^ auth.hashCode()
139       ^ Util.hashCode(validity)
140       ^ Util.hashCode(comment);
141   }
142 
143   /**
144    * Returns an <code>SexpList</code> that represents this
145    * <code>AclEntry</code>.
146    */
147   public SexpList toSexp() {
148     List l = new ArrayList(5);
149     l.add(getSubject().toSexp());
150     if (getPropagate()) {
151       l.add(SexpUtil.toSexp("propagate"));
152     }
153     l.add(getTag().toSexp());
154     if (getValidity() != null) {
155       l.add(getValidity().toSexp());
156     }
157     if (getComment() != null) {
158       l.add(SexpUtil.toSexpComment(getComment()));
159     }
160     return SexpUtil.toSexp("entry", l);
161   }
162 
163   /**
164    * Parses an <code>AclEntry</code> from a given <code>SexpList</code>.
165    * 
166    * @param l the <code>SexpList</code> to parse.
167    * @return the <code>AclEntry</code> contained in <code>l</code>. 
168    * @throws SexpParseException
169    */
170   static AclEntry parseAclEntry(SexpList l) throws SexpParseException {
171     Iterator ebody = SexpUtil.getBody(l);
172     // FIXME: same as Cert parsing!
173     // <sub-obj>
174     Subject subject =
175       Subject.Default.parseSubject(
176         SexpUtil.getNextList(ebody, "acl entry subject"));
177     // <deleg>? <tag>
178     boolean propagate = false;
179     SexpList propOrTag =
180       SexpUtil.getNextList(ebody, "cert propagate or tag");
181     String type = propOrTag.getType();
182     if (type.equals("propagate")) {
183       propagate = true;
184       SexpUtil.check(propOrTag.size() == 1, "extra fields in propagate");
185       propOrTag = SexpUtil.getNextList(ebody, "tag", "cert tag");
186     }
187     Tag tag = Tag.parseTag(propOrTag);
188     // <valid>? <comment>?
189     Validity validity = null;
190     String comment = null;
191     if (ebody.hasNext()) {
192       SexpList validOrComment =
193         SexpUtil.getNextList(ebody, "cert valid or comment");
194       type = validOrComment.getType();
195       if (type.equals("valid")) {
196         validity = Validity.parseValidity(validOrComment);
197         if (ebody.hasNext()) {
198           validOrComment =
199             SexpUtil.getNextList(ebody, "comment", "cert comment");
200           type = "comment"; // FIXME: ugly!
201         }
202       }
203       if (type.equals("comment")) {
204         Iterator combody = SexpUtil.getBody(validOrComment);
205         comment = SexpUtil.getNextString(combody, "comment body");
206         SexpUtil.checkDone(combody, "comment");
207       }
208     }
209     return new AclEntry(subject, tag, propagate, validity, comment);
210   }
211 }