Coverage Report - org.acegisecurity.acls.objectidentity.ObjectIdentityImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
ObjectIdentityImpl
39% 
75% 
2.125
 
 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.acegisecurity.acls.objectidentity;
 16  
 
 17  
 import org.acegisecurity.acls.IdentityUnavailableException;
 18  
 
 19  
 import org.springframework.util.Assert;
 20  
 import org.springframework.util.ReflectionUtils;
 21  
 
 22  
 import java.io.Serializable;
 23  
 
 24  
 import java.lang.reflect.Method;
 25  
 
 26  
 
 27  
 /**
 28  
  * Simple implementation of {@link org.acegisecurity.acl.basic.AclObjectIdentity AclObjectIdentity}.
 29  
  * <p>
 30  
  * Uses <code>String</code>s to store the identity of the domain object instance. Also offers a constructor that uses
 31  
  * reflection to build the identity information.
 32  
  * </p>
 33  
  */
 34  
 public class ObjectIdentityImpl implements ObjectIdentity {
 35  
     //~ Instance fields ================================================================================================
 36  
 
 37  
     private Class javaType;
 38  
     private Serializable identifier;
 39  
 
 40  
     //~ Constructors ===================================================================================================
 41  
 
 42  12
     public ObjectIdentityImpl(String javaType, Serializable identifier) {
 43  12
         Assert.hasText(javaType, "Java Type required");
 44  12
         Assert.notNull(identifier, "identifier required");
 45  
 
 46  
         try {
 47  12
             this.javaType = Class.forName(javaType);
 48  0
         } catch (Exception ex) {
 49  0
             ReflectionUtils.handleReflectionException(ex);
 50  12
         }
 51  
 
 52  12
         this.identifier = identifier;
 53  12
     }
 54  
 
 55  0
     public ObjectIdentityImpl(Class javaType, Serializable identifier) {
 56  0
         Assert.notNull(javaType, "Java Type required");
 57  0
         Assert.notNull(identifier, "identifier required");
 58  0
         this.javaType = javaType;
 59  0
         this.identifier = identifier;
 60  0
     }
 61  
 
 62  
 /**
 63  
      * Creates the <code>ObjectIdentityImpl</code> based on the passed
 64  
      * object instance. The passed object must provide a <code>getId()</code>
 65  
      * method, otherwise an exception will be thrown. The object passed will
 66  
      * be considered the {@link #javaType}, so if more control is required,
 67  
      * an alternate constructor should be used instead.
 68  
      *
 69  
      * @param object the domain object instance to create an identity for
 70  
      *
 71  
      * @throws IdentityUnavailableException if identity could not be extracted
 72  
      */
 73  0
     public ObjectIdentityImpl(Object object) throws IdentityUnavailableException {
 74  0
         Assert.notNull(object, "object cannot be null");
 75  
 
 76  0
         this.javaType = object.getClass();
 77  
 
 78  
         Object result;
 79  
 
 80  
         try {
 81  0
             Method method = this.javaType.getMethod("getId", new Class[] {});
 82  0
             result = method.invoke(object, new Object[] {});
 83  0
         } catch (Exception e) {
 84  0
             throw new IdentityUnavailableException("Could not extract identity from object " + object, e);
 85  0
         }
 86  
 
 87  0
         Assert.notNull(result, "getId() is required to return a non-null value");
 88  0
         Assert.isInstanceOf(Serializable.class, result, "Getter must provide a return value of type Serializable");
 89  0
         this.identifier = (Serializable) result;
 90  0
     }
 91  
 
 92  
     //~ Methods ========================================================================================================
 93  
 
 94  
     /**
 95  
      * Important so caching operates properly.<P>Considers an object of the same class equal if it has the same
 96  
      * <code>classname</code> and <code>id</code> properties.</p>
 97  
      *
 98  
      * @param arg0 object to compare
 99  
      *
 100  
      * @return <code>true</code> if the presented object matches this object
 101  
      */
 102  
     public boolean equals(Object arg0) {
 103  35
         if (arg0 == null) {
 104  0
             return false;
 105  
         }
 106  
 
 107  35
         if (!(arg0 instanceof ObjectIdentityImpl)) {
 108  0
             return false;
 109  
         }
 110  
 
 111  35
         ObjectIdentityImpl other = (ObjectIdentityImpl) arg0;
 112  
 
 113  35
         if (this.getIdentifier().equals(other.getIdentifier()) && this.getJavaType().equals(other.getJavaType())) {
 114  35
             return true;
 115  
         }
 116  
 
 117  0
         return false;
 118  
     }
 119  
 
 120  
     public Serializable getIdentifier() {
 121  105
         return identifier;
 122  
     }
 123  
 
 124  
     public Class getJavaType() {
 125  96
         return javaType;
 126  
     }
 127  
 
 128  
     /**
 129  
      * Important so caching operates properly.
 130  
      *
 131  
      * @return the hash
 132  
      */
 133  
     public int hashCode() {
 134  221
         int code = 31;
 135  221
         code ^= this.javaType.hashCode();
 136  221
         code ^= this.identifier.hashCode();
 137  
 
 138  221
         return code;
 139  
     }
 140  
 
 141  
     public String toString() {
 142  0
         StringBuffer sb = new StringBuffer();
 143  0
         sb.append(this.getClass().getName()).append("[");
 144  0
         sb.append("Java Type: ").append(this.javaType);
 145  0
         sb.append("; Identifier: ").append(this.identifier).append("]");
 146  
 
 147  0
         return sb.toString();
 148  
     }
 149  
 }