2
0
Эх сурвалжийг харах

Automatically introspect generic to determine supports(Class) instead of relying on IoC container or Java code to specify it.

Ben Alex 20 жил өмнө
parent
commit
fc1fe03871

+ 15 - 2
domain/src/main/java/org/acegisecurity/domain/hibernate/DaoHibernate.java

@@ -25,6 +25,7 @@ import net.sf.acegisecurity.domain.dao.Dao;
 import net.sf.acegisecurity.domain.dao.EvictionCapable;
 import net.sf.acegisecurity.domain.dao.InitializationCapable;
 import net.sf.acegisecurity.domain.dao.PaginatedList;
+import net.sf.acegisecurity.domain.util.GenericsUtils;
 import net.sf.acegisecurity.domain.validation.ValidationManager;
 
 import org.hibernate.Criteria;
@@ -62,6 +63,15 @@ public class DaoHibernate<E extends PersistableEntity> extends HibernateDaoSuppo
 	/** Enables mutator methods to validate an object prior to persistence */
 	private ValidationManager validationManager;
 
+	public DaoHibernate() {
+		this.supportsClass = GenericsUtils.getGeneric(getClass());
+		if (this.supportsClass == null) {
+			if (logger.isWarnEnabled()) {
+				logger.warn("Could not determine the generics type - you will need to set manually");
+			}
+		}
+	}
+	
     //~ Methods ================================================================
 
     public void setSupportsClass(Class supportClass) {
@@ -220,8 +230,7 @@ public class DaoHibernate<E extends PersistableEntity> extends HibernateDaoSuppo
     protected final void initDao() throws Exception {
         Assert.notNull(supportsClass, "supportClass is required");
 		Assert.notNull(validationManager, "validationManager is required");
-        Assert.isTrue(PersistableEntity.class.isAssignableFrom(supportsClass),
-            "supportClass is not an implementation of PersistableEntity");
+        Assert.isTrue(PersistableEntity.class.isAssignableFrom(supportsClass), "supportClass is not an implementation of PersistableEntity");
         initHibernateDao();
     }
 
@@ -338,6 +347,10 @@ public class DaoHibernate<E extends PersistableEntity> extends HibernateDaoSuppo
                         if (name.equals("version")) {
                             continue;
                         }
+                        
+                        if (name.equals("lastUpdatedUsername") || name.equals("lastUpdatedDate")) {
+                        	continue;
+                        }
 
                         if (type.equals(Hibernate.STRING)) {
                             // if the property is mapped as String, find partial match

+ 18 - 0
domain/src/main/java/org/acegisecurity/domain/service/ManagerImpl.java

@@ -16,13 +16,17 @@
 package net.sf.acegisecurity.domain.service;
 
 import java.io.Serializable;
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
 import java.util.Collection;
 import java.util.List;
 
 import net.sf.acegisecurity.domain.PersistableEntity;
 import net.sf.acegisecurity.domain.dao.Dao;
 import net.sf.acegisecurity.domain.dao.PaginatedList;
+import net.sf.acegisecurity.domain.util.GenericsUtils;
 
+import org.springframework.beans.factory.BeanNameAware;
 import org.springframework.beans.factory.InitializingBean;
 import org.springframework.context.support.ApplicationObjectSupport;
 import org.springframework.transaction.annotation.Transactional;
@@ -40,10 +44,20 @@ public class ManagerImpl<E extends PersistableEntity> extends ApplicationObjectS
 
     /** The class that this instance provides services for */
     private Class supportsClass;
+	private String beanName;
 	
 	protected Dao<E> dao;
 
     //~ Methods ================================================================
+	
+	public ManagerImpl() {
+		this.supportsClass = GenericsUtils.getGeneric(getClass());
+		if (supportsClass == null) {
+			if (logger.isWarnEnabled()) {
+				logger.warn("Could not determine the generics type - you will need to set manually");
+			}
+		}
+	}
 
     public void setSupportsClass(Class supportClass) {
         this.supportsClass = supportClass;
@@ -171,4 +185,8 @@ public class ManagerImpl<E extends PersistableEntity> extends ApplicationObjectS
 		}
         return dao.update(value);
     }
+
+	public void setBeanName(String beanName) {
+		this.beanName = beanName;
+	}
 }

+ 33 - 0
domain/src/main/java/org/acegisecurity/domain/util/GenericsUtils.java

@@ -0,0 +1,33 @@
+package net.sf.acegisecurity.domain.util;
+
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
+
+
+/**
+ * Provides a helper that locates the declarated generics type of a class.
+ *
+ * @author Ben Alex
+ * @version $Id$
+ */
+public class GenericsUtils {
+    /**
+     * Locates the first generic declaration on a class.
+     *
+     * @param clazz The class to introspect
+     * @return the first generic declaration, or <code>null</code> if cannot be determined
+     */
+    public static Class getGeneric(Class clazz) {
+        Type genType = clazz.getGenericSuperclass();
+
+        if (genType instanceof ParameterizedType) {
+            Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
+
+            if ((params != null) && (params.length == 1)) {
+                return (Class) params[0];
+            }
+        }
+
+        return null;
+    }
+}