KickJava   Java API By Example, From Geeks To Geeks.

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


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.Field JavaDoc;
19
20 /**
21  * Comment
22  *
23  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
24  * @version $Revision: 1.2.2.3 $
25  */

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