[hibernate-commits] Hibernate SVN: r14275 - in entitymanager/trunk/src: test/org/hibernate/ejb/test/ejb3configuration and 1 other directory.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Tue Jan 15 17:27:11 EST 2008


Author: epbernard
Date: 2008-01-15 17:27:11 -0500 (Tue, 15 Jan 2008)
New Revision: 14275

Added:
   entitymanager/trunk/src/test/org/hibernate/ejb/test/ejb3configuration/LocalExceptionInterceptor.java
Modified:
   entitymanager/trunk/src/java/org/hibernate/ejb/AbstractEntityManagerImpl.java
   entitymanager/trunk/src/java/org/hibernate/ejb/Ejb3Configuration.java
   entitymanager/trunk/src/java/org/hibernate/ejb/EntityManagerFactoryImpl.java
   entitymanager/trunk/src/java/org/hibernate/ejb/EntityManagerImpl.java
   entitymanager/trunk/src/java/org/hibernate/ejb/HibernatePersistence.java
   entitymanager/trunk/src/test/org/hibernate/ejb/test/ejb3configuration/InterceptorTest.java
Log:
EJB-305 add ability to define a session-scoped interceptor through a property hibernate.ejb.interceptor.session_scoped

Modified: entitymanager/trunk/src/java/org/hibernate/ejb/AbstractEntityManagerImpl.java
===================================================================
--- entitymanager/trunk/src/java/org/hibernate/ejb/AbstractEntityManagerImpl.java	2008-01-15 20:53:17 UTC (rev 14274)
+++ entitymanager/trunk/src/java/org/hibernate/ejb/AbstractEntityManagerImpl.java	2008-01-15 22:27:11 UTC (rev 14275)
@@ -45,6 +45,7 @@
 import org.hibernate.QueryException;
 import org.hibernate.TransientObjectException;
 import org.hibernate.StaleObjectStateException;
+import org.hibernate.annotations.common.util.ReflectHelper;
 import org.hibernate.cfg.Environment;
 import org.hibernate.ejb.transaction.JoinableCMTTransaction;
 import org.hibernate.ejb.util.ConfigurationHelper;
@@ -70,8 +71,7 @@
 	private Map properties;
 
 	protected AbstractEntityManagerImpl(
-			PersistenceContextType type, PersistenceUnitTransactionType transactionType, Map properties
-	) {
+			PersistenceContextType type, PersistenceUnitTransactionType transactionType, Map properties) {
 		this.persistenceContextType = type;
 		this.transactionType = transactionType;
 		this.properties = properties != null ? properties : CollectionHelper.EMPTY_MAP;

Modified: entitymanager/trunk/src/java/org/hibernate/ejb/Ejb3Configuration.java
===================================================================
--- entitymanager/trunk/src/java/org/hibernate/ejb/Ejb3Configuration.java	2008-01-15 20:53:17 UTC (rev 14274)
+++ entitymanager/trunk/src/java/org/hibernate/ejb/Ejb3Configuration.java	2008-01-15 22:27:11 UTC (rev 14275)
@@ -668,7 +668,8 @@
 			return new EntityManagerFactoryImpl(
 					cfg.buildSessionFactory(),
 					transactionType,
-					discardOnClose
+					discardOnClose,
+					getSessionInterceptorClass( cfg.getProperties() )
 			);
 		}
 		catch (HibernateException e) {
@@ -681,6 +682,32 @@
 		}
 	}
 
