KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.persistence.EntityManager;
10 import javax.persistence.PersistenceContextType;
11 import org.jboss.ejb3.BeanContext;
12 import org.jboss.ejb3.Container;
13 import org.jboss.ejb3.entity.ExtendedPersistenceContext;
14 import org.jboss.ejb3.entity.InjectedEntityManager;
15 import org.jboss.ejb3.entity.ManagedEntityManagerFactory;
16 import org.jboss.ejb3.stateful.StatefulContainer;
17 import org.jboss.logging.Logger;
18
19 /**
20  * Comment
21  *
22  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
23  * @version $Revision: 1.1.2.2 $
24  */

25 public abstract class EntityManagerInjector implements Injector
26 {
27    protected static final Logger log = Logger.getLogger(EntityManagerFieldInjector.class);
28    protected Container container;
29    protected ManagedEntityManagerFactory factory;
30    protected PersistenceContextType type;
31
32
33    protected EntityManagerInjector(Container container, ManagedEntityManagerFactory factory, PersistenceContextType type)
34    {
35       this.container = container;
36       this.factory = factory;
37       this.type = type;
38    }
39
40    protected EntityManager getEntityManager(BeanContext ctx, boolean isTransient)
41    {
42       EntityManager entityManager;
43       if (type == PersistenceContextType.EXTENDED)
44       {
45          if (!(container instanceof StatefulContainer)) throw new IllegalStateException JavaDoc("EXTENDED persistence contexts can only be used within a Stateful Session bean");
46          entityManager = (EntityManager)ctx.getMetaData().getMetaData(ManagedEntityManagerFactory.LONG_LIVED_SESSION, factory.getJndiName());
47          if (entityManager == null)
48          {
49             entityManager = new ExtendedPersistenceContext(isTransient, factory);
50             log.info("***Injecting an extended EntityManager: " + factory.getJndiName());
51             ctx.getMetaData().addMetaData(ManagedEntityManagerFactory.LONG_LIVED_SESSION, factory.getJndiName(), entityManager);
52          }
53       }
54       else
55       {
56          entityManager = new InjectedEntityManager(factory);
57       }
58       return entityManager;
59    }
60 }
61
Popular Tags