Domain subproject Java 1.5 compatibility.

This commit is contained in:
Ben Alex 2005-05-09 03:35:57 +00:00
parent de6a258460
commit 5f1cb77e40
18 changed files with 297 additions and 168 deletions

View File

@ -220,5 +220,13 @@
<attributes> <attributes>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry kind="var" path="MAVEN_REPO/hibernate/jars/ejb-3.0-edr2.jar">
<attributes>
</attributes>
</classpathentry>
<classpathentry kind="var" path="MAVEN_REPO/hibernate/jars/hibernate-annotations-3.0-beta1.jar">
<attributes>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/eclipseclasses"/> <classpathentry kind="output" path="target/eclipseclasses"/>
</classpath> </classpath>

View File

@ -24,6 +24,9 @@
xmlns:util="jelly:util" xmlns:util="jelly:util"
xmlns:maven="jelly:maven" xmlns:maven="jelly:maven"
> >
<!--
Disabled signing the domain JAR for now because it causes Hibernate issues
if people subclass from the net.sf.acegitech.domain.impl package.
<postGoal name="jar:jar"> <postGoal name="jar:jar">
<j:if test="${context.getVariable('signature.alias') != null}"> <j:if test="${context.getVariable('signature.alias') != null}">
@ -35,5 +38,5 @@
</ant:signjar> </ant:signjar>
</j:if> </j:if>
</postGoal> </postGoal>
-->
</project> </project>

View File

@ -17,6 +17,21 @@
<artifactId>hibernate</artifactId> <artifactId>hibernate</artifactId>
<version>3.0.1</version> <version>3.0.1</version>
<type>jar</type> <type>jar</type>
<url>http://www.hibernate.org</url>
</dependency>
<dependency>
<groupId>hibernate</groupId>
<artifactId>ejb</artifactId>
<version>3.0-edr2</version>
<type>jar</type>
<url>http://www.hibernate.org</url>
</dependency>
<dependency>
<groupId>hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.0-beta1</version>
<type>jar</type>
<url>http://www.hibernate.org</url>
</dependency> </dependency>
<dependency> <dependency>
<groupId>commons-lang</groupId> <groupId>commons-lang</groupId>

View File

@ -22,10 +22,9 @@ import java.io.Serializable;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
/** /**
* Provides fundamental DAO capabilities for a single concrete {@link * Provides fundamental DAO capabilities for a single concrete {@link
* PersistableEntity}. * PersistableEntity}, using JDK 1.5 generics.
* *
* <P> * <P>
* This interface provides a portable approach to Data Access Object (DAO) * This interface provides a portable approach to Data Access Object (DAO)
@ -60,7 +59,7 @@ import java.util.List;
* @author Ben Alex * @author Ben Alex
* @version $Id$ * @version $Id$
*/ */
public interface Dao { public interface Dao<E extends PersistableEntity> {
//~ Methods ================================================================ //~ Methods ================================================================
/** /**
@ -71,7 +70,7 @@ public interface Dao {
* *
* @return the value created (with the identity property initialised) * @return the value created (with the identity property initialised)
*/ */
public PersistableEntity create(PersistableEntity value); public E create(E value);
/** /**
* Saves an existing object to the persistence layer, or creates a new * Saves an existing object to the persistence layer, or creates a new
@ -84,14 +83,14 @@ public interface Dao {
* *
* @return the saved or updated (as appropriate) value * @return the saved or updated (as appropriate) value
*/ */
public PersistableEntity createOrUpdate(PersistableEntity value); public E createOrUpdate(E value);
/** /**
* Delete an object. * Delete an object.
* *
* @param value the value to delete * @param value the value to delete
*/ */
public void delete(PersistableEntity value); public void delete(E value);
/** /**
* Return all persistent instances, including subclasses. * Return all persistent instances, including subclasses.
@ -99,7 +98,7 @@ public interface Dao {
* @return all persistence instances (an empty <code>List</code> will be * @return all persistence instances (an empty <code>List</code> will be
* returned if no matches are found) * returned if no matches are found)
*/ */
public List findAll(); public List<E> findAll();
/** /**
* Find a <code>List</code> of <code>PersistableEntity</code>s, searched by * Find a <code>List</code> of <code>PersistableEntity</code>s, searched by
@ -110,7 +109,7 @@ public interface Dao {
* @return the values with those identifiers (an empty <code>List</code> * @return the values with those identifiers (an empty <code>List</code>
* will be returned if no matches are found) * will be returned if no matches are found)
*/ */
public List findId(Collection ids); public List<E> findId(Collection<Serializable> ids);
/** /**
* Load a persistent instance by its identifier. * Load a persistent instance by its identifier.
@ -120,7 +119,7 @@ public interface Dao {
* *
* @return the request item, or <code>null</code> if not found * @return the request item, or <code>null</code> if not found
*/ */
public PersistableEntity readId(Serializable id); public E readId(Serializable id);
/** /**
* Find persistent instances with properties matching those of the passed * Find persistent instances with properties matching those of the passed
@ -133,8 +132,8 @@ public interface Dao {
* the query by example evaluation. * the query by example evaluation.
* </p> * </p>
* *
* @param value parameters to filter on (the class of this object will be * @param value parameters to filter on (the class of this object will
* added to the filter) * be added to the filter)
* @param firstElement the first result (start at zero to obtain all * @param firstElement the first result (start at zero to obtain all
* results) * results)
* @param maxElements the maximum number of results desired for this page * @param maxElements the maximum number of results desired for this page
@ -146,17 +145,17 @@ public interface Dao {
* @return the requested page of the result list (a properly formed * @return the requested page of the result list (a properly formed
* <code>PaginatedList</code> is returned if no results match) * <code>PaginatedList</code> is returned if no results match)
*/ */
public PaginatedList scroll(PersistableEntity value, int firstElement, public PaginatedList<E> scroll(E value, int firstElement,
int maxElements, String orderByAsc); int maxElements, String orderByAsc);
/** /**
* Find persistent instances with properties matching those of the passed * Find persistent instances with properties matching those of the passed
* <code>PersistableEntity</code>, ignoring the class of the passed * <code>PersistableEntity</code>, ignoring the class of the passed
* <code>PersistableEntity</code> (useful if you pass a superclass, as you * <code>PersistableEntity</code> (useful if you pass a superclass, as you
* want to find all subclass instances which match). * want to find all subclass instances which match).
* *
* @param value parameters to filter on (the class of this object will NOT * @param value parameters to filter on (the class of this object will
* be added to the filter) * NOT be added to the filter)
* @param firstElement the first result (start at zero to obtain all * @param firstElement the first result (start at zero to obtain all
* results) * results)
* @param maxElements the maximum number of results desired for this page * @param maxElements the maximum number of results desired for this page
@ -167,11 +166,11 @@ public interface Dao {
* *
* @return the requested page of the result list (a properly formed * @return the requested page of the result list (a properly formed
* <code>PaginatedList</code> is returned if no results match) * <code>PaginatedList</code> is returned if no results match)
*/ */
public PaginatedList scrollWithSubclasses(PersistableEntity value, public PaginatedList<E> scrollWithSubclasses(E value, int firstElement,
int firstElement, int maxElements, String orderByAsc); int maxElements, String orderByAsc);
/** /**
* Indicates whether the DAO instance provides persistence services for the * Indicates whether the DAO instance provides persistence services for the
* specified class. * specified class.
* *
@ -191,5 +190,5 @@ public interface Dao {
* *
* @return the updated value * @return the updated value
*/ */
public PersistableEntity update(PersistableEntity value); public E update(E value);
} }

