| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
| NamedEntityObjectIdentity |
|
| 2.25;2.25 |
| 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 | ||
| 16 | package org.acegisecurity.acl.basic; |
|
| 17 | ||
| 18 | import org.springframework.util.Assert; |
|
| 19 | import org.springframework.util.ClassUtils; |
|
| 20 | ||
| 21 | import java.lang.reflect.InvocationTargetException; |
|
| 22 | import java.lang.reflect.Method; |
|
| 23 | ||
| 24 | ||
| 25 | /** |
|
| 26 | * Simple implementation of {@link AclObjectIdentity}.<P>Uses <code>String</code>s to store the identity of the |
|
| 27 | * domain object instance. Also offers a constructor that uses reflection to build the identity information.</p> |
|
| 28 | */ |
|
| 29 | public class NamedEntityObjectIdentity implements AclObjectIdentity { |
|
| 30 | //~ Instance fields ================================================================================================ |
|
| 31 | ||
| 32 | private String classname; |
|
| 33 | private String id; |
|
| 34 | ||
| 35 | //~ Constructors =================================================================================================== |
|
| 36 | ||
| 37 | 202 | public NamedEntityObjectIdentity(String classname, String id) { |
| 38 | 202 | Assert.hasText(classname, "classname required"); |
| 39 | 200 | Assert.hasText(id, "id required"); |
| 40 | 198 | this.classname = classname; |
| 41 | 198 | this.id = id; |
| 42 | 198 | } |
| 43 | ||
| 44 | /** |
|
| 45 | * Creates the <code>NamedEntityObjectIdentity</code> based on the passed |
|
| 46 | * object instance. The passed object must provide a <code>getId()</code> |
|
| 47 | * method, otherwise an exception will be thrown. |
|
| 48 | * |
|
| 49 | * @param object the domain object instance to create an identity for |
|
| 50 | * |
|
| 51 | * @throws IllegalAccessException |
|
| 52 | * @throws InvocationTargetException |
|
| 53 | * @throws IllegalArgumentException |
|
| 54 | */ |
|
| 55 | 9 | public NamedEntityObjectIdentity(Object object) throws IllegalAccessException, InvocationTargetException { |
| 56 | 9 | Assert.notNull(object, "object cannot be null"); |
| 57 | ||
| 58 | 8 | this.classname = (getPackageName(object.getClass().getName()) == null) |
| 59 | ? ClassUtils.getShortName(object.getClass()) |
|
| 60 | : (getPackageName(object.getClass().getName()) + "." + ClassUtils.getShortName(object.getClass())); |
|
| 61 | ||
| 62 | 8 | Class clazz = object.getClass(); |
| 63 | ||
| 64 | try { |
|
| 65 | 8 | Method method = clazz.getMethod("getId", new Class[] {}); |
| 66 | 3 | Object result = method.invoke(object, new Object[] {}); |
| 67 | 3 | this.id = result.toString(); |
| 68 | 5 | } catch (NoSuchMethodException nsme) { |
| 69 | 5 | throw new IllegalArgumentException("Object of class '" + clazz |
| 70 | + "' does not provide the required getId() method: " + object); |
|
| 71 | 3 | } |
| 72 | 3 | } |
| 73 | ||
| 74 | //~ Methods ======================================================================================================== |
|
| 75 | ||
| 76 | /** |
|
| 77 | * Important so caching operates properly.<P>Considers an object of the same class equal if it has the same |
|
| 78 | * <code>classname</code> and <code>id</code> properties.</p> |
|
| 79 | * |
|
| 80 | * @param arg0 object to compare |
|
| 81 | * |
|
| 82 | * @return <code>true</code> if the presented object matches this object |
|
| 83 | */ |
|
| 84 | public boolean equals(Object arg0) { |
|
| 85 | 14 | if (arg0 == null) { |
| 86 | 1 | return false; |
| 87 | } |
|
| 88 | ||
| 89 | 13 | if (!(arg0 instanceof NamedEntityObjectIdentity)) { |
| 90 | 1 | return false; |
| 91 | } |
|
| 92 | ||
| 93 | 12 | NamedEntityObjectIdentity other = (NamedEntityObjectIdentity) arg0; |
| 94 | ||
| 95 | 12 | if (this.getId().equals(other.getId()) && this.getClassname().equals(other.getClassname())) { |
| 96 | 11 | return true; |
| 97 | } |
|
| 98 | ||
| 99 | 1 | return false; |
| 100 | } |
|
| 101 | ||
| 102 | /** |
|
| 103 | * Indicates the classname portion of the object identity. |
|
| 104 | * |
|
| 105 | * @return the classname (never <code>null</code>) |
|
| 106 | */ |
|
| 107 | public String getClassname() { |
|
| 108 | 84 | return classname; |
| 109 | } |
|
| 110 | ||
| 111 | /** |
|
| 112 | * Indicates the instance identity portion of the object identity. |
|
| 113 | * |
|
| 114 | * @return the instance identity (never <code>null</code>) |
|
| 115 | */ |
|
| 116 | public String getId() { |
|
| 117 | 86 | return id; |
| 118 | } |
|
| 119 | ||
| 120 | private String getPackageName(String className) { |
|
| 121 | 16 | Assert.hasLength(className, "class name must not be empty"); |
| 122 | ||
| 123 | 16 | int lastDotIndex = className.lastIndexOf("."); |
| 124 | ||
| 125 | 16 | if (lastDotIndex == -1) { |
| 126 | 0 | return null; |
| 127 | } |
|
| 128 | ||
| 129 | 16 | return className.substring(0, lastDotIndex); |
| 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 | 30 | StringBuffer sb = new StringBuffer(); |
| 139 | 30 | sb.append(this.classname).append(this.id); |
| 140 | ||
| 141 | 30 | return sb.toString().hashCode(); |
| 142 | } |
|
| 143 | ||
| 144 | public String toString() { |
|
| 145 | 63 | StringBuffer sb = new StringBuffer(); |
| 146 | 63 | sb.append(this.getClass().getName()).append("["); |
| 147 | 63 | sb.append("Classname: ").append(this.classname); |
| 148 | 63 | sb.append("; Identity: ").append(this.id).append("]"); |
| 149 | ||
| 150 | 63 | return sb.toString(); |
| 151 | } |
|
| 152 | } |