2 Replies Latest reply on Nov 24, 2010 1:52 PM by dan.j.allen

    Interceptors blocking Injection?

    jasondlee

      I'm following Arun's blog entry here: http://blogs.sun.com/arungupta/entry/totd151transactionalinterceptorsusing


      The problem I have, though, is that once I add the interceptor binding annotation to a class, injection does not seem to work on it.  For example, if I alter his ShoppingCart class like this:




      @TxInterceptorBinding
      @RequestScoped
      public class ShoppingCart {
          @PersistenceUnit
          private EntityManagerFactory emf;
      
          public String getMessage() {
              System.out.println("Checking out");
              return "Checking out";
          }
      }





      emf is null when getMessage() is called (I changed the method name and signature so I could call it and see output from my JSF page).


      The log file shows no errors or warnings.  Any thoughts?

        • 1. Re: Interceptors blocking Injection?
          dan.j.allen

          Jason,


          This appears to be an outstanding issue in the Weld GlassFish integration. I tried the test case below on GlassFish 3.0.1 (Weld 1.0.1-SP3) and GlassFish 3.1 (Weld 1.1.0.Beta2) and got the same results: no resource injection. I removed the @Transactional binding as you did and observed the resource injection work.


          In both cases, injection using @Inject works, so this is strictly a problem with resource injections (e.g., @Resource, @PersistenceUnit, etc).


          The injection does work on JBoss AS 6, so my gut tells me it's a problem at the integration layer.


          Here's the Arquillian test I used, along with the supporting classes and resources:


          TransactionInterceptorTest.java


          @RunWith(Arquillian.class)
          public class TransactionInterceptorTest
          {
             @Deployment
             public static Archive<?> createDeployment()
             {
                return ShrinkWrap.create(WebArchive.class, "test.war")
                   .addClasses(ShoppingCart.class, TransactionInterceptor.class, Transactional.class)
                   .addManifestResource("test-persistence.xml", "persistence.xml")
                   .addWebResource("transaction-interceptor-beans.xml", "beans.xml");
             }
          
             @Inject
             ShoppingCart cart;
          
             @Test
             public void interceptor_should_be_applied()
             {
                TransactionInterceptor.interceptorInvoked = false;
                cart.checkOut();
                assertTrue(TransactionInterceptor.interceptorInvoked);
             }
             
             @Test
             public void resource_injection_should_occur()
             {
                assertTrue(cart.isResourceInjectionWorking());
             }
          }



          Transactional.java


          @Inherited
          @InterceptorBinding
          @Retention(RetentionPolicy.RUNTIME)
          @Target({ ElementType.METHOD, ElementType.TYPE })
          public @interface Transactional {}



          TransactionInterceptor.java


          @Transactional
          @Interceptor
          public class TransactionInterceptor
          {
             public static boolean interceptorInvoked;
             
             @Inject
             private UserTransaction tx;
          
             @AroundInvoke
             public Object manageTransaction(InvocationContext context) throws Exception
             {
                interceptorInvoked = true;
                tx.begin();
                System.out.println("Starting transaction");
                Object result = context.proceed();
                tx.commit();
                System.out.println("Committing transaction");
          
                return result;
             }
          }



          ShoppingCart.java


          @Named
          @RequestScoped
          @Transactional
          public class ShoppingCart
          {
             @PersistenceUnit(unitName = "em")
             private EntityManagerFactory emf;
             
             @PersistenceContext
             private EntityManager em;
             
             @Inject
             private UserTransaction utx;
             
             public void checkOut()
             {
                System.out.println("Checking out");
             }
             
             public boolean isResourceInjectionWorking()
             {
                System.out.println("emf = " + emf + "; em = " + em + "; utx = " + utx);
                return emf != null;
             }
          }



          transaction-interceptor-beans.xml


          <beans xmlns="http://java.sun.com/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="
                http://java.sun.com/xml/ns/javaee
                http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
            <interceptors>
              <class>com.acme.resting.test.TransactionInterceptor</class>
            </interceptors>
          </beans>



          test-persistence.xml


          <persistence version="2.0"
             xmlns="http://java.sun.com/xml/ns/persistence" 
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="
                http://java.sun.com/xml/ns/persistence
                http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
             <persistence-unit name="em">
                <jta-data-source>jdbc/__default</jta-data-source>
             </persistence-unit>
          </persistence>


           

          • 2. Re: Interceptors blocking Injection?
            dan.j.allen

            Can you file an issue in GlassFish?