KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > injection > UserTransactionMethodInjector


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.ejb3.injection;
8
9 import org.jboss.aop.Advisor;
10 import org.jboss.ejb3.BeanContext;
11 import org.jboss.ejb3.Container;
12 import org.jboss.ejb3.tx.TxUtil;
13 import org.jboss.ejb3.tx.UserTransactionImpl;
14
15 import javax.ejb.TransactionManagementType JavaDoc;
16 import javax.transaction.UserTransaction JavaDoc;
17
18 import java.lang.reflect.InvocationTargetException JavaDoc;
19 import java.lang.reflect.Method JavaDoc;
20
21 /**
22  * Comment
23  *
24  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
25  * @version $Revision: 1.2.2.3 $
26  */

27 public class UserTransactionMethodInjector implements Injector
28 {
29    private Method JavaDoc setMethod;
30
31    public UserTransactionMethodInjector(Method JavaDoc setMethod, Container container)
32    {
33       TransactionManagementType JavaDoc type = TxUtil.getTransactionManagementType(((Advisor) container));
34       if (type != TransactionManagementType.BEAN) throw new IllegalStateException JavaDoc("Container " + container.getEjbName() + ": it is illegal to inject UserTransaction into a CMT bean");
35       this.setMethod = setMethod;
36    }
37
38    public void inject(BeanContext ctx)
39    {
40       UserTransaction JavaDoc ut = new UserTransactionImpl();
41       Object JavaDoc[] args = {ut};
42       try
43       {
44          setMethod.invoke(ctx.getInstance(), args);
45       }
46       catch (IllegalAccessException JavaDoc e)
47       {
48          throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
49
}
50       catch (IllegalArgumentException JavaDoc e)
51       {
52          throw new RuntimeException JavaDoc("Failed in setting EntityManager on setter method: " + setMethod.toString());
53       }
54       catch (InvocationTargetException JavaDoc e)
55       {
56          throw new RuntimeException JavaDoc(e.getCause()); //To change body of catch statement use Options | File Templates.
57
}
58    }
59 }
60
Popular Tags