[seam-commits] Seam SVN: r12070 - branches/community/Seam_2_2/src/main/org/jboss/seam/persistence.
seam-commits at lists.jboss.org
seam-commits at lists.jboss.org
Tue Feb 23 15:35:57 EST 2010
Author: youngm
Date: 2010-02-23 15:35:56 -0500 (Tue, 23 Feb 2010)
New Revision: 12070
Added:
branches/community/Seam_2_2/src/main/org/jboss/seam/persistence/EntityManagerInvocationHandler.java
branches/community/Seam_2_2/src/main/org/jboss/seam/persistence/HibernateSessionInvocationHandler.java
Modified:
branches/community/Seam_2_2/src/main/org/jboss/seam/persistence/AbstractPersistenceProvider.java
branches/community/Seam_2_2/src/main/org/jboss/seam/persistence/EntityManagerProxy.java
branches/community/Seam_2_2/src/main/org/jboss/seam/persistence/FullTextEntityManagerProxy.java
branches/community/Seam_2_2/src/main/org/jboss/seam/persistence/FullTextHibernateSessionProxy.java
branches/community/Seam_2_2/src/main/org/jboss/seam/persistence/HibernatePersistenceProvider.java
branches/community/Seam_2_2/src/main/org/jboss/seam/persistence/HibernateSessionProxy.java
branches/community/Seam_2_2/src/main/org/jboss/seam/persistence/PersistenceProvider.java
Log:
JBSEAM-4552
Modified: branches/community/Seam_2_2/src/main/org/jboss/seam/persistence/AbstractPersistenceProvider.java
===================================================================
--- branches/community/Seam_2_2/src/main/org/jboss/seam/persistence/AbstractPersistenceProvider.java 2010-02-23 19:52:51 UTC (rev 12069)
+++ branches/community/Seam_2_2/src/main/org/jboss/seam/persistence/AbstractPersistenceProvider.java 2010-02-23 20:35:56 UTC (rev 12070)
@@ -1,6 +1,7 @@
package org.jboss.seam.persistence;
import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
import java.util.Date;
import javax.persistence.EntityManager;
@@ -110,7 +111,9 @@
*/
public EntityManager proxyEntityManager(EntityManager entityManager)
{
- return new EntityManagerProxy(entityManager);
+ return (EntityManager) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
+ new Class[] { EntityManagerProxy.class },
+ new EntityManagerInvocationHandler(entityManager));
}
/**
Added: branches/community/Seam_2_2/src/main/org/jboss/seam/persistence/EntityManagerInvocationHandler.java
===================================================================
--- branches/community/Seam_2_2/src/main/org/jboss/seam/persistence/EntityManagerInvocationHandler.java (rev 0)
+++ branches/community/Seam_2_2/src/main/org/jboss/seam/persistence/EntityManagerInvocationHandler.java 2010-02-23 20:35:56 UTC (rev 12070)
@@ -0,0 +1,93 @@
+package org.jboss.seam.persistence;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+import javax.persistence.EntityManager;
+import javax.persistence.Query;
+
+import org.jboss.seam.security.permission.PermissionManager;
+
+/**
+ * InvocationHandler that Proxies the EntityManager, and implements EL
+ * interpolation in JPA-QL
+ *
+ * @author Gavin King
+ * @author Mike Youngstrom
+ */
+public class EntityManagerInvocationHandler implements InvocationHandler
+{
+
+ private EntityManager delegate;
+
+ public EntityManagerInvocationHandler(EntityManager delegate)
+ {
+ this.delegate = delegate;
+ }
+
+ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
+ {
+ try
+ {
+ if ("createQuery".equals(method.getName()) && method.getParameterTypes().length > 0 && method.getParameterTypes()[0].equals(String.class))
+ {
+ return handleCreateQueryWithString(method, args);
+ }
+ if ("getDelegate".equals(method.getName()))
+ {
+ return handleGetDelegate(method, args);
+ }
+ if ("remove".equals(method.getName()) && method.getParameterTypes().length > 0)
+ {
+ return handleRemove(method, args);
+ }
+ return method.invoke(delegate, args);
+ }
+ catch (InvocationTargetException e)
+ {
+ throw e.getTargetException();
+ }
+ }
+
+ protected Object handleCreateQueryWithString(Method method, Object[] args) throws Throwable
+ {
+ if (args[0] == null)
+ {
+ return method.invoke(delegate, args);
+ }
+ String ejbql = (String) args[0];
+ if (ejbql.indexOf('#') > 0)
+ {
+ QueryParser qp = new QueryParser(ejbql);
+ Object[] newArgs = args.clone();
+ newArgs[0] = qp.getEjbql();
+ Query query = (Query) method.invoke(delegate, newArgs);
+ for (int i = 0; i < qp.getParameterValueBindings().size(); i++)
+ {
+ query.setParameter(QueryParser.getParameterName(i), qp.getParameterValueBindings().get(i).getValue());
+ }
+ return query;
+ }
+ else
+ {
+ return method.invoke(delegate, args);
+ }
+ }
+
+ protected Object handleGetDelegate(Method method, Object[] args) throws Throwable
+ {
+ return PersistenceProvider.instance().proxyDelegate(method.invoke(delegate, args));
+ }
+
+ protected Object handleRemove(Method method, Object[] args) throws Throwable
+ {
+ if (args.length == 0)
+ {
+ return method.invoke(delegate, args);
+ }
+ Object result = method.invoke(delegate, args);
+ PermissionManager.instance().clearPermissions(args[0]);
+ return result;
+ }
+}
Property changes on: branches/community/Seam_2_2/src/main/org/jboss/seam/persistence/EntityManagerInvocationHandler.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Modified: branches/community/Seam_2_2/src/main/org/jboss/seam/persistence/EntityManagerProxy.java
===================================================================
--- branches/community/Seam_2_2/src/main/org/jboss/seam/persistence/EntityManagerProxy.java 2010-02-23 19:52:51 UTC (rev 12069)
+++ branches/community/Seam_2_2/src/main/org/jboss/seam/persistence/EntityManagerProxy.java 2010-02-23 20:35:56 UTC (rev 12070)
@@ -3,13 +3,7 @@
import java.io.Serializable;
import javax.persistence.EntityManager;
-import javax.persistence.EntityTransaction;
-import javax.persistence.FlushModeType;
-import javax.persistence.LockModeType;
-import javax.persistence.Query;
-import org.jboss.seam.security.permission.PermissionManager;
-
/**
* Proxies the EntityManager, and implements EL interpolation
* in JPA-QL
@@ -17,139 +11,6 @@
* @author Gavin King
*
*/
-public class EntityManagerProxy implements EntityManager, Serializable
+public interface EntityManagerProxy extends EntityManager, Serializable
{
- private EntityManager delegate;
-
- public EntityManagerProxy(EntityManager entityManager)
- {
- delegate = entityManager;
- }
-
- public void clear()
- {
- delegate.clear();
- }
-
- public void close()
- {
- delegate.close();
- }
-
- public boolean contains(Object entity)
- {
- return delegate.contains(entity);
- }
-
- public Query createNamedQuery(String name)
- {
- return delegate.createNamedQuery(name);
- }
-
- public Query createNativeQuery(String sql, Class clazz)
- {
- return delegate.createNativeQuery(sql, clazz);
- }
-
- public Query createNativeQuery(String sql, String lang)
- {
- return delegate.createNativeQuery(sql, lang);
- }
-
- public Query createNativeQuery(String sql)
- {
- return delegate.createNativeQuery(sql);
- }
-
- public Query createQuery(String ejbql)
- {
- if ( ejbql.indexOf('#')>0 )
- {
- QueryParser qp = new QueryParser(ejbql);
- Query query = delegate.createQuery( qp.getEjbql() );
- for (int i=0; i<qp.getParameterValueBindings().size(); i++)
- {
- query.setParameter(
- QueryParser.getParameterName(i),
- qp.getParameterValueBindings().get(i).getValue()
- );
- }
- return query;
- }
- else
- {
- return delegate.createQuery(ejbql);
- }
- }
-
- public <T> T find(Class<T> clazz, Object id)
- {
- return delegate.find(clazz, id);
- }
-
- public void flush()
- {
- delegate.flush();
- }
-
- public Object getDelegate()
- {
- return PersistenceProvider.instance().proxyDelegate( delegate.getDelegate() );
- }
-
- public FlushModeType getFlushMode()
- {
- return delegate.getFlushMode();
- }
-
- public <T> T getReference(Class<T> clazz, Object id)
- {
- return delegate.getReference(clazz, id);
- }
-
- public EntityTransaction getTransaction()
- {
- return delegate.getTransaction();
- }
-
- public boolean isOpen()
- {
- return delegate.isOpen();
- }
-
- public void joinTransaction()
- {
- delegate.joinTransaction();
- }
-
- public void lock(Object entity, LockModeType lm)
- {
- delegate.lock(entity, lm);
- }
-
- public <T> T merge(T entity)
- {
- return delegate.merge(entity);
- }
-
- public void persist(Object entity)
- {
- delegate.persist(entity);
- }
-
- public void refresh(Object entity)
- {
- delegate.refresh(entity);
- }
-
- public void remove(Object entity)
- {
- delegate.remove(entity);
- PermissionManager.instance().clearPermissions(entity);
- }
-
- public void setFlushMode(FlushModeType fm)
- {
- delegate.setFlushMode(fm);
- }
}
Modified: branches/community/Seam_2_2/src/main/org/jboss/seam/persistence/FullTextEntityManagerProxy.java
===================================================================
--- branches/community/Seam_2_2/src/main/org/jboss/seam/persistence/FullTextEntityManagerProxy.java 2010-02-23 19:52:51 UTC (rev 12069)
+++ branches/community/Seam_2_2/src/main/org/jboss/seam/persistence/FullTextEntityManagerProxy.java 2010-02-23 20:35:56 UTC (rev 12070)
@@ -1,57 +1,17 @@
//$Id$
package org.jboss.seam.persistence;
-import java.io.Serializable;
-
-import org.apache.lucene.search.Query;
-import org.hibernate.search.SearchFactory;
import org.hibernate.search.jpa.FullTextEntityManager;
-import org.hibernate.search.jpa.FullTextQuery;
/**
- * Wrap a FullTextEntityManager
+ * Marker Interface here to show that a given EntityManager is doing EL
+ * manipulation and for backwards compatibility with previous non proxy
+ * solution.
*
* @author Emmanuel Bernard
* @author Sanne Grinovero
+ * @author Mike Youngstrom
*/
-public class FullTextEntityManagerProxy extends EntityManagerProxy implements FullTextEntityManager
+public interface FullTextEntityManagerProxy extends EntityManagerProxy, FullTextEntityManager
{
-
- private final FullTextEntityManager fullTextEntityManager;
-
- public FullTextEntityManagerProxy(FullTextEntityManager entityManager)
- {
- super(entityManager);
- this.fullTextEntityManager = entityManager;
- }
-
- public FullTextQuery createFullTextQuery(Query query, Class<?>... classes)
- {
- return fullTextEntityManager.createFullTextQuery(query, classes);
- }
-
- public void flushToIndexes()
- {
- fullTextEntityManager.flushToIndexes();
- }
-
- public <T> void index(T entity)
- {
- fullTextEntityManager.index(entity);
- }
-
- public SearchFactory getSearchFactory()
- {
- return fullTextEntityManager.getSearchFactory();
- }
-
- public <T> void purge(Class<T> aClass, Serializable id)
- {
- fullTextEntityManager.purge(aClass, id);
- }
-
- public <T> void purgeAll(Class<T> entityType)
- {
- fullTextEntityManager.purgeAll(entityType);
- }
}
Modified: branches/community/Seam_2_2/src/main/org/jboss/seam/persistence/FullTextHibernateSessionProxy.java
===================================================================
--- branches/community/Seam_2_2/src/main/org/jboss/seam/persistence/FullTextHibernateSessionProxy.java 2010-02-23 19:52:51 UTC (rev 12069)
+++ branches/community/Seam_2_2/src/main/org/jboss/seam/persistence/FullTextHibernateSessionProxy.java 2010-02-23 20:35:56 UTC (rev 12070)
@@ -1,179 +1,16 @@
package org.jboss.seam.persistence;
-import java.io.Serializable;
-import java.util.Collection;
-import java.util.Iterator;
-import java.util.List;
-
-import org.hibernate.HibernateException;
-import org.hibernate.Query;
-import org.hibernate.jdbc.Work;
-import org.hibernate.search.FullTextQuery;
import org.hibernate.search.FullTextSession;
-import org.hibernate.search.SearchFactory;
-import org.hibernate.type.Type;
/**
- * Wraps a Hibernate Search session
+ * Marker Interface here to show that a given EntityManager is doing EL
+ * manipulation and for backwards compatibility with previous non proxy
+ * solution.
*
* @author Gavin King
* @author Sanne Grinovero
+ * @author Mike Youngstrom
*/
- at SuppressWarnings("deprecation")
-public class FullTextHibernateSessionProxy extends HibernateSessionProxy implements FullTextSession
+public interface FullTextHibernateSessionProxy extends HibernateSessionProxy, FullTextSession
{
- private FullTextSession fullTextSession;
-
- public FullTextHibernateSessionProxy(FullTextSession fullTextSession)
- {
- super(fullTextSession);
- this.fullTextSession = fullTextSession;
- }
-
- public <T> void index(T entity)
- {
- fullTextSession.index(entity);
- }
-
- public FullTextQuery createFullTextQuery(org.apache.lucene.search.Query ftQuery, Class<?>... entityTypes)
- {
- return fullTextSession.createFullTextQuery(ftQuery, entityTypes);
- }
-
- public Query createSQLQuery(String arg0, String arg1, Class arg2)
- {
- return fullTextSession.createSQLQuery(arg0, arg1, arg2);
- }
-
- public Query createSQLQuery(String arg0, String[] arg1, Class[] arg2)
- {
- return fullTextSession.createSQLQuery(arg0, arg1, arg2);
- }
-
- public int delete(String arg0, Object arg1, Type arg2) throws HibernateException
- {
- return fullTextSession.delete(arg0, arg1, arg2);
- }
-
- public int delete(String arg0, Object[] arg1, Type[] arg2) throws HibernateException
- {
- return fullTextSession.delete(arg0, arg1, arg2);
- }
-
- public int delete(String arg0) throws HibernateException
- {
- return fullTextSession.delete(arg0);
- }
-
- public void doWork(Work work) throws HibernateException
- {
- fullTextSession.doWork(work);
- }
-
- public void flushToIndexes()
- {
- fullTextSession.flushToIndexes();
- }
-
- public Collection filter(Object arg0, String arg1, Object arg2, Type arg3) throws HibernateException
- {
- return fullTextSession.filter(arg0, arg1, arg2, arg3);
- }
-
- public Collection filter(Object arg0, String arg1, Object[] arg2, Type[] arg3) throws HibernateException
- {
- return fullTextSession.filter(arg0, arg1, arg2, arg3);
- }
-
- public Collection filter(Object arg0, String arg1) throws HibernateException
- {
- return fullTextSession.filter(arg0, arg1);
- }
-
- public List find(String arg0, Object arg1, Type arg2) throws HibernateException
- {
- return fullTextSession.find(arg0, arg1, arg2);
- }
-
- public List find(String arg0, Object[] arg1, Type[] arg2) throws HibernateException
- {
- return fullTextSession.find(arg0, arg1, arg2);
- }
-
- public List find(String arg0) throws HibernateException
- {
- return fullTextSession.find(arg0);
- }
-
-
- public SearchFactory getSearchFactory()
- {
- return fullTextSession.getSearchFactory();
- }
-
- public <T> void purge(Class<T> entityType, Serializable id)
- {
- fullTextSession.purge(entityType, id);
- }
-
- public <T> void purgeAll(Class<T> aClass)
- {
- fullTextSession.purgeAll(aClass);
- }
-
- public Iterator iterate(String arg0, Object arg1, Type arg2) throws HibernateException
- {
- return fullTextSession.iterate(arg0, arg1, arg2);
- }
-
- public Iterator iterate(String arg0, Object[] arg1, Type[] arg2) throws HibernateException
- {
- return fullTextSession.iterate(arg0, arg1, arg2);
- }
-
- public Iterator iterate(String arg0) throws HibernateException
- {
- return fullTextSession.iterate(arg0);
- }
-
- public void save(Object arg0, Serializable arg1) throws HibernateException
- {
- fullTextSession.save(arg0, arg1);
- }
-
- public void save(String arg0, Object arg1, Serializable arg2) throws HibernateException
- {
- fullTextSession.save(arg0, arg1, arg2);
- }
-
- public Object saveOrUpdateCopy(Object arg0, Serializable arg1) throws HibernateException
- {
- return fullTextSession.saveOrUpdateCopy(arg0, arg1);
- }
-
- public Object saveOrUpdateCopy(Object arg0) throws HibernateException
- {
- return fullTextSession.saveOrUpdateCopy(arg0);
- }
-
- public Object saveOrUpdateCopy(String arg0, Object arg1, Serializable arg2) throws HibernateException
- {
- return fullTextSession.saveOrUpdateCopy(arg0, arg1, arg2);
- }
-
- public Object saveOrUpdateCopy(String arg0, Object arg1) throws HibernateException
- {
- return fullTextSession.saveOrUpdateCopy(arg0, arg1);
- }
-
- public void update(Object arg0, Serializable arg1) throws HibernateException
- {
- fullTextSession.update(arg0, arg1);
- }
-
- public void update(String arg0, Object arg1, Serializable arg2) throws HibernateException
- {
- fullTextSession.update(arg0, arg1, arg2);
- }
-
}
Modified: branches/community/Seam_2_2/src/main/org/jboss/seam/persistence/HibernatePersistenceProvider.java
===================================================================
--- branches/community/Seam_2_2/src/main/org/jboss/seam/persistence/HibernatePersistenceProvider.java 2010-02-23 19:52:51 UTC (rev 12069)
+++ branches/community/Seam_2_2/src/main/org/jboss/seam/persistence/HibernatePersistenceProvider.java 2010-02-23 20:35:56 UTC (rev 12070)
@@ -1,9 +1,9 @@
package org.jboss.seam.persistence;
import static org.jboss.seam.annotations.Install.FRAMEWORK;
-import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
import java.util.Collection;
import java.util.Map;
@@ -47,9 +47,9 @@
{
private static Log log = Logging.getLog(HibernatePersistenceProvider.class);
- private static Constructor FULL_TEXT_SESSION_PROXY_CONSTRUCTOR;
+ private static Class FULL_TEXT_SESSION_PROXY_CLASS;
private static Method FULL_TEXT_SESSION_CONSTRUCTOR;
- private static Constructor FULL_TEXT_ENTITYMANAGER_PROXY_CONSTRUCTOR;
+ private static Class FULL_TEXT_ENTITYMANAGER_PROXY_CLASS;
private static Method FULL_TEXT_ENTITYMANAGER_CONSTRUCTOR;
static
{
@@ -74,9 +74,7 @@
log.debug("org.hibernate.search.Search.getFullTextSession(Session) not found, trying deprecated method name createFullTextSession");
FULL_TEXT_SESSION_CONSTRUCTOR = searchClass.getDeclaredMethod("createFullTextSession", Session.class);
}
- Class fullTextSessionProxyClass = Class.forName("org.jboss.seam.persistence.FullTextHibernateSessionProxy");
- Class fullTextSessionClass = Class.forName("org.hibernate.search.FullTextSession");
- FULL_TEXT_SESSION_PROXY_CONSTRUCTOR = fullTextSessionProxyClass.getDeclaredConstructor(fullTextSessionClass);
+ FULL_TEXT_SESSION_PROXY_CLASS = Class.forName("org.jboss.seam.persistence.FullTextHibernateSessionProxy");
Class jpaSearchClass = Class.forName("org.hibernate.search.jpa.Search");
try {
FULL_TEXT_ENTITYMANAGER_CONSTRUCTOR = jpaSearchClass.getDeclaredMethod("getFullTextEntityManager", EntityManager.class);
@@ -85,9 +83,7 @@
log.debug("org.hibernate.search.jpa.getFullTextSession(EntityManager) not found, trying deprecated method name createFullTextEntityManager");
FULL_TEXT_ENTITYMANAGER_CONSTRUCTOR = jpaSearchClass.getDeclaredMethod("createFullTextEntityManager", EntityManager.class);
}
- Class fullTextEntityManagerProxyClass = Class.forName("org.jboss.seam.persistence.FullTextEntityManagerProxy");
- Class fullTextEntityManagerClass = Class.forName("org.hibernate.search.jpa.FullTextEntityManager");
- FULL_TEXT_ENTITYMANAGER_PROXY_CONSTRUCTOR = fullTextEntityManagerProxyClass.getDeclaredConstructor(fullTextEntityManagerClass);
+ FULL_TEXT_ENTITYMANAGER_PROXY_CLASS = Class.forName("org.jboss.seam.persistence.FullTextEntityManagerProxy");
log.debug("Hibernate Search is available :-)");
}
}
@@ -111,18 +107,24 @@
*/
static Session proxySession(Session session)
{
- if (FULL_TEXT_SESSION_PROXY_CONSTRUCTOR==null)
+ if (FULL_TEXT_SESSION_PROXY_CLASS==null)
{
- return new HibernateSessionProxy(session);
+ return (Session) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
+ new Class[] { HibernateSessionProxy.class },
+ new HibernateSessionInvocationHandler(session));
}
else
{
try {
- return (Session) FULL_TEXT_SESSION_PROXY_CONSTRUCTOR.newInstance( FULL_TEXT_SESSION_CONSTRUCTOR.invoke(null, session) );
+ return (Session) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
+ new Class[] { FULL_TEXT_SESSION_PROXY_CLASS },
+ new HibernateSessionInvocationHandler((Session) FULL_TEXT_SESSION_CONSTRUCTOR.invoke(null, session)));
}
catch(Exception e) {
log.warn("Unable to wrap into a FullTextSessionProxy, regular SessionProxy returned", e);
- return new HibernateSessionProxy(session);
+ return (Session) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
+ new Class[] { HibernateSessionProxy.class },
+ new HibernateSessionInvocationHandler(session));
}
}
}
@@ -292,7 +294,7 @@
@Override
public EntityManager proxyEntityManager(EntityManager entityManager)
{
- if (FULL_TEXT_ENTITYMANAGER_PROXY_CONSTRUCTOR==null)
+ if (FULL_TEXT_ENTITYMANAGER_PROXY_CLASS==null)
{
return super.proxyEntityManager(entityManager);
}
@@ -300,11 +302,12 @@
{
try
{
- return (EntityManager) FULL_TEXT_ENTITYMANAGER_PROXY_CONSTRUCTOR.newInstance(
- FULL_TEXT_ENTITYMANAGER_CONSTRUCTOR.invoke(null, super.proxyEntityManager( entityManager) )
- //TODO is double wrapping the right choice? ie to wrap the session?
- );
- }
+ return (EntityManager) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
+ new Class[] { FULL_TEXT_ENTITYMANAGER_PROXY_CLASS },
+ new EntityManagerInvocationHandler(
+ (EntityManager) FULL_TEXT_ENTITYMANAGER_CONSTRUCTOR.invoke(null,
+ super.proxyEntityManager(entityManager))));
+ }
catch (Exception e)
{
//throw new RuntimeException("could not proxy FullTextEntityManager", e);
Added: branches/community/Seam_2_2/src/main/org/jboss/seam/persistence/HibernateSessionInvocationHandler.java
===================================================================
--- branches/community/Seam_2_2/src/main/org/jboss/seam/persistence/HibernateSessionInvocationHandler.java (rev 0)
+++ branches/community/Seam_2_2/src/main/org/jboss/seam/persistence/HibernateSessionInvocationHandler.java 2010-02-23 20:35:56 UTC (rev 12070)
@@ -0,0 +1,79 @@
+package org.jboss.seam.persistence;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+import org.hibernate.Query;
+import org.hibernate.Session;
+
+/**
+ * InvocationHandler that proxies the Session, and implements EL interpolation
+ * in HQL. Needs to implement SessionImplementor because DetachedCriteria casts
+ * the Session to SessionImplementor.
+ *
+ * @author Gavin King
+ * @author Emmanuel Bernard
+ * @author Mike Youngstrom
+ *
+ */
+public class HibernateSessionInvocationHandler implements InvocationHandler
+{
+
+ private Session delegate;
+
+ public HibernateSessionInvocationHandler(Session delegate)
+ {
+ this.delegate = delegate;
+ }
+
+ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
+ {
+ try
+ {
+ if ("createQuery".equals(method.getName()) && method.getParameterTypes().length > 0 && method.getParameterTypes()[0].equals(String.class))
+ {
+ return handleCreateQueryWithString(method, args);
+ }
+ if ("reconnect".equals(method.getName()) && method.getParameterTypes().length == 0)
+ {
+ return handleReconnectNoArg(method);
+ }
+ return method.invoke(delegate, args);
+ }
+ catch (InvocationTargetException e)
+ {
+ throw e.getTargetException();
+ }
+ }
+
+ protected Object handleCreateQueryWithString(Method method, Object[] args) throws Throwable
+ {
+ if (args[0] == null)
+ {
+ return method.invoke(delegate, args);
+ }
+ String ejbql = (String) args[0];
+ if (ejbql.indexOf('#') > 0)
+ {
+ QueryParser qp = new QueryParser(ejbql);
+ Object[] newArgs = args.clone();
+ newArgs[0] = qp.getEjbql();
+ Query query = (Query) method.invoke(delegate, newArgs);
+ for (int i = 0; i < qp.getParameterValueBindings().size(); i++)
+ {
+ query.setParameter(QueryParser.getParameterName(i), qp.getParameterValueBindings().get(i).getValue());
+ }
+ return query;
+ }
+ else
+ {
+ return method.invoke(delegate, args);
+ }
+ }
+
+ protected Object handleReconnectNoArg(Method method) throws Throwable
+ {
+ throw new UnsupportedOperationException("deprecated");
+ }
+}
Property changes on: branches/community/Seam_2_2/src/main/org/jboss/seam/persistence/HibernateSessionInvocationHandler.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Modified: branches/community/Seam_2_2/src/main/org/jboss/seam/persistence/HibernateSessionProxy.java
===================================================================
--- branches/community/Seam_2_2/src/main/org/jboss/seam/persistence/HibernateSessionProxy.java 2010-02-23 19:52:51 UTC (rev 12069)
+++ branches/community/Seam_2_2/src/main/org/jboss/seam/persistence/HibernateSessionProxy.java 2010-02-23 20:35:56 UTC (rev 12070)
@@ -1,675 +1,20 @@
package org.jboss.seam.persistence;
-import java.io.Serializable;
-import java.sql.Connection;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
-import org.hibernate.CacheMode;
-import org.hibernate.Criteria;
-import org.hibernate.EntityMode;
-import org.hibernate.Filter;
-import org.hibernate.FlushMode;
-import org.hibernate.HibernateException;
-import org.hibernate.Interceptor;
-import org.hibernate.LockMode;
-import org.hibernate.Query;
-import org.hibernate.ReplicationMode;
-import org.hibernate.SQLQuery;
-import org.hibernate.ScrollMode;
-import org.hibernate.ScrollableResults;
import org.hibernate.Session;
-import org.hibernate.SessionFactory;
-import org.hibernate.Transaction;
-import org.hibernate.collection.PersistentCollection;
-import org.hibernate.engine.EntityKey;
-import org.hibernate.engine.PersistenceContext;
-import org.hibernate.engine.QueryParameters;
-import org.hibernate.engine.SessionFactoryImplementor;
import org.hibernate.engine.SessionImplementor;
-import org.hibernate.engine.ActionQueue;
-import org.hibernate.engine.EntityEntry;
-import org.hibernate.engine.query.sql.NativeSQLQuerySpecification;
-import org.hibernate.event.EventListeners;
import org.hibernate.event.EventSource;
-import org.hibernate.impl.CriteriaImpl;
-import org.hibernate.jdbc.Batcher;
-import org.hibernate.jdbc.JDBCContext;
-import org.hibernate.jdbc.Work;
-import org.hibernate.loader.custom.CustomQuery;
-import org.hibernate.persister.entity.EntityPersister;
-import org.hibernate.stat.SessionStatistics;
-import org.hibernate.type.Type;
/**
- * Proxies the Session, and implements EL interpolation
- * in HQL. Needs to implement SessionImplementor because
- * DetachedCriteria casts the Session to SessionImplementor.
+ * Marker interface that signifies a proxy is using the
+ * HibernateSessionInvocationHandler. Also here for backwards compatibility with
+ * previous HibernateSessionProxy.
*
* @author Gavin King
- * @author Emmanuel Bernard
- * FIXME: EventSource should not really be there, remove once HSearch is fixed
- *
+ * @author Emmanuel Bernard FIXME: EventSource should not really be there,
+ * remove once HSearch is fixed
+ * @author Mike Youngstrom
+ *
*/
-public class HibernateSessionProxy implements Session, SessionImplementor, EventSource
+public interface HibernateSessionProxy extends Session, SessionImplementor, EventSource
{
- private Session delegate;
-
- /**
- * Don't use that constructor directly, use HibernatePersistenceProvider.proxySession()
- */
- public HibernateSessionProxy(Session session)
- {
- delegate = session;
- }
-
- public Transaction beginTransaction() throws HibernateException
- {
- return delegate.beginTransaction();
- }
-
- public void cancelQuery() throws HibernateException
- {
- delegate.cancelQuery();
- }
-
- public void clear()
- {
- delegate.clear();
- }
-
- public Connection close() throws HibernateException
- {
- return delegate.close();
- }
-
- @SuppressWarnings("deprecation")
- public Connection connection() throws HibernateException
- {
- return delegate.connection();
- }
-
- public boolean contains(Object arg0)
- {
- return delegate.contains(arg0);
- }
-
- public Criteria createCriteria(Class arg0, String arg1)
- {
- return delegate.createCriteria(arg0, arg1);
- }
-
- public Criteria createCriteria(Class arg0)
- {
- return delegate.createCriteria(arg0);
- }
-
- public Criteria createCriteria(String arg0, String arg1)
- {
- return delegate.createCriteria(arg0, arg1);
- }
-
- public Criteria createCriteria(String arg0)
- {
- return delegate.createCriteria(arg0);
- }
-
- public Query createFilter(Object arg0, String arg1) throws HibernateException
- {
- return delegate.createFilter(arg0, arg1);
- }
-
- public Query createQuery(String hql) throws HibernateException
- {
- if ( hql.indexOf('#')>0 )
- {
- QueryParser qp = new QueryParser(hql);
- Query query = delegate.createQuery( qp.getEjbql() );
- for (int i=0; i<qp.getParameterValueBindings().size(); i++)
- {
- query.setParameter(
- QueryParser.getParameterName(i),
- qp.getParameterValueBindings().get(i).getValue()
- );
- }
- return query;
- }
- else
- {
- return delegate.createQuery(hql);
- }
- }
-
- public SQLQuery createSQLQuery(String arg0) throws HibernateException
- {
- return delegate.createSQLQuery(arg0);
- }
-
- public void delete(Object arg0) throws HibernateException
- {
- delegate.delete(arg0);
- }
-
- public void delete(String arg0, Object arg1) throws HibernateException
- {
- delegate.delete(arg0, arg1);
- }
-
- public void disableFilter(String arg0)
- {
- delegate.disableFilter(arg0);
- }
-
- public Connection disconnect() throws HibernateException
- {
- return delegate.disconnect();
- }
-
- public void doWork(Work work) throws HibernateException
- {
- delegate.doWork(work);
- }
-
- public Filter enableFilter(String arg0)
- {
- return delegate.enableFilter(arg0);
- }
-
- public void evict(Object arg0) throws HibernateException
- {
- delegate.evict(arg0);
- }
-
- public void flush() throws HibernateException
- {
- delegate.flush();
- }
-
- public Object get(Class arg0, Serializable arg1, LockMode arg2) throws HibernateException
- {
- return delegate.get(arg0, arg1, arg2);
- }
-
- public Object get(Class arg0, Serializable arg1) throws HibernateException
- {
- return delegate.get(arg0, arg1);
- }
-
- public Object get(String arg0, Serializable arg1, LockMode arg2) throws HibernateException
- {
- return delegate.get(arg0, arg1, arg2);
- }
-
- public Object get(String arg0, Serializable arg1) throws HibernateException
- {
- return delegate.get(arg0, arg1);
- }
-
- public CacheMode getCacheMode()
- {
- return delegate.getCacheMode();
- }
-
- public LockMode getCurrentLockMode(Object arg0) throws HibernateException
- {
- return delegate.getCurrentLockMode(arg0);
- }
-
- public Filter getEnabledFilter(String arg0)
- {
- return delegate.getEnabledFilter(arg0);
- }
-
- public EntityMode getEntityMode()
- {
- return delegate.getEntityMode();
- }
-
- public String getEntityName(Object arg0) throws HibernateException
- {
- return delegate.getEntityName(arg0);
- }
-
- public FlushMode getFlushMode()
- {
- return delegate.getFlushMode();
- }
-
- public Serializable getIdentifier(Object arg0) throws HibernateException
- {
- return delegate.getIdentifier(arg0);
- }
-
- public Query getNamedQuery(String arg0) throws HibernateException
- {
- return delegate.getNamedQuery(arg0);
- }
-
- public Session getSession(EntityMode arg0)
- {
- return delegate.getSession(arg0);
- }
-
- public SessionFactory getSessionFactory()
- {
- return delegate.getSessionFactory();
- }
-
- public SessionStatistics getStatistics()
- {
- return delegate.getStatistics();
- }
-
- public Transaction getTransaction()
- {
- return delegate.getTransaction();
- }
-
- public boolean isConnected()
- {
- return delegate.isConnected();
- }
-
- public boolean isDirty() throws HibernateException
- {
- return delegate.isDirty();
- }
-
- public boolean isOpen()
- {
- return delegate.isOpen();
- }
-
- public Object load(Class arg0, Serializable arg1, LockMode arg2) throws HibernateException
- {
- return delegate.load(arg0, arg1, arg2);
- }
-
- public Object load(Class arg0, Serializable arg1) throws HibernateException
- {
- return delegate.load(arg0, arg1);
- }
-
- public void load(Object arg0, Serializable arg1) throws HibernateException
- {
- delegate.load(arg0, arg1);
- }
-
- public Object load(String arg0, Serializable arg1, LockMode arg2) throws HibernateException
- {
- return delegate.load(arg0, arg1, arg2);
- }
-
- public Object load(String arg0, Serializable arg1) throws HibernateException
- {
- return delegate.load(arg0, arg1);
- }
-
- public void lock(Object arg0, LockMode arg1) throws HibernateException
- {
- delegate.lock(arg0, arg1);
- }
-
- public void lock(String arg0, Object arg1, LockMode arg2) throws HibernateException
- {
- delegate.lock(arg0, arg1, arg2);
- }
-
- public Object merge(Object arg0) throws HibernateException
- {
- return delegate.merge(arg0);
- }
-
- public Object merge(String arg0, Object arg1) throws HibernateException
- {
- return delegate.merge(arg0, arg1);
- }
-
- public void persist(Object arg0) throws HibernateException
- {
- delegate.persist(arg0);
- }
-
- public void persist(String arg0, Object arg1) throws HibernateException
- {
- delegate.persist(arg0, arg1);
- }
-
- public void reconnect() throws HibernateException
- {
- throw new UnsupportedOperationException("deprecated");
- }
-
- public void reconnect(Connection arg0) throws HibernateException
- {
- delegate.reconnect(arg0);
- }
-
- public void refresh(Object arg0, LockMode arg1) throws HibernateException
- {
- delegate.refresh(arg0, arg1);
- }
-
- public void refresh(Object arg0) throws HibernateException
- {
- delegate.refresh(arg0);
- }
-
- public void replicate(Object arg0, ReplicationMode arg1) throws HibernateException
- {
- delegate.replicate(arg0, arg1);
- }
-
- public void replicate(String arg0, Object arg1, ReplicationMode arg2) throws HibernateException
- {
- delegate.replicate(arg0, arg1, arg2);
- }
-
- public Serializable save(Object arg0) throws HibernateException
- {
- return delegate.save(arg0);
- }
-
- public Serializable save(String arg0, Object arg1) throws HibernateException
- {
- return delegate.save(arg0, arg1);
- }
-
- public void saveOrUpdate(Object arg0) throws HibernateException
- {
- delegate.saveOrUpdate(arg0);
- }
-
- public void saveOrUpdate(String arg0, Object arg1) throws HibernateException
- {
- delegate.saveOrUpdate(arg0, arg1);
- }
-
- public void setCacheMode(CacheMode arg0)
- {
- delegate.setCacheMode(arg0);
- }
-
- public void setFlushMode(FlushMode arg0)
- {
- delegate.setFlushMode(arg0);
- }
-
- public void setReadOnly(Object arg0, boolean arg1)
- {
- delegate.setReadOnly(arg0, arg1);
- }
-
- public void update(Object arg0) throws HibernateException
- {
- delegate.update(arg0);
- }
-
- public void update(String arg0, Object arg1) throws HibernateException
- {
- delegate.update(arg0, arg1);
- }
-
- private SessionImplementor getDelegateSessionImplementor()
- {
- return (SessionImplementor) delegate;
- }
-
- private EventSource getDelegateEventSource()
- {
- return (EventSource) delegate;
- }
-
- public void afterScrollOperation()
- {
- getDelegateSessionImplementor().afterScrollOperation();
- }
-
- public void afterTransactionCompletion(boolean arg0, Transaction arg1)
- {
- getDelegateSessionImplementor().afterTransactionCompletion(arg0, arg1);
- }
-
- public void beforeTransactionCompletion(Transaction arg0)
- {
- getDelegateSessionImplementor().beforeTransactionCompletion(arg0);
- }
-
- public String bestGuessEntityName(Object arg0)
- {
- return getDelegateSessionImplementor().bestGuessEntityName(arg0);
- }
-
- public int executeNativeUpdate(NativeSQLQuerySpecification arg0, QueryParameters arg1) throws HibernateException
- {
- return getDelegateSessionImplementor().executeNativeUpdate(arg0, arg1);
- }
-
- public int executeUpdate(String arg0, QueryParameters arg1) throws HibernateException
- {
- return getDelegateSessionImplementor().executeUpdate(arg0, arg1);
- }
-
- public Batcher getBatcher()
- {
- return getDelegateSessionImplementor().getBatcher();
- }
-
- public Serializable getContextEntityIdentifier(Object arg0)
- {
- return getDelegateSessionImplementor().getContextEntityIdentifier(arg0);
- }
-
- public int getDontFlushFromFind()
- {
- return getDelegateSessionImplementor().getDontFlushFromFind();
- }
-
- public Map getEnabledFilters()
- {
- return getDelegateSessionImplementor().getEnabledFilters();
- }
-
- public EntityPersister getEntityPersister(String arg0, Object arg1) throws HibernateException
- {
- return getDelegateSessionImplementor().getEntityPersister(arg0, arg1);
- }
-
- public Object getEntityUsingInterceptor(EntityKey arg0) throws HibernateException
- {
- return getDelegateSessionImplementor().getEntityUsingInterceptor(arg0);
- }
-
- public SessionFactoryImplementor getFactory()
- {
- return getDelegateSessionImplementor().getFactory();
- }
-
- public String getFetchProfile()
- {
- return getDelegateSessionImplementor().getFetchProfile();
- }
-
- public Type getFilterParameterType(String arg0)
- {
- return getDelegateSessionImplementor().getFilterParameterType(arg0);
- }
-
- public Object getFilterParameterValue(String arg0)
- {
- return getDelegateSessionImplementor().getFilterParameterValue(arg0);
- }
-
- public Interceptor getInterceptor()
- {
- return getDelegateSessionImplementor().getInterceptor();
- }
-
- public JDBCContext getJDBCContext()
- {
- return getDelegateSessionImplementor().getJDBCContext();
- }
-
- public EventListeners getListeners()
- {
- return getDelegateSessionImplementor().getListeners();
- }
-
- public Query getNamedSQLQuery(String arg0)
- {
- return getDelegateSessionImplementor().getNamedSQLQuery(arg0);
- }
-
- public PersistenceContext getPersistenceContext()
- {
- return getDelegateSessionImplementor().getPersistenceContext();
- }
-
- public long getTimestamp()
- {
- return getDelegateSessionImplementor().getTimestamp();
- }
-
- public String guessEntityName(Object arg0) throws HibernateException
- {
- return getDelegateSessionImplementor().guessEntityName(arg0);
- }
-
- public Object immediateLoad(String arg0, Serializable arg1) throws HibernateException
- {
- return getDelegateSessionImplementor().immediateLoad(arg0, arg1);
- }
-
- public void initializeCollection(PersistentCollection arg0, boolean arg1) throws HibernateException
- {
- getDelegateSessionImplementor().initializeCollection(arg0, arg1);
- }
-
- public Object instantiate(String arg0, Serializable arg1) throws HibernateException
- {
- return getDelegateSessionImplementor().instantiate(arg0, arg1);
- }
-
- public Object internalLoad(String arg0, Serializable arg1, boolean arg2, boolean arg3) throws HibernateException
- {
- return getDelegateSessionImplementor().internalLoad(arg0, arg1, arg2, arg3);
- }
-
- public boolean isClosed()
- {
- return getDelegateSessionImplementor().isClosed();
- }
-
- public boolean isEventSource()
- {
- return getDelegateSessionImplementor().isEventSource();
- }
-
- public boolean isTransactionInProgress()
- {
- return getDelegateSessionImplementor().isTransactionInProgress();
- }
-
- public Iterator iterate(String arg0, QueryParameters arg1) throws HibernateException
- {
- return getDelegateSessionImplementor().iterate(arg0, arg1);
- }
-
- public Iterator iterateFilter(Object arg0, String arg1, QueryParameters arg2) throws HibernateException
- {
- return getDelegateSessionImplementor().iterateFilter(arg0, arg1, arg2);
- }
-
- public List list(CriteriaImpl arg0)
- {
- return getDelegateSessionImplementor().list(arg0);
- }
-
- public List list(NativeSQLQuerySpecification arg0, QueryParameters arg1) throws HibernateException
- {
- return getDelegateSessionImplementor().list(arg0, arg1);
- }
-
- public List list(String arg0, QueryParameters arg1) throws HibernateException
- {
- return getDelegateSessionImplementor().list(arg0, arg1);
- }
-
- public List listCustomQuery(CustomQuery arg0, QueryParameters arg1) throws HibernateException
- {
- return getDelegateSessionImplementor().listCustomQuery(arg0, arg1);
- }
-
- public List listFilter(Object arg0, String arg1, QueryParameters arg2) throws HibernateException
- {
- return getDelegateSessionImplementor().listFilter(arg0, arg1, arg2);
- }
-
- public ScrollableResults scroll(CriteriaImpl arg0, ScrollMode arg1)
- {
- return getDelegateSessionImplementor().scroll(arg0, arg1);
- }
-
- public ScrollableResults scroll(NativeSQLQuerySpecification arg0, QueryParameters arg1) throws HibernateException
- {
- return getDelegateSessionImplementor().scroll(arg0, arg1);
- }
-
- public ScrollableResults scroll(String arg0, QueryParameters arg1) throws HibernateException
- {
- return getDelegateSessionImplementor().scroll(arg0, arg1);
- }
-
- public ScrollableResults scrollCustomQuery(CustomQuery arg0, QueryParameters arg1) throws HibernateException
- {
- return getDelegateSessionImplementor().scrollCustomQuery(arg0, arg1);
- }
-
- public void setAutoClear(boolean arg0)
- {
- getDelegateSessionImplementor().setAutoClear(arg0);
- }
-
- public void setFetchProfile(String arg0)
- {
- getDelegateSessionImplementor().setFetchProfile(arg0);
- }
-
- public ActionQueue getActionQueue() {
- return getDelegateEventSource().getActionQueue();
- }
-
- public Object instantiate(EntityPersister entityPersister, Serializable serializable) throws HibernateException {
- return getDelegateEventSource().instantiate( entityPersister, serializable );
- }
-
- public void forceFlush(EntityEntry entityEntry) throws HibernateException {
- getDelegateEventSource().forceFlush( entityEntry );
- }
-
- public void merge(String s, Object o, Map map) throws HibernateException {
- getDelegateEventSource().merge( s, o, map );
- }
-
- public void persist(String s, Object o, Map map) throws HibernateException {
- getDelegateEventSource().persist( s, o, map );
- }
-
- public void persistOnFlush(String s, Object o, Map map) {
- getDelegateEventSource().persistOnFlush( s, o, map );
- }
-
- public void refresh(Object o, Map map) throws HibernateException {
- getDelegateEventSource().refresh( o, map );
- }
-
- public void saveOrUpdateCopy(String s, Object o, Map map) throws HibernateException {
- getDelegateEventSource().saveOrUpdateCopy( s, o , map );
- }
-
- public void delete(String s, Object o, boolean b, Set set) {
- getDelegateEventSource().delete( s, o, b, set );
- }
}
Modified: branches/community/Seam_2_2/src/main/org/jboss/seam/persistence/PersistenceProvider.java
===================================================================
--- branches/community/Seam_2_2/src/main/org/jboss/seam/persistence/PersistenceProvider.java 2010-02-23 19:52:51 UTC (rev 12069)
+++ branches/community/Seam_2_2/src/main/org/jboss/seam/persistence/PersistenceProvider.java 2010-02-23 20:35:56 UTC (rev 12070)
@@ -3,6 +3,7 @@
import static org.jboss.seam.annotations.Install.BUILT_IN;
import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
@@ -216,7 +217,9 @@
*/
public EntityManager proxyEntityManager(EntityManager entityManager)
{
- return new EntityManagerProxy(entityManager);
+ return (EntityManager) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
+ new Class[] { EntityManagerProxy.class },
+ new EntityManagerInvocationHandler(entityManager));
}
/**
More information about the seam-commits
mailing list