ObjectIdentityImpl.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. import org.acegisecurity.acl.basic.AclObjectIdentity;
  17. import org.acegisecurity.acls.IdentityUnavailableException;
  18. import org.springframework.util.Assert;
  19. import org.springframework.util.ReflectionUtils;
  20. import java.io.Serializable;
  21. import java.lang.reflect.Method;
  22. /**
  23. * Simple implementation of {@link AclObjectIdentity}.
  24. *
  25. * <P>
  26. * Uses <code>String</code>s to store the identity of the domain object
  27. * instance. Also offers a constructor that uses reflection to build the
  28. * identity information.
  29. * </p>
  30. */
  31. public class ObjectIdentityImpl implements ObjectIdentity {
  32. //~ Instance fields ========================================================
  33. private Class javaType;
  34. private Serializable identifier;
  35. //~ Constructors ===========================================================
  36. public ObjectIdentityImpl(String javaType, Serializable identifier) {
  37. Assert.hasText(javaType, "Java Type required");
  38. Assert.notNull(identifier, "identifier required");
  39. try {
  40. this.javaType = Class.forName(javaType);
  41. } catch (Exception ex) {
  42. ReflectionUtils.handleReflectionException(ex);
  43. }
  44. this.identifier = identifier;
  45. }
  46. public ObjectIdentityImpl(Class javaType, Serializable identifier) {
  47. Assert.notNull(javaType, "Java Type required");
  48. Assert.notNull(identifier, "identifier required");
  49. this.javaType = javaType;
  50. this.identifier = identifier;
  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 IdentityUnavailableException if identity could not be extracted
  60. */
  61. public ObjectIdentityImpl(Object object)
  62. throws IdentityUnavailableException {
  63. Assert.notNull(object, "object cannot be null");
  64. this.javaType = object.getClass();
  65. Object result;
  66. try {
  67. Method method = this.javaType.getMethod("getId", new Class[] {});
  68. result = method.invoke(object, new Object[] {});
  69. } catch (Exception e) {
  70. throw new IdentityUnavailableException(
  71. "Could not extract identity from object " + object, e);
  72. }
  73. Assert.isInstanceOf(Serializable.class, result,
  74. "Getter must provide a return value of type Serializable");
  75. this.identifier = (Serializable) result;
  76. }
  77. //~ Methods ================================================================
  78. /**
  79. * Important so caching operates properly.
  80. *
  81. * <P>
  82. * Considers an object of the same class equal if it has the same
  83. * <code>classname</code> and <code>id</code> properties.
  84. * </p>
  85. *
  86. * @param arg0 object to compare
  87. *
  88. * @return <code>true</code> if the presented object matches this object
  89. */
  90. public boolean equals(Object arg0) {
  91. if (arg0 == null) {
  92. return false;
  93. }
  94. if (!(arg0 instanceof ObjectIdentityImpl)) {
  95. return false;
  96. }
  97. ObjectIdentityImpl other = (ObjectIdentityImpl) arg0;
  98. if (this.getIdentifier().equals(other.getIdentifier())
  99. && this.getJavaType().equals(other.getJavaType())) {
  100. return true;
  101. }
  102. return false;
  103. }
  104. public Serializable getIdentifier() {
  105. return identifier;
  106. }
  107. public Class getJavaType() {
  108. return javaType;
  109. }
  110. /**
  111. * Important so caching operates properly.
  112. *
  113. * @return the hash
  114. */
  115. public int hashCode() {
  116. int code = 31;
  117. code ^= this.javaType.hashCode();
  118. code ^= this.identifier.hashCode();
  119. return code;
  120. }
  121. public String toString() {
  122. StringBuffer sb = new StringBuffer();
  123. sb.append(this.getClass().getName()).append("[");
  124. sb.append("Java Type: ").append(this.javaType);
  125. sb.append("; Identifier: ").append(this.identifier).append("]");
  126. return sb.toString();
  127. }
  128. }