KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.lang.reflect.Field JavaDoc;
10 import javax.persistence.EntityManager;
11 import javax.persistence.Transient;
12 import javax.persistence.PersistenceContextType;
13 import org.jboss.ejb3.BeanContext;
14 import org.jboss.ejb3.Container;
15 import org.jboss.ejb3.EJBContainer;
16 import org.jboss.ejb3.entity.ManagedEntityManagerFactory;
17
18 /**
19  * Comment
20  *
21  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
22  * @version $Revision: 1.4.2.3 $
23  */

24 public class EntityManagerFieldInjector extends EntityManagerInjector
25 {
26    protected Field JavaDoc field;
27
28    public EntityManagerFieldInjector(Field JavaDoc field, Container container, ManagedEntityManagerFactory factory, PersistenceContextType type)
29    {
30       super(container, factory, type);
31       this.field = field;
32       this.field.setAccessible(true);
33    }
34
35    public void inject(BeanContext ctx)
36    {
37       try
38       {
39          EntityManager entityManager = null;
40          boolean isTransient = ((EJBContainer)container).resolveAnnotation(field, Transient.class) != null;
41          entityManager = getEntityManager(ctx, isTransient);
42          field.set(ctx.getInstance(), entityManager);
43       }
44       catch (IllegalAccessException JavaDoc e)
45       {
46          throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
47
}
48       catch (IllegalArgumentException JavaDoc e)
49       {
50          throw new RuntimeException JavaDoc("Failed in setting EntityManager on setter field: " + field.toString());
51       }
52    }
53
54 }
55
Popular Tags