View Javadoc

1   /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
2    *
3    * Licensed under the Apache License, Version 2.0 (the "License");
4    * you may not use this file except in compliance with the License.
5    * You may obtain a copy of the License at
6    *
7    *     http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.springframework.security.acls.domain;
16  
17  import org.springframework.security.acls.AccessControlEntry;
18  import org.springframework.security.acls.Acl;
19  import org.springframework.security.acls.AuditableAccessControlEntry;
20  import org.springframework.security.acls.Permission;
21  import org.springframework.security.acls.sid.Sid;
22  
23  import org.springframework.util.Assert;
24  
25  import java.io.Serializable;
26  
27  
28  /**
29   * An immutable default implementation of <code>AccessControlEntry</code>.
30   *
31   * @author Ben Alex
32   * @version $Id: AccessControlEntryImpl.java 2864 2008-04-05 05:58:11Z benalex $
33   */
34  public class AccessControlEntryImpl implements AccessControlEntry, AuditableAccessControlEntry {
35      //~ Instance fields ================================================================================================
36  
37      private Acl acl;
38      private Permission permission;
39      private Serializable id;
40      private Sid sid;
41      private boolean auditFailure = false;
42      private boolean auditSuccess = false;
43      private boolean granting;
44  
45      //~ Constructors ===================================================================================================
46  
47      public AccessControlEntryImpl(Serializable id, Acl acl, Sid sid, Permission permission, boolean granting,
48          boolean auditSuccess, boolean auditFailure) {
49          Assert.notNull(acl, "Acl required");
50          Assert.notNull(sid, "Sid required");
51          Assert.notNull(permission, "Permission required");
52          this.id = id;
53          this.acl = acl; // can be null
54          this.sid = sid;
55          this.permission = permission;
56          this.granting = granting;
57          this.auditSuccess = auditSuccess;
58          this.auditFailure = auditFailure;
59      }
60  
61      //~ Methods ========================================================================================================
62  
63      public boolean equals(Object arg0) {
64          if (!(arg0 instanceof AccessControlEntryImpl)) {
65              return false;
66          }
67  
68          AccessControlEntryImpl rhs = (AccessControlEntryImpl) arg0;
69  
70          if (this.acl == null) {
71              if (rhs.getAcl() != null) {
72                  return false;
73              }
74              // Both this.acl and rhs.acl are null and thus equal
75          } else {
76              // this.acl is non-null
77              if (rhs.getAcl() == null) {
78                  return false;
79              }
80              
81              // Both this.acl and rhs.acl are non-null, so do a comparison
82              if (this.acl.getObjectIdentity() == null) {
83                  if (rhs.acl.getObjectIdentity() != null) {
84                      return false;
85                  }
86                  // Both this.acl and rhs.acl are null and thus equal
87              } else {
88                  // Both this.acl.objectIdentity and rhs.acl.objectIdentity are non-null
89                  if (!this.acl.getObjectIdentity().equals(rhs.getAcl().getObjectIdentity())) {
90                      return false;
91                  }
92              }
93          }
94          
95          if (this.id == null) {
96              if (rhs.id != null) {
97                  return false;
98              }
99              // Both this.id and rhs.id are null and thus equal
100         } else {
101             // this.id is non-null
102             if (rhs.id == null) {
103                 return false;
104             }
105 
106             // Both this.id and rhs.id are non-null
107             if (!this.id.equals(rhs.id)) {
108                 return false;
109             }
110         }
111         
112         if ((this.auditFailure != rhs.isAuditFailure()) || (this.auditSuccess != rhs.isAuditSuccess())
113             || (this.granting != rhs.isGranting())
114             || !this.permission.equals(rhs.getPermission()) || !this.sid.equals(rhs.getSid())) {
115             return false;
116         }
117 
118         return true;
119     }
120 
121     public Acl getAcl() {
122         return acl;
123     }
124 
125     public Serializable getId() {
126         return id;
127     }
128 
129     public Permission getPermission() {
130         return permission;
131     }
132 
133     public Sid getSid() {
134         return sid;
135     }
136 
137     public boolean isAuditFailure() {
138         return auditFailure;
139     }
140 
141     public boolean isAuditSuccess() {
142         return auditSuccess;
143     }
144 
145     public boolean isGranting() {
146         return granting;
147     }
148 
149     void setAuditFailure(boolean auditFailure) {
150         this.auditFailure = auditFailure;
151     }
152 
153     void setAuditSuccess(boolean auditSuccess) {
154         this.auditSuccess = auditSuccess;
155     }
156 
157     void setPermission(Permission permission) {
158         Assert.notNull(permission, "Permission required");
159         this.permission = permission;
160     }
161 
162     public String toString() {
163         StringBuffer sb = new StringBuffer();
164         sb.append("AccessControlEntryImpl[");
165         sb.append("id: ").append(this.id).append("; ");
166         sb.append("granting: ").append(this.granting).append("; ");
167         sb.append("sid: ").append(this.sid).append("; ");
168         sb.append("permission: ").append(this.permission).append("; ");
169         sb.append("auditSuccess: ").append(this.auditSuccess).append("; ");
170         sb.append("auditFailure: ").append(this.auditFailure);
171         sb.append("]");
172 
173         return sb.toString();
174     }
175 }