View Javadoc
1   package ejava.examples.webtier.jpa;
2   
3   import java.util.HashMap;
4   import java.util.Map;
5   import java.util.Properties;
6   
7   import javax.persistence.EntityManager;
8   import javax.persistence.EntityManagerFactory;
9   import javax.persistence.Persistence;
10  
11  import org.apache.commons.logging.Log;
12  import org.apache.commons.logging.LogFactory;
13  
14  /**
15   * This class is used to create and hold open EntityManagerFactory objects 
16   * when not running within a Java EE environment.
17   *
18   * @author jcstaff
19   */
20  public class JPAUtil {
21      private static final Log log = LogFactory.getLog(JPAUtil.class);
22      private static final Map<String, EntityManagerFactory> factories = 
23          new HashMap<String, EntityManagerFactory>();
24      private static ThreadLocal<EntityManager> em = 
25          new ThreadLocal<EntityManager>();
26      private static Properties emfProperties = new Properties();
27      
28      /**
29       * This method defines the properties to pass to createEntityManagerFactory
30       */
31      public static void setEntityManagerFactoryProperties(Properties props) {
32          emfProperties = (Properties)props.clone();
33      }
34      
35      /**
36       * This method will create or return the EntityManagerFactory for 
37       * the specified persistence unit.
38       * 
39       * @param puName matches value in persistence.xml
40       * @return
41       */
42      public static EntityManagerFactory getEntityManagerFactory(String puName) {
43          EntityManagerFactory emf = factories.get(puName);
44          if (emf == null) {
45              synchronized(factories) {
46                  emf = factories.get(puName);
47                  if (emf == null) {
48                      log.debug("creating EntityManagerFactory(" + puName + ")");
49                      emf = (emfProperties != null && emfProperties.size() > 0) ?
50                              Persistence.createEntityManagerFactory(puName, 
51                                      emfProperties) :
52                              Persistence.createEntityManagerFactory(puName);                                
53                      factories.put(puName, emf);
54                  }
55              }
56          }
57          return emf;
58      }
59          
60      public static void setEntityManager(EntityManager emgr) {
61          em.set(emgr);
62      }    
63  
64      public static EntityManager getEntityManager(String puName) {
65          EntityManager emgr = em.get();
66          if (emgr == null) {
67              synchronized (em) {
68                  if (emgr == null) {
69                      //okay-nobody gave us one, use JavaSE technique to get one
70                      emgr = getEntityManagerFactory(puName).createEntityManager();
71                      setEntityManager(emgr);
72                  }
73              }
74          }
75          return em.get();
76      }
77      public static EntityManager getEntityManager() {
78          EntityManager emgr = em.get();
79          if (emgr == null) {
80              log.fatal("EntityManager not initialized for Thread!!!");
81          }
82          return em.get();
83      }
84      /**
85       * This method just allows the caller to look at entity manager to see
86       * if it is currently set.
87       * @return
88       */
89      public static EntityManager peekEntityManager() {
90          return em.get();
91      }
92      public static void closeEntityManager() {
93          EntityManager emgr = em.get();
94          if (emgr != null) {
95              em.remove();
96              emgr.close();
97          }
98      }
99  
100     
101     /**
102      * This method closes all EntityManagerFactory objects and clears them
103      * from the cache.
104      * 
105      */
106     public static void close() {
107         log.debug("closing " + factories.size() + " EntityManagerFactories");
108         synchronized(factories) {
109             for(String puName : factories.keySet()) {
110                 factories.get(puName).close();
111                 log.debug(puName + " closed");
112             }
113             factories.clear();
114         }
115     }
116     
117     
118 }