View File

@ -62,7 +62,7 @@ public class EvictionUtils {
* @param collection whose members to evict (never <code>null</code>) * @param collection whose members to evict (never <code>null</code>)
*/ */
public static void evictIfRequired(Object daoOrServices, public static void evictIfRequired(Object daoOrServices,
Collection collection) { Collection<Object> collection) {
Assert.notNull(collection, "Cannot evict a null Collection"); Assert.notNull(collection, "Cannot evict a null Collection");
if (getEvictionCapable(daoOrServices) == null) { if (getEvictionCapable(daoOrServices) == null) {
@ -70,7 +70,7 @@ public class EvictionUtils {
return; return;
} }
Iterator iter = collection.iterator(); Iterator<Object> iter = collection.iterator();
while (iter.hasNext()) { while (iter.hasNext()) {
Object obj = iter.next(); Object obj = iter.next();

View File

@ -30,7 +30,7 @@ import java.util.Vector;
/** /**
* <p> * <p>
* Represents a paginated <code>List</code>. * JDK1.5 compatible paginated <code>List</code>.
* </p> * </p>
* *
* <p> * <p>
@ -52,11 +52,11 @@ import java.util.Vector;
* @author Ben Alex * @author Ben Alex
* @version $Id$ * @version $Id$
*/ */
public class PaginatedList implements List { public class PaginatedList<E extends PersistableEntity> implements List<E> {
//~ Instance fields ======================================================== //~ Instance fields ========================================================
protected final transient Log logger = LogFactory.getLog(getClass()); protected final transient Log logger = LogFactory.getLog(getClass());
private List list; private List<E> list;
private int firstElement; private int firstElement;
private int maxElements; private int maxElements;
private int size; private int size;
@ -73,23 +73,23 @@ public class PaginatedList implements List {
* @param entity the entity to include (can be <code>null</code>, which * @param entity the entity to include (can be <code>null</code>, which
* indicates an empty <code>PaginatedList</code> should be created) * indicates an empty <code>PaginatedList</code> should be created)
*/ */
public PaginatedList(PersistableEntity entity) { public PaginatedList(E entity) {
if (entity == null) { if (entity == null) {
this.list = new Vector(); this.list = new Vector<E>();
this.firstElement = 0; this.firstElement = 0;
this.maxElements = Integer.MAX_VALUE; this.maxElements = Integer.MAX_VALUE;
this.size = 0; this.size = 0;
} else { } else {
List list = new Vector(); List<E> myList = new Vector<E>();
list.add(entity); myList.add(entity);
this.list = list; this.list = myList;
this.firstElement = 0; this.firstElement = 0;
this.maxElements = Integer.MAX_VALUE; this.maxElements = Integer.MAX_VALUE;
this.size = 1; this.size = 1;
} }
} }
public PaginatedList(List list, int firstElement, int maxElements, int size) { public PaginatedList(List<E> list, int firstElement, int maxElements, int size) {
this.list = list; this.list = list;
this.firstElement = firstElement; this.firstElement = firstElement;
this.maxElements = maxElements; this.maxElements = maxElements;
@ -133,7 +133,7 @@ public class PaginatedList implements List {
return (size() - 1) / getMaxElements(); return (size() - 1) / getMaxElements();
} }
public void setList(List list) { public void setList(List<E> list) {
this.list = list; this.list = list;
} }
@ -142,7 +142,7 @@ public class PaginatedList implements List {
* *
* @return this page of the results * @return this page of the results
*/ */
public List getList() { public List<E> getList() {
return list; return list;
} }
@ -196,7 +196,7 @@ public class PaginatedList implements List {
* *
* @see java.util.List#add(int, java.lang.Object) * @see java.util.List#add(int, java.lang.Object)
*/ */
public void add(int arg0, Object arg1) { public void add(int arg0, E arg1) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@ -211,7 +211,7 @@ public class PaginatedList implements List {
* *
* @see java.util.Collection#add(java.lang.Object) * @see java.util.Collection#add(java.lang.Object)
*/ */
public boolean add(Object arg0) { public boolean add(E arg0) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@ -226,7 +226,7 @@ public class PaginatedList implements List {
* *
* @see java.util.Collection#addAll(java.util.Collection) * @see java.util.Collection#addAll(java.util.Collection)
*/ */
public boolean addAll(Collection arg0) { public boolean addAll(Collection<? extends E> arg0) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@ -242,7 +242,7 @@ public class PaginatedList implements List {
* *
* @see java.util.List#addAll(int, java.util.Collection) * @see java.util.List#addAll(int, java.util.Collection)
*/ */
public boolean addAll(int arg0, Collection arg1) { public boolean addAll(int arg0, Collection<? extends E> arg1) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@ -283,7 +283,7 @@ public class PaginatedList implements List {
* *
* @see java.util.Collection#containsAll(java.util.Collection) * @see java.util.Collection#containsAll(java.util.Collection)
*/ */
public boolean containsAll(Collection arg0) { public boolean containsAll(Collection<?> arg0) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@ -296,7 +296,7 @@ public class PaginatedList implements List {
* *
* @see java.util.List#get(int) * @see java.util.List#get(int)
*/ */
public Object get(int arg0) { public E get(int arg0) {
return list.get(arg0); return list.get(arg0);
} }
@ -315,7 +315,7 @@ public class PaginatedList implements List {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
public Iterator iterator() { public Iterator<E> iterator() {
return new PaginatedListIterator(); return new PaginatedListIterator();
} }
@ -343,7 +343,7 @@ public class PaginatedList implements List {
* *
* @see java.util.List#listIterator() * @see java.util.List#listIterator()
*/ */
public ListIterator listIterator() { public ListIterator<E> listIterator() {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@ -358,7 +358,7 @@ public class PaginatedList implements List {
* *
* @see java.util.List#listIterator(int) * @see java.util.List#listIterator(int)
*/ */
public ListIterator listIterator(int arg0) { public ListIterator<E> listIterator(int arg0) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@ -373,7 +373,7 @@ public class PaginatedList implements List {
* *
* @see java.util.List#remove(int) * @see java.util.List#remove(int)
*/ */
public Object remove(int arg0) { public E remove(int arg0) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@ -418,7 +418,7 @@ public class PaginatedList implements List {
* *
* @see java.util.Collection#retainAll(java.util.Collection) * @see java.util.Collection#retainAll(java.util.Collection)
*/ */
public boolean retainAll(Collection arg0) { public boolean retainAll(Collection<?> arg0) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@ -434,7 +434,7 @@ public class PaginatedList implements List {
* *
* @see java.util.List#set(int, java.lang.Object) * @see java.util.List#set(int, java.lang.Object)
*/ */
public Object set(int arg0, Object arg1) { public E set(int arg0, E arg1) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@ -459,7 +459,7 @@ public class PaginatedList implements List {
* *
* @see java.util.List#subList(int, int) * @see java.util.List#subList(int, int)
*/ */
public List subList(int arg0, int arg1) { public List<E> subList(int arg0, int arg1) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@ -467,7 +467,7 @@ public class PaginatedList implements List {
return list.toArray(); return list.toArray();
} }
public Object[] toArray(Object[] arg0) { public <T> T[] toArray(T[] arg0) {
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug("List size when convert to array " logger.debug("List size when convert to array "
+ list.toArray().length); + list.toArray().length);
@ -475,57 +475,61 @@ public class PaginatedList implements List {
return list.toArray(arg0); return list.toArray(arg0);
} }
private class PaginatedListIterator implements Iterator<E> {
//~ Instance fields ========================================================
//~ Inner Classes ========================================================== private Iterator<E> iterator;
private int i = 0;
private class PaginatedListIterator implements Iterator { /**
private Iterator iterator; * @see java.util.Iterator#hasNext()
private int i = 0; */
public boolean hasNext() {
return i < size();
}
/** /**
* @see java.util.Iterator#hasNext() * This method follows the rules of Iterator.next() except that it returns
*/ * null when requesting an element that it's not in the current page.
public boolean hasNext() { *
return i < size(); * @see java.util.Iterator#next()
} */
public E next() {
if (i == getFirstElement()) {
iterator = getList().iterator();
}
/** if ((i >= getFirstElement())
* This method follows the rules of Iterator.next() except that it && (i < (getFirstElement() + getMaxElements()))) {
* returns null when requesting an element that it's not in the i++;
* current page.
*
* @see java.util.Iterator#next()
*/
public Object next() {
if (i == getFirstElement()) {
iterator = getList().iterator();
}
if ((i >= getFirstElement()) return iterator.next();
&& (i < (getFirstElement() + getMaxElements()))) { }
i++;
return iterator.next(); if (hasNext()) {
} i++;
if (hasNext()) { return null;
i++; } else {
throw new NoSuchElementException();
}
}
return null; /**
} else { * Unsupported operation
throw new NoSuchElementException(); *
} * @throws UnsupportedOperationException
} *
* @see java.util.Iterator#remove()
/** */
* Unsupported operation public void remove() {
* throw new UnsupportedOperationException();
* @throws UnsupportedOperationException }
* }
* @see java.util.Iterator#remove()
*/
public void remove() {
throw new UnsupportedOperationException();
}
}
} }

View File

@ -38,15 +38,14 @@ import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.util.Assert; import org.springframework.util.Assert;
/** /**
* {@link Dao} implementation that uses Hibernate 3 for persistence. * Generics supporting {@link Dao} implementation that uses Hibernate 3 for persistence.
* *
* @author Ben Alex * @author Ben Alex
* @author Matthew Porter * @author Matthew Porter
* @version $Id$ * @version $Id$
*/ */
public class DaoHibernate extends HibernateDaoSupport implements Dao, public class DaoHibernate<E extends PersistableEntity> extends HibernateDaoSupport implements Dao<E>,
EvictionCapable { EvictionCapable {
//~ Instance fields ======================================================== //~ Instance fields ========================================================
@ -63,21 +62,21 @@ public class DaoHibernate extends HibernateDaoSupport implements Dao,
return supportsClass; return supportsClass;
} }
public PersistableEntity create(PersistableEntity value) { public E create(E value) {
Assert.notNull(value); Assert.notNull(value);
getHibernateTemplate().save(value); getHibernateTemplate().save(value);
return readId(value.getInternalId()); return readId(value.getInternalId());
} }
public PersistableEntity createOrUpdate(PersistableEntity value) { public E createOrUpdate(E value) {
Assert.notNull(value); Assert.notNull(value);
getHibernateTemplate().saveOrUpdate(value); getHibernateTemplate().saveOrUpdate(value);
return readId(value.getInternalId()); return readId(value.getInternalId());
} }
public void delete(PersistableEntity value) { public void delete(E value) {
Assert.notNull(value); Assert.notNull(value);
getHibernateTemplate().delete(value); getHibernateTemplate().delete(value);
} }
@ -87,24 +86,24 @@ public class DaoHibernate extends HibernateDaoSupport implements Dao,
getHibernateTemplate().evict(entity); getHibernateTemplate().evict(entity);
} }
public List findAll() { public List<E> findAll() {
return getHibernateTemplate().loadAll(supportsClass); return getHibernateTemplate().loadAll(supportsClass);
} }
public List findId(Collection ids) { public List<E> findId(Collection<Serializable> ids) {
Assert.notNull(ids, "Collection of IDs cannot be null"); Assert.notNull(ids, "Collection of IDs cannot be null");
Assert.notEmpty(ids, "There must be some values in the Collection list"); Assert.notEmpty(ids, "There must be some values in the Collection list");
return (List) getHibernateTemplate().execute(getFindByIdCallback(ids)); return (List) getHibernateTemplate().execute(getFindByIdCallback(ids));
} }
public PersistableEntity readId(Serializable id) { public E readId(Serializable id) {
Assert.notNull(id); Assert.notNull(id);
return (PersistableEntity) getHibernateTemplate().get(supportsClass, id); return (E) getHibernateTemplate().get(supportsClass, id);
} }
public PaginatedList scroll(PersistableEntity value, int firstElement, public PaginatedList<E> scroll(E value, int firstElement,
int maxElements, String orderByAsc) { int maxElements, String orderByAsc) {
Assert.notNull(value); Assert.notNull(value);
Assert.hasText(orderByAsc, Assert.hasText(orderByAsc,
@ -112,10 +111,10 @@ public class DaoHibernate extends HibernateDaoSupport implements Dao,
Assert.isInstanceOf(this.supportsClass, value, "Can only scroll with values this DAO supports"); Assert.isInstanceOf(this.supportsClass, value, "Can only scroll with values this DAO supports");
return (PaginatedList) getHibernateTemplate().execute(getFindByValueCallback( return (PaginatedList) getHibernateTemplate().execute(getFindByValueCallback(
value.getClass(), value, firstElement, maxElements, Order.asc(orderByAsc))); value.getClass(), value, firstElement, maxElements, Order.asc(orderByAsc)));
} }
public PaginatedList scrollWithSubclasses(PersistableEntity value, int firstElement, public PaginatedList<E> scrollWithSubclasses(E value, int firstElement,
int maxElements, String orderByAsc) { int maxElements, String orderByAsc) {
Assert.notNull(value); Assert.notNull(value);
Assert.hasText(orderByAsc, Assert.hasText(orderByAsc,
@ -123,7 +122,7 @@ public class DaoHibernate extends HibernateDaoSupport implements Dao,
Assert.isInstanceOf(this.supportsClass, value, "Can only scroll with values this DAO supports"); Assert.isInstanceOf(this.supportsClass, value, "Can only scroll with values this DAO supports");
return (PaginatedList) getHibernateTemplate().execute(getFindByValueCallback( return (PaginatedList) getHibernateTemplate().execute(getFindByValueCallback(
this.supportsClass, value, firstElement, maxElements, Order.asc(orderByAsc))); this.supportsClass, value, firstElement, maxElements, Order.asc(orderByAsc)));
} }
public boolean supports(Class clazz) { public boolean supports(Class clazz) {
@ -132,7 +131,7 @@ public class DaoHibernate extends HibernateDaoSupport implements Dao,
return this.supportsClass.equals(clazz); return this.supportsClass.equals(clazz);
} }
public PersistableEntity update(PersistableEntity value) { public E update(E value) {
Assert.notNull(value); Assert.notNull(value);
getHibernateTemplate().update(value); getHibernateTemplate().update(value);
@ -167,7 +166,7 @@ public class DaoHibernate extends HibernateDaoSupport implements Dao,
* *
* @return a <code>List</code> containing the matching objects * @return a <code>List</code> containing the matching objects
*/ */
private HibernateCallback getFindByIdCallback(final Collection ids) { private HibernateCallback getFindByIdCallback(final Collection<Serializable> ids) {
return new HibernateCallback() { return new HibernateCallback() {
public Object doInHibernate(Session session) public Object doInHibernate(Session session)
throws HibernateException { throws HibernateException {
@ -263,10 +262,9 @@ public class DaoHibernate extends HibernateDaoSupport implements Dao,
*/ */
int size = criteria.list().size(); int size = criteria.list().size();
List list = criteria.setFirstResult(firstElement) List<E> list = criteria.setFirstResult(firstElement).setMaxResults(count).list();
.setMaxResults(count).list();
return new PaginatedList(list, firstElement, count, size); return new PaginatedList<E>(list, firstElement, count, size);
} }
}; };
} }

View File

@ -0,0 +1,95 @@
/* Copyright 2004, 2005 Acegi Technology Pty Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.sf.acegisecurity.domain.hibernate;
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import org.hibernate.HibernateException;
import org.hibernate.usertype.UserType;
/**
* Java 1.5 <code>enum</code>eration compatible Hibernate 3 <code>UserType</code>.
*
* @author Ben Alex
* @version $Id$
*/
public class EnumUserType<E extends Enum<E>> implements UserType {
private Class<E> clazz = null;
protected EnumUserType(Class<E> c) {
this.clazz = c;
}
private static final int[] SQL_TYPES = {Types.VARCHAR};
public int[] sqlTypes() {
return SQL_TYPES;
}
public Class returnedClass() {
return clazz;
}
public Object nullSafeGet(ResultSet resultSet, String[] names, Object owner) throws HibernateException, SQLException {
String name = resultSet.getString(names[0]);
E result = null;
if (!resultSet.wasNull()) {
result = Enum.valueOf(clazz, name);
}
return result;
}
public void nullSafeSet(PreparedStatement preparedStatement, Object value, int index) throws HibernateException, SQLException {
if (null == value) {
preparedStatement.setNull(index, Types.VARCHAR);
} else {
preparedStatement.setString(index, ((Enum)value).name());
}
}
public Object deepCopy(Object value) throws HibernateException{
return value;
}
public boolean isMutable() {
return false;
}
public Object assemble(Serializable cached, Object owner) throws HibernateException {
return cached;
}
public Serializable disassemble(Object value) throws HibernateException {
return (Serializable)value;
}
public Object replace(Object original, Object target, Object owner) throws HibernateException {
return original;
}
public int hashCode(Object x) throws HibernateException {
return x.hashCode();
}
public boolean equals(Object x, Object y) throws HibernateException {
if (x == y)
return true;
if (null == x || null == y)
return false;
return x.equals(y);
}
}

View File

@ -35,6 +35,7 @@ import org.springframework.util.Assert;
import java.util.Collection; import java.util.Collection;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
@ -90,17 +91,17 @@ public class IntrospectionManagerHibernate implements IntrospectionManager,
Assert.notNull(sessionFactory, "SessionFactory is required"); Assert.notNull(sessionFactory, "SessionFactory is required");
// Eagerly pre-register Validators for all Hibernate metadata-defined classes // Eagerly pre-register Validators for all Hibernate metadata-defined classes
Collection mappedClasses = this.sessionFactory.getAllClassMetadata() Map<String,ClassMetadata> metadataMap = this.sessionFactory.getAllClassMetadata();
.keySet(); Collection<String> mappedClasses = metadataMap.keySet();
for (Iterator iter = mappedClasses.iterator(); iter.hasNext();) { for (Iterator<String> iter = mappedClasses.iterator(); iter.hasNext();) {
String className = (String) iter.next(); String className = iter.next();
this.validationRegistryManager.findValidator(Class.forName( this.validationRegistryManager.findValidator(Class.forName(
className)); className));
} }
} }
public void obtainImmediateChildren(Object parentObject, List allObjects) { public void obtainImmediateChildren(Object parentObject, List<Object> allObjects) {
Assert.notNull(parentObject, Assert.notNull(parentObject,
"Violation of interface contract: parentObject null"); "Violation of interface contract: parentObject null");
Assert.notNull(allObjects, Assert.notNull(allObjects,

View File

@ -15,8 +15,11 @@
package net.sf.acegisecurity.domain.impl; package net.sf.acegisecurity.domain.impl;
import net.sf.acegisecurity.domain.PersistableEntity; import javax.persistence.Column;
import javax.persistence.Transient;
import javax.persistence.Version;
import net.sf.acegisecurity.domain.PersistableEntity;
/** /**
* An abstract implementation of {@link * An abstract implementation of {@link
@ -24,6 +27,8 @@ import net.sf.acegisecurity.domain.PersistableEntity;
* *
* @author Ben Alex * @author Ben Alex
* @version $Id$ * @version $Id$
*
*
*/ */
public abstract class AbstractPersistableEntity extends BusinessObject public abstract class AbstractPersistableEntity extends BusinessObject
implements PersistableEntity { implements PersistableEntity {
@ -45,6 +50,7 @@ public abstract class AbstractPersistableEntity extends BusinessObject
* @return <code>true</code> if the instance has not been persisted, * @return <code>true</code> if the instance has not been persisted,
* <code>false</code> otherwise * <code>false</code> otherwise
*/ */
@Transient
public final boolean isNew() { public final boolean isNew() {
return (getInternalId() == null); return (getInternalId() == null);
} }
@ -60,6 +66,8 @@ public abstract class AbstractPersistableEntity extends BusinessObject
* *
* @return the version * @return the version
*/ */
@Version
@Column(name="version", nullable=false)
public final int getVersion() { public final int getVersion() {
return version; return version;
} }
@ -68,8 +76,6 @@ public abstract class AbstractPersistableEntity extends BusinessObject
* Sets the version numbers. Should only be used by the persistence layer. * Sets the version numbers. Should only be used by the persistence layer.
* *
* @param version the new version number to use * @param version the new version number to use
*
* @hibernate.version type="integer"
*/ */
protected final void setVersion(int version) { protected final void setVersion(int version) {
this.version = version; this.version = version;

View File

@ -17,9 +17,10 @@ package net.sf.acegisecurity.domain.impl;
import java.io.Serializable; import java.io.Serializable;
import javax.persistence.Transient;
/** /**
* A persistable entity that uses an <code>Integer</code> based identity. * A persistable entity that uses a <code>Integer</code> based identity.
* *
* @author Ben Alex * @author Ben Alex
* @version $Id$ * @version $Id$
@ -27,7 +28,7 @@ import java.io.Serializable;
public abstract class PersistableEntityInteger extends AbstractPersistableEntity { public abstract class PersistableEntityInteger extends AbstractPersistableEntity {
//~ Instance fields ======================================================== //~ Instance fields ========================================================
private Integer id; protected Integer id;
//~ Methods ================================================================ //~ Methods ================================================================
@ -47,14 +48,12 @@ public abstract class PersistableEntityInteger extends AbstractPersistableEntity
/** /**
* Obtains the persistence identity of this instance. * Obtains the persistence identity of this instance.
* *
* @return the instance's identity * <p>Marked as abstract to remind users to implement. They'll need to implement
* * so their annotations reflect the correct sequence name.
* @hibernate.id generator-class="native"
*/ */
public Integer getId() { @Transient
return id; public abstract Integer getId();
}
/** /**
* DO NOT USE DIRECTLY. * DO NOT USE DIRECTLY.
@ -72,6 +71,7 @@ public abstract class PersistableEntityInteger extends AbstractPersistableEntity
* *
* @return the instance's identity * @return the instance's identity
*/ */
@Transient
public Serializable getInternalId() { public Serializable getInternalId() {
return this.getId(); return this.getId();
} }

View File

@ -17,6 +17,7 @@ package net.sf.acegisecurity.domain.impl;
import java.io.Serializable; import java.io.Serializable;
import javax.persistence.Transient;
/** /**
* A persistable entity that uses a <code>Long</code> based identity. * A persistable entity that uses a <code>Long</code> based identity.
@ -27,7 +28,7 @@ import java.io.Serializable;
public abstract class PersistableEntityLong extends AbstractPersistableEntity { public abstract class PersistableEntityLong extends AbstractPersistableEntity {
//~ Instance fields ======================================================== //~ Instance fields ========================================================
private Long id; protected Long id;
//~ Methods ================================================================ //~ Methods ================================================================
@ -47,14 +48,12 @@ public abstract class PersistableEntityLong extends AbstractPersistableEntity {
/** /**
* Obtains the persistence identity of this instance. * Obtains the persistence identity of this instance.
* *
* @return the instance's identity * <p>Marked as abstract to remind users to implement. They'll need to implement
* * so their annotations reflect the correct sequence name.
* @hibernate.id generator-class="native"
*/ */
public Long getId() { @Transient
return id; public abstract Long getId();
}
/** /**
* DO NOT USE DIRECTLY. * DO NOT USE DIRECTLY.
@ -72,6 +71,7 @@ public abstract class PersistableEntityLong extends AbstractPersistableEntity {
* *
* @return the instance's identity * @return the instance's identity
*/ */
@Transient
public Serializable getInternalId() { public Serializable getInternalId() {
return this.getId(); return this.getId();
} }

View File

@ -53,7 +53,7 @@ public class CollectionUtils {
* *
* @return * @return
*/ */
public static Set add(Set set, Object object) { public static <E> Set<E> add(Set<E> set, E object) {
set.add(object); set.add(object);
return set; return set;
@ -67,7 +67,7 @@ public class CollectionUtils {
* *
* @return * @return
*/ */
public static List add(List list, Object object) { public static <E> List<E> add(List<E> list, E object) {
list.add(object); list.add(object);
return list; return list;
@ -83,20 +83,20 @@ public class CollectionUtils {
* *
* @throws IllegalArgumentException DOCUMENT ME! * @throws IllegalArgumentException DOCUMENT ME!
*/ */
public static Collection clone(Collection collection) { public static <E> Collection<E> clone(Collection<E> collection) {
if (collection == null) { if (collection == null) {
return null; return null;
} }
Class clazz = collection.getClass(); Class clazz = collection.getClass();
Collection clone = null; Collection<E> clone = null;
if (List.class.isAssignableFrom(clazz)) { if (List.class.isAssignableFrom(clazz)) {
clone = new ArrayList(collection); clone = new ArrayList<E>(collection);
} else if (SortedSet.class.isAssignableFrom(clazz)) { } else if (SortedSet.class.isAssignableFrom(clazz)) {
clone = new TreeSet(collection); clone = new TreeSet<E>(collection);
} else if (Set.class.isAssignableFrom(clazz)) { } else if (Set.class.isAssignableFrom(clazz)) {
clone = new HashSet(collection); clone = new HashSet<E>(collection);
} else { } else {
throw new IllegalArgumentException("Unknown collection class: " throw new IllegalArgumentException("Unknown collection class: "
+ clazz); + clazz);
@ -117,18 +117,18 @@ public class CollectionUtils {
* @throws IllegalArgumentException if the <code>Map</code> implementation * @throws IllegalArgumentException if the <code>Map</code> implementation
* is not supported by this method * is not supported by this method
*/ */
public static Map clone(Map map) { public static <K,V> Map<K,V> clone(Map<K,V> map) {
if (map == null) { if (map == null) {
return null; return null;
} }
Class clazz = map.getClass(); Class clazz = map.getClass();
Map clone = null; Map<K,V> clone = null;
if (SortedMap.class.isAssignableFrom(clazz)) { if (SortedMap.class.isAssignableFrom(clazz)) {
clone = new TreeMap(map); clone = new TreeMap<K,V>(map);
} else if (Map.class.isAssignableFrom(clazz)) { } else if (Map.class.isAssignableFrom(clazz)) {
clone = new HashMap(map); clone = new HashMap<K,V>(map);
} else { } else {
throw new IllegalArgumentException("Unknown map class: " + clazz); throw new IllegalArgumentException("Unknown map class: " + clazz);
} }
@ -144,8 +144,8 @@ public class CollectionUtils {
* *
* @return * @return
*/ */
public static List newList(Object object) { public static <E> List<E> newList(E object) {
return add(new ArrayList(1), object); return add(new ArrayList<E>(1), object);
} }
/** /**
@ -156,7 +156,7 @@ public class CollectionUtils {
* *
* @return * @return
*/ */
public static Set newSet(Object object) { public static <E> Set<E> newSet(E object) {
return add(new HashSet(), object); return add(new HashSet<E>(), object);
} }
} }

View File

@ -51,5 +51,5 @@ public interface IntrospectionManager {
* @param allObjects the list to which this method should append each * @param allObjects the list to which this method should append each
* immediate child (guaranteed to never be <code>null</code>) * immediate child (guaranteed to never be <code>null</code>)
*/ */
public void obtainImmediateChildren(Object parentObject, List allObjects); public void obtainImmediateChildren(Object parentObject, List<Object> allObjects);
} }

View File

@ -47,7 +47,7 @@ public class ValidationAdvisor extends StaticMethodMatcherPointcutAdvisor
implements InitializingBean { implements InitializingBean {
//~ Instance fields ======================================================== //~ Instance fields ========================================================
private Class supportsClass; private Class<? extends Object> supportsClass;
private String[] methods = {"create", "update", "createOrUpdate"}; private String[] methods = {"create", "update", "createOrUpdate"};
//~ Constructors =========================================================== //~ Constructors ===========================================================
@ -72,7 +72,7 @@ public class ValidationAdvisor extends StaticMethodMatcherPointcutAdvisor
return methods; return methods;
} }
public void setSupportsClass(Class clazz) { public void setSupportsClass(Class<? extends Object> clazz) {
this.supportsClass = clazz; this.supportsClass = clazz;
} }

View File

@ -34,7 +34,7 @@ import org.springframework.util.Assert;
* *
* <p> * <p>
* For each method invocation, any argument that is assignable from {@link * For each method invocation, any argument that is assignable from {@link
* #argumentClasses}<b>and</b> is non-<code>null</code> will be passed to the * #argumentClasses} <b>and</b> is non-<code>null</code> will be passed to the
* {@link net.sf.acegisecurity.domain.validation.ValidationManager} for * {@link net.sf.acegisecurity.domain.validation.ValidationManager} for
* processing. * processing.
* </p> * </p>
@ -48,7 +48,7 @@ public class ValidationInterceptor implements MethodInterceptor,
protected final Log logger = LogFactory.getLog(getClass()); protected final Log logger = LogFactory.getLog(getClass());
private ValidationManager validationManager; private ValidationManager validationManager;
private Class[] argumentClasses = {BusinessObject.class, PersistableEntity.class}; private Class<?>[] argumentClasses = {BusinessObject.class, PersistableEntity.class};
//~ Methods ================================================================ //~ Methods ================================================================

View File

@ -116,7 +116,7 @@ public class ValidationManagerImpl implements InitializingBean,
"Cannot validate a null domain object, as unable to getClass()"); "Cannot validate a null domain object, as unable to getClass()");
// Construct a list of objects to be validated and adds self // Construct a list of objects to be validated and adds self
List allObjects = new Vector(); List<Object> allObjects = new Vector<Object>();
allObjects.add(domainObject); allObjects.add(domainObject);
// Add all children (and grandchildren, great-grandchildren etc) // Add all children (and grandchildren, great-grandchildren etc)
@ -128,7 +128,7 @@ public class ValidationManagerImpl implements InitializingBean,
"The list of objects to be validated was empty"); "The list of objects to be validated was empty");
// Process list of objects to be validated by validating each // Process list of objects to be validated by validating each
Iterator iter = allObjects.iterator(); Iterator<Object> iter = allObjects.iterator();
while (iter.hasNext()) { while (iter.hasNext()) {
Object currentDomainObject = iter.next(); Object currentDomainObject = iter.next();
@ -204,7 +204,7 @@ public class ValidationManagerImpl implements InitializingBean,
* @param parentObject the object we wish to locate all children for * @param parentObject the object we wish to locate all children for
* @param allObjects the list to add the located children to * @param allObjects the list to add the located children to
*/ */
private void obtainAllChildren(Object parentObject, List allObjects) { private void obtainAllChildren(Object parentObject, List<Object> allObjects) {
Assert.notNull(parentObject, "Violation of parentObject method contract"); Assert.notNull(parentObject, "Violation of parentObject method contract");
Assert.notNull(allObjects, "Violation of allObjects method contract"); Assert.notNull(allObjects, "Violation of allObjects method contract");
Assert.isTrue(allObjects.contains(parentObject), Assert.isTrue(allObjects.contains(parentObject),
@ -215,7 +215,7 @@ public class ValidationManagerImpl implements InitializingBean,
} }
// Add immediate children of this domain object // Add immediate children of this domain object
List currentChildren = new Vector(); List<Object> currentChildren = new Vector<Object>();
introspectionManager.obtainImmediateChildren(parentObject, introspectionManager.obtainImmediateChildren(parentObject,
currentChildren); currentChildren);
@ -223,7 +223,7 @@ public class ValidationManagerImpl implements InitializingBean,
allObjects.addAll(currentChildren); allObjects.addAll(currentChildren);
// Now iterate the children, adding their children to the object list // Now iterate the children, adding their children to the object list
Iterator childrenIter = currentChildren.iterator(); Iterator<Object> childrenIter = currentChildren.iterator();
while (childrenIter.hasNext()) { while (childrenIter.hasNext()) {
Object childObject = childrenIter.next(); Object childObject = childrenIter.next();

View File

@ -46,7 +46,7 @@ public class ValidationRegistryManagerImpl implements ValidationRegistryManager,
//~ Instance fields ======================================================== //~ Instance fields ========================================================
private ListableBeanFactory bf; private ListableBeanFactory bf;
private Map validatorMap = new HashMap(); private Map<Class,String> validatorMap = new HashMap<Class,String>();
//~ Methods ================================================================ //~ Methods ================================================================
@ -68,11 +68,11 @@ public class ValidationRegistryManagerImpl implements ValidationRegistryManager,
} }
// Attempt to find Validator via introspection // Attempt to find Validator via introspection
Map beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(bf, Validator.class, true, true); Map<String,Validator> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(bf, Validator.class, true, true);
Iterator iter = beans.keySet().iterator(); Iterator<String> iter = beans.keySet().iterator();
while (iter.hasNext()) { while (iter.hasNext()) {
String beanName = (String) iter.next(); String beanName = iter.next();
Validator validator = (Validator) beans.get(beanName); Validator validator = beans.get(beanName);
if (validator.supports(domainClass)) { if (validator.supports(domainClass)) {
this.validatorMap.put(domainClass, beanName); this.validatorMap.put(domainClass, beanName);
return validator; return validator;