View Javadoc

    1   /* Copyright 2004, 2005 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
    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      //~ Instance fields ========================================================
    35
    36      private String classname;
    37      private String id;
    38
    39      //~ Constructors ===========================================================
    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      //~ Methods ================================================================
    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 }