+	private Class getSessionInterceptorClass(Properties properties) {
+		String sessionInterceptorClassname = (String) properties.get( HibernatePersistence.SESSION_INTERCEPTOR );
+		if ( StringHelper.isNotEmpty( sessionInterceptorClassname ) ) {
+			try {
+				Class interceptorClass = ReflectHelper.classForName( sessionInterceptorClassname, Ejb3Configuration.class );
+				interceptorClass.newInstance();
+				return interceptorClass;
+			}
+			catch (ClassNotFoundException e) {
+				throw new PersistenceException( getExceptionHeader() + "Unable to load "
+						+ HibernatePersistence.SESSION_INTERCEPTOR + ": " + sessionInterceptorClassname, e);
+			}
+			catch (IllegalAccessException e) {
+				throw new PersistenceException( getExceptionHeader() + "Unable to instanciate "
+						+ HibernatePersistence.SESSION_INTERCEPTOR + ": " + sessionInterceptorClassname, e);
+			}
+			catch (InstantiationException e) {
+				throw new PersistenceException( getExceptionHeader() + "Unable to instanciate "
+						+ HibernatePersistence.SESSION_INTERCEPTOR + ": " + sessionInterceptorClassname, e);
+			}
+		}
+		else {
+			return null;
+		}
+	}
+
 	public Reference getReference() throws NamingException {
 		log.debug("Returning a Reference to the Ejb3Configuration");
 		ByteArrayOutputStream stream = new ByteArrayOutputStream();

Modified: entitymanager/trunk/src/java/org/hibernate/ejb/EntityManagerFactoryImpl.java
===================================================================
--- entitymanager/trunk/src/java/org/hibernate/ejb/EntityManagerFactoryImpl.java	2008-01-15 20:53:17 UTC (rev 14274)
+++ entitymanager/trunk/src/java/org/hibernate/ejb/EntityManagerFactoryImpl.java	2008-01-15 22:27:11 UTC (rev 14275)
@@ -17,15 +17,17 @@
 	private SessionFactory sessionFactory;
 	private PersistenceUnitTransactionType transactionType;
 	private boolean discardOnClose;
+	private Class sessionInterceptorClass;
 
 	public EntityManagerFactoryImpl(
 			SessionFactory sessionFactory,
 			PersistenceUnitTransactionType transactionType,
-			boolean discardOnClose
-	) {
+			boolean discardOnClose,
+			Class sessionInterceptorClass) {
 		this.sessionFactory = sessionFactory;
 		this.transactionType = transactionType;
 		this.discardOnClose = discardOnClose;
+		this.sessionInterceptorClass = sessionInterceptorClass;
 	}
 
 	public EntityManager createEntityManager() {
@@ -35,7 +37,8 @@
 	public EntityManager createEntityManager(Map map) {
 		//TODO support discardOnClose, persistencecontexttype?, interceptor,
 		return new EntityManagerImpl(
-				sessionFactory, PersistenceContextType.EXTENDED, transactionType, discardOnClose, map
+				sessionFactory, PersistenceContextType.EXTENDED, transactionType,
+				discardOnClose, sessionInterceptorClass, map
 		);
 	}
 

Modified: entitymanager/trunk/src/java/org/hibernate/ejb/EntityManagerImpl.java
===================================================================
--- entitymanager/trunk/src/java/org/hibernate/ejb/EntityManagerImpl.java	2008-01-15 20:53:17 UTC (rev 14274)
+++ entitymanager/trunk/src/java/org/hibernate/ejb/EntityManagerImpl.java	2008-01-15 22:27:11 UTC (rev 14275)
@@ -3,6 +3,7 @@
 
 import java.util.Map;
 import javax.persistence.PersistenceContextType;
+import javax.persistence.PersistenceException;
 import javax.persistence.spi.PersistenceUnitTransactionType;
 import javax.transaction.Synchronization;
 
@@ -11,6 +12,8 @@
 import org.hibernate.HibernateException;
 import org.hibernate.Session;
 import org.hibernate.SessionFactory;
+import org.hibernate.Interceptor;
+import org.hibernate.annotations.common.util.ReflectHelper;
 import org.hibernate.cfg.Environment;
 import org.hibernate.engine.SessionImplementor;
 
@@ -24,16 +27,37 @@
 	protected SessionFactory sessionFactory;
 	protected boolean open;
 	protected boolean discardOnClose;
+	private Class sessionInterceptorClass;
 
 	public EntityManagerImpl(
 			SessionFactory sessionFactory, PersistenceContextType pcType,
 			PersistenceUnitTransactionType transactionType,
-			boolean discardOnClose, Map properties
+			boolean discardOnClose, Class sessionInterceptorClass, Map properties
 	) {
 		super( pcType, transactionType, properties );
 		this.sessionFactory = sessionFactory;
 		this.open = true;
 		this.discardOnClose = discardOnClose;
+		Object localSic = null;
+		if (properties != null) localSic = properties.get( HibernatePersistence.SESSION_INTERCEPTOR );
+		if ( localSic != null ) {
+			if (localSic instanceof Class) {
+				sessionInterceptorClass = (Class) localSic;
+			}
+			else if (localSic instanceof String) {
+				try {
+					sessionInterceptorClass =
+							ReflectHelper.classForName( (String) localSic, EntityManagerImpl.class );
+				}
+				catch (ClassNotFoundException e) {
+					throw new PersistenceException("Unable to instanciate interceptor: " + localSic, e);
+				}
+			}
+			else {
+				throw new PersistenceException("Unable to instanciate interceptor: " + localSic);
+			}
+		}
+		this.sessionInterceptorClass = sessionInterceptorClass;
 		postInit();
 	}
 
@@ -45,7 +69,22 @@
 
 	protected Session getRawSession() {
 		if ( session == null ) {
-			session = sessionFactory.openSession();
+			Interceptor interceptor = null;
+			if (sessionInterceptorClass != null) {
+				try {
+					interceptor = (Interceptor) sessionInterceptorClass.newInstance();
+				}
+				catch (InstantiationException e) {
+					throw new PersistenceException("Unable to instanciate session interceptor: " + sessionInterceptorClass, e);
+				}
+				catch (IllegalAccessException e) {
+					throw new PersistenceException("Unable to instanciate session interceptor: " + sessionInterceptorClass, e);
+				}
+				catch (ClassCastException e) {
+					throw new PersistenceException("Session interceptor does not implement Interceptor: " + sessionInterceptorClass, e);
+				}
+			}
+			session = sessionFactory.openSession( interceptor );
 			if ( persistenceContextType == PersistenceContextType.TRANSACTION ) {
 				( (SessionImplementor) session ).setAutoClear( true );
 			}

Modified: entitymanager/trunk/src/java/org/hibernate/ejb/HibernatePersistence.java
===================================================================
--- entitymanager/trunk/src/java/org/hibernate/ejb/HibernatePersistence.java	2008-01-15 20:53:17 UTC (rev 14274)
+++ entitymanager/trunk/src/java/org/hibernate/ejb/HibernatePersistence.java	2008-01-15 22:27:11 UTC (rev 14275)
@@ -51,9 +51,14 @@
 	public static final String COLLECTION_CACHE_PREFIX = "hibernate.ejb.collectioncache";
 	/**
 	 * Interceptor class name, the class has to have a no-arg constructor
+	 * the interceptor instance is shared amongst all EntityManager of a given EntityManagerFactory
 	 */
 	public static final String INTERCEPTOR = "hibernate.ejb.interceptor";
 	/**
+	 * Interceptor class name, the class has to have a no-arg constructor
+	 */
+	public static final String SESSION_INTERCEPTOR = "hibernate.ejb.interceptor.session_scoped";
+	/**
 	 * Naming strategy class name, the class has to have a no-arg constructor
 	 */
 	public static final String NAMING_STRATEGY = "hibernate.ejb.naming_strategy";

Modified: entitymanager/trunk/src/test/org/hibernate/ejb/test/ejb3configuration/InterceptorTest.java
===================================================================
--- entitymanager/trunk/src/test/org/hibernate/ejb/test/ejb3configuration/InterceptorTest.java	2008-01-15 20:53:17 UTC (rev 14274)
+++ entitymanager/trunk/src/test/org/hibernate/ejb/test/ejb3configuration/InterceptorTest.java	2008-01-15 22:27:11 UTC (rev 14275)
@@ -56,6 +56,29 @@
 		}
 	}
 
+	public void testConfiguredSessionInterceptor() {
+		configuration.setProperty( HibernatePersistence.SESSION_INTERCEPTOR, LocalExceptionInterceptor.class.getName() );
+		configuration.setProperty( "aaaaaaaaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbbbbbb" );
+		EntityManagerFactory emf = configuration.createEntityManagerFactory();
+		EntityManager em = emf.createEntityManager();
+		Item i = new Item();
+		i.setName( "Laptop" );
+		try {
+			em.getTransaction().begin();
+			em.persist( i );
+			em.getTransaction().commit();
+			fail( "No interceptor" );
+		}
+		catch (IllegalStateException e) {
+			assertEquals( LocalExceptionInterceptor.LOCAL_EXCEPTION_MESSAGE, e.getMessage() );
+		}
+		finally {
+			if ( em.getTransaction() != null && em.getTransaction().isActive() ) em.getTransaction().rollback();
+			em.close();
+			emf.close();
+		}
+	}
+
 	public void testEmptyCreateEntityManagerFactoryAndPropertyUse() {
 		configuration.setProperty( HibernatePersistence.INTERCEPTOR, ExceptionInterceptor.class.getName() );
 		EntityManagerFactory emf = configuration.createEntityManagerFactory();

Added: entitymanager/trunk/src/test/org/hibernate/ejb/test/ejb3configuration/LocalExceptionInterceptor.java
===================================================================
--- entitymanager/trunk/src/test/org/hibernate/ejb/test/ejb3configuration/LocalExceptionInterceptor.java	                        (rev 0)
+++ entitymanager/trunk/src/test/org/hibernate/ejb/test/ejb3configuration/LocalExceptionInterceptor.java	2008-01-15 22:27:11 UTC (rev 14275)
@@ -0,0 +1,19 @@
+//$
+package org.hibernate.ejb.test.ejb3configuration;
+
+import java.io.Serializable;
+
+import org.hibernate.type.Type;
+import org.hibernate.CallbackException;
+
+/**
+ * @author Emmanuel Bernard
+ */
+public class LocalExceptionInterceptor extends ExceptionInterceptor {
+	public static final String LOCAL_EXCEPTION_MESSAGE = "Session-scoped interceptor enabled";
+
+	public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types)
+			throws CallbackException {
+		throw new IllegalStateException( LOCAL_EXCEPTION_MESSAGE );
+	}
+}




More information about the hibernate-commits mailing list