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

This commit is contained in:
Ben Alex 2005-08-21 10:36:26 +00:00
parent ebb99abc78
commit fc1fe03871
3 changed files with 66 additions and 2 deletions

View File

@ -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();
}
@ -339,6 +348,10 @@ public class DaoHibernate<E extends PersistableEntity> extends HibernateDaoSuppo
continue;
}
if (name.equals("lastUpdatedUsername") || name.equals("lastUpdatedDate")) {
continue;
}
if (type.equals(Hibernate.STRING)) {
// if the property is mapped as String, find partial match
criteria.add(Expression.ilike(name,

View File

@ -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,11 +44,21 @@ 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;
}
}

View File

@ -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;
}
}