1 //----------------------------------------------------------------------
2 //
3 // PerfectJPattern: "Design patterns are good but components are better!"
4 // JpaSessionStrategy.java Copyright (c) 2009 Giovanni Azua Garcia
5 // bravegag@hotmail.com
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 3
10 // of the License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, see <http://www.gnu.org/licenses/>.
19 //
20 //----------------------------------------------------------------------
21 package org.perfectjpattern.jee.integration.dao;
22
23 import java.util.*;
24
25 import javax.persistence.*;
26
27 import org.perfectjpattern.core.api.structural.adapter.*;
28 import org.perfectjpattern.jee.api.integration.dao.*;
29
30 /**
31 * Concrete simple implementation of {@link ISessionStrategy} targeting
32 * JPA on JSE environment
33 *
34 * @author <a href="mailto:bravegag@hotmail.com">Giovanni Azua</a>
35 * @version $Revision: 1.0 $Date: Feb 18, 2009 1:27:19 PM $
36 */
37 public class JpaSessionStrategy
38 implements ISessionStrategy
39 {
40 //------------------------------------------------------------------------
41 // public
42 //------------------------------------------------------------------------
43 public
44 JpaSessionStrategy()
45 {
46 this("default", new Properties());
47 }
48
49 //------------------------------------------------------------------------
50 public
51 JpaSessionStrategy(String aName, Properties aProperties)
52 {
53 try
54 {
55 theEntityManagerFactory = Persistence.createEntityManagerFactory(
56 aName, aProperties);
57 }
58 // CHECKSTYLE:OFF
59 catch (Throwable anException)
60 // CHECKSTYLE:ON
61 {
62 throw new DaoException(anException);
63 }
64 }
65
66 //------------------------------------------------------------------------
67 /**
68 * {@inheritDoc}
69 */
70 public ISession
71 getSession()
72 {
73 try
74 {
75 if (theSessionAdapter == null)
76 {
77 EntityManager myEntityManager = theEntityManagerFactory.
78 createEntityManager();
79 theSessionAdapter = new EntityManagerAdapter(myEntityManager);
80 }
81
82 assert theSessionAdapter != null :
83 "'theSessionAdapter' must not be null";
84
85 return theSessionAdapter.getTarget();
86 }
87 // CHECKSTYLE:OFF
88 catch (RuntimeException anException)
89 // CHECKSTYLE:ON
90 {
91 throw new DaoException(anException);
92 }
93 }
94
95 //------------------------------------------------------------------------
96 /**
97 * {@inheritDoc}
98 */
99 public void
100 shutdown()
101 {
102 theEntityManagerFactory.close();
103 }
104
105 //------------------------------------------------------------------------
106 // members
107 //------------------------------------------------------------------------
108 private final EntityManagerFactory theEntityManagerFactory;
109 private IAdapter<ISession, EntityManager> theSessionAdapter;
110 }