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

Quick Search    Search Deep

Source code: net/sf/acegisecurity/acl/basic/NamedEntityObjectIdentity.java


1   /* Copyright 2004 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  
16  package net.sf.acegisecurity.acl.basic;
17  
18  import org.springframework.util.Assert;
19  
20  import java.lang.reflect.InvocationTargetException;
21  import java.lang.reflect.Method;
22  
23  
24  /**
25   * Simple implementation of {@link AclObjectIdentity}.
26   * 
27   * <P>
28   * Uses <code>String</code>s to store the identity of the domain object
29   * instance. Also offers a constructor that uses reflection to build the
30   * identity information.
31   * </p>
32   *
33   * @author Ben Alex
34   * @version $Id: NamedEntityObjectIdentity.java,v 1.3 2005/04/15 01:21:33 luke_t Exp $
35   */
36  public class NamedEntityObjectIdentity implements AclObjectIdentity {
37      //~ Instance fields ========================================================
38  
39      private String classname;
40      private String id;
41  
42      //~ Constructors ===========================================================
43  
44      public NamedEntityObjectIdentity(String classname, String id) {
45          if ((classname == null) || "".equals(classname)) {
46              throw new IllegalArgumentException("classname required");
47          }
48  
49          if ((id == null) || "".equals(id)) {
50              throw new IllegalArgumentException("id required");
51          }
52  
53          this.classname = classname;
54          this.id = id;
55      }
56  
57      /**
58       * Creates the <code>NamedEntityObjectIdentity</code> based on the passed
59       * object instance. The passed object must provide a <code>getId()</code>
60       * method, otherwise an exception will be thrown.
61       *
62       * @param object the domain object instance to create an identity for
63       *
64       * @throws IllegalAccessException
65       * @throws InvocationTargetException
66       * @throws IllegalArgumentException
67       */
68      public NamedEntityObjectIdentity(Object object)
69          throws IllegalAccessException, InvocationTargetException {
70          Assert.notNull(object, "object cannot be null");
71  
72          this.classname = object.getClass().getName();
73  
74          Class clazz = object.getClass();
75  
76          try {
77              Method method = clazz.getMethod("getId", null);
78              Object result = method.invoke(object, null);
79              this.id = result.toString();
80          } catch (NoSuchMethodException nsme) {
81              throw new IllegalArgumentException("Object of class '" + clazz
82                      + "' does not provide the required getId() method: " + object);
83          }
84      }
85  
86      protected NamedEntityObjectIdentity() {
87          throw new IllegalArgumentException("Cannot use default constructor");
88      }
89  
90      //~ Methods ================================================================
91  
92      /**
93       * Indicates the classname portion of the object identity.
94       *
95       * @return the classname (never <code>null</code>)
96       */
97      public String getClassname() {
98          return classname;
99      }
100 
101     /**
102      * Indicates the instance identity portion of the object identity.
103      *
104      * @return the instance identity (never <code>null</code>)
105      */
106     public String getId() {
107         return id;
108     }
109 
110     /**
111      * Important so caching operates properly.
112      * 
113      * <P>
114      * Considers an object of the same class equal if it has the same
115      * <code>classname</code> and <code>id</code> properties.
116      * </p>
117      *
118      * @param arg0 object to compare
119      *
120      * @return <code>true</code> if the presented object matches this object
121      */
122     public boolean equals(Object arg0) {
123         if (arg0 == null) {
124             return false;
125         }
126 
127         if (!(arg0 instanceof NamedEntityObjectIdentity)) {
128             return false;
129         }
130 
131         NamedEntityObjectIdentity other = (NamedEntityObjectIdentity) arg0;
132 
133         if (this.getId().equals(other.getId())
134             && this.getClassname().equals(other.getClassname())) {
135             return true;
136         }
137 
138         return false;
139     }
140 
141     /**
142      * Important so caching operates properly.
143      *
144      * @return the hash of the classname and id
145      */
146     public int hashCode() {
147         StringBuffer sb = new StringBuffer();
148         sb.append(this.classname).append(this.id);
149 
150         return sb.toString().hashCode();
151     }
152 
153     public String toString() {
154         StringBuffer sb = new StringBuffer();
155         sb.append(this.getClass().getName()).append("[");
156         sb.append("Classname: ").append(this.classname);
157         sb.append("; Identity: ").append(this.id).append("]");
158 
159         return sb.toString();
160     }
161 }