1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.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 public class NamedEntityObjectIdentity implements AclObjectIdentity {
34
35
36 private String classname;
37 private String id;
38
39
40
41 public NamedEntityObjectIdentity(String classname, String id) {
42 Assert.hasText(classname, "classname required");
43 Assert.hasText(id, "id required");
44 this.classname = classname;
45 this.id = id;
46 }
47
48 protected NamedEntityObjectIdentity() {
49 throw new IllegalArgumentException("Cannot use default constructor");
50 }
51
52 /***
53 * Creates the <code>NamedEntityObjectIdentity</code> based on the passed
54 * object instance. The passed object must provide a <code>getId()</code>
55 * method, otherwise an exception will be thrown.
56 *
57 * @param object the domain object instance to create an identity for
58 *
59 * @throws IllegalAccessException
60 * @throws InvocationTargetException
61 * @throws IllegalArgumentException
62 */
63 public NamedEntityObjectIdentity(Object object)
64 throws IllegalAccessException, InvocationTargetException {
65 Assert.notNull(object, "object cannot be null");
66
67 this.classname = object.getClass().getName();
68
69 Class clazz = object.getClass();
70
71 try {
72 Method method = clazz.getMethod("getId", new Class[] {});
73 Object result = method.invoke(object, new Object[] {});
74 this.id = result.toString();
75 } catch (NoSuchMethodException nsme) {
76 throw new IllegalArgumentException("Object of class '" + clazz
77 + "' does not provide the required getId() method: " + object);
78 }
79 }
80
81
82
83 /***
84 * Important so caching operates properly.
85 *
86 * <P>
87 * Considers an object of the same class equal if it has the same
88 * <code>classname</code> and <code>id</code> properties.
89 * </p>
90 *
91 * @param arg0 object to compare
92 *
93 * @return <code>true</code> if the presented object matches this object
94 */
95 public boolean equals(Object arg0) {
96 if (arg0 == null) {
97 return false;
98 }
99
100 if (!(arg0 instanceof NamedEntityObjectIdentity)) {
101 return false;
102 }
103
104 NamedEntityObjectIdentity other = (NamedEntityObjectIdentity) arg0;
105
106 if (this.getId().equals(other.getId())
107 && this.getClassname().equals(other.getClassname())) {
108 return true;
109 }
110
111 return false;
112 }
113
114 /***
115 * Indicates the classname portion of the object identity.
116 *
117 * @return the classname (never <code>null</code>)
118 */
119 public String getClassname() {
120 return classname;
121 }
122
123 /***
124 * Indicates the instance identity portion of the object identity.
125 *
126 * @return the instance identity (never <code>null</code>)
127 */
128 public String getId() {
129 return id;
130 }
131
132 /***
133 * Important so caching operates properly.
134 *
135 * @return the hash of the classname and id
136 */
137 public int hashCode() {
138 StringBuffer sb = new StringBuffer();
139 sb.append(this.classname).append(this.id);
140
141 return sb.toString().hashCode();
142 }
143
144 public String toString() {
145 StringBuffer sb = new StringBuffer();
146 sb.append(this.getClass().getName()).append("[");
147 sb.append("Classname: ").append(this.classname);
148 sb.append("; Identity: ").append(this.id).append("]");
149
150 return sb.toString();
151 }
152 }