FileDocCategorySizeDatePackage
ExtendedSessionInvocationHandler.javaAPI DocJBoss 4.2.13551Fri Jul 13 20:53:58 BST 2007org.jboss.ejb3.entity.hibernate

ExtendedSessionInvocationHandler

public class ExtendedSessionInvocationHandler extends Object implements Serializable, InvocationHandler
Handle method execution delegation to an Hibernate Session following the extended persistence context rules
author
Emmanuel Bernard

Fields Summary
private static final long
serialVersionUID
private String
identity
Constructors Summary
public ExtendedSessionInvocationHandler(String identity)


     
   
      this.identity = identity;
   
public ExtendedSessionInvocationHandler()

   
Methods Summary
public org.hibernate.SessiongetHibernateSession()

      EntityManager persistenceContext = getPersistenceContext();
      if (persistenceContext instanceof HibernateEntityManager )
      {
         return ((HibernateEntityManager) persistenceContext).getSession();
      }
      throw new RuntimeException("ILLEGAL ACTION:  Not a Hibernate persistence provider");
   
public javax.persistence.EntityManagergetPersistenceContext()

      StatefulBeanContext beanContext = StatefulBeanContext.currentBean.get();
      EntityManager persistenceContext = beanContext.getExtendedPersistenceContext(identity);
      if (persistenceContext == null)
         throw new RuntimeException("Unable to determine persistenceContext: " + identity
                                    + " in injected SFSB: " + beanContext.getContainer().getObjectName());
      return persistenceContext;
   
public java.lang.Objectinvoke(java.lang.Object proxy, java.lang.reflect.Method method, java.lang.Object[] args)

      String methodName = method.getName();
      if ( "getPersistenceContext".equals( methodName ) ) {
         return getPersistenceContext();
      }
      else if ( "close".equals( methodName ) ) {
         throw new IllegalStateException("It is illegal to close an injected Hibernate Session");
      }
      else {
         //catch all
         try {
            return method.invoke( getHibernateSession(), args );
         }
         catch ( InvocationTargetException e ) {
				if ( e.getTargetException() instanceof RuntimeException ) {
					throw ( RuntimeException ) e.getTargetException();
				}
				else {
					throw e;
				}
			}
      }