1 //----------------------------------------------------------------------
2 //
3 // PerfectJPattern: "Design patterns are good but components are better!"
4 // HibernateCurrentSessionStrategy.java Copyright (c) 2009
5 // Giovanni Azua Garcia 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 org.apache.commons.lang.*;
24 import org.hibernate.*;
25 import org.hibernate.cfg.*;
26 import org.perfectjpattern.core.api.structural.adapter.*;
27 import org.perfectjpattern.jee.api.integration.dao.*;
28
29 /**
30 * Concrete simple implementation of {@link ISessionStrategy} corresponding to
31 * {@link SessionFactory#getCurrentSession()}
32 * <br/>
33 * <br/>
34 * This strategy is mandated by the Hibernate configuration value specified in
35 * property <code>current_session_context_class</code> e.g. <code>thread</code>
36 * value will default to using {@link org.hibernate.context.
37 * ThreadLocalSessionContext} which is the default and preferred method in
38 * Java SE environments.
39 *
40 * @author <a href="mailto:bravegag@hotmail.com">Giovanni Azua</a>
41 * @version $Revision: 1.0 $Date: Feb 8, 2009 5:24:51 PM $
42 */
43 public final
44 class HibernateCurrentSessionStrategy
45 implements ISessionStrategy
46 {
47 //------------------------------------------------------------------------
48 // public
49 //------------------------------------------------------------------------
50 public
51 HibernateCurrentSessionStrategy()
52 {
53 try
54 {
55 Configuration myConfiguration = new Configuration();
56 myConfiguration.configure();
57
58 theSessionFactory = myConfiguration.buildSessionFactory();
59 }
60 // CHECKSTYLE:OFF
61 catch (Throwable anException)
62 // CHECKSTYLE:ON
63 {
64 throw new DaoException(anException);
65 }
66 }
67
68 //------------------------------------------------------------------------
69 public
70 HibernateCurrentSessionStrategy(SessionFactory aSessionFactory)
71 {
72 Validate.notNull(aSessionFactory, "'aSessionFactory' must not be null");
73
74 theSessionFactory = aSessionFactory;
75 }
76
77 //------------------------------------------------------------------------
78 /**
79 * {@inheritDoc}
80 */
81 public void
82 shutdown()
83 {
84 theSessionFactory.close();
85 }
86
87 //------------------------------------------------------------------------
88 /**
89 * {@inheritDoc}
90 */
91 public ISession
92 getSession()
93 {
94 try
95 {
96 Session mySession = theSessionFactory.getCurrentSession();
97
98 if (theSessionAdapter == null || theSessionAdapter.getAdaptee()
99 != mySession)
100 {
101 theSessionAdapter = new HibernateSessionAdapter(mySession);
102 }
103
104 return theSessionAdapter.getTarget();
105 }
106 catch (HibernateException anException)
107 {
108 throw new DaoException(anException);
109 }
110 }
111
112 //------------------------------------------------------------------------
113 // members
114 //------------------------------------------------------------------------
115 private final SessionFactory theSessionFactory;
116 private IAdapter<ISession, Session> theSessionAdapter;
117 }