Clover Coverage Report - perfectjpattern(Aggregated)
Coverage timestamp: Sat Feb 28 2009 14:35:07 CET
../../../../../img/srcFileCovDistChart10.png 0% of files have more coverage
12   117   8   3
2   56   0.67   4
4     2  
1    
10% of code in this file is excluded from these metrics.
 
  HibernateCurrentSessionStrategy       Line # 44 12 10% 8 0 100% 1.0
 
  (14)
 
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  6 toggle public
51    HibernateCurrentSessionStrategy()
52    {
53  6 try
54    {
55  6 Configuration myConfiguration = new Configuration();
56  6 myConfiguration.configure();
57   
58  6 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  2 toggle public
70    HibernateCurrentSessionStrategy(SessionFactory aSessionFactory)
71    {
72  2 Validate.notNull(aSessionFactory, "'aSessionFactory' must not be null");
73   
74  2 theSessionFactory = aSessionFactory;
75    }
76   
77    //------------------------------------------------------------------------
78    /**
79    * {@inheritDoc}
80    */
 
81  3 toggle public void
82    shutdown()
83    {
84  3 theSessionFactory.close();
85    }
86   
87    //------------------------------------------------------------------------
88    /**
89    * {@inheritDoc}
90    */
 
91  110 toggle public ISession
92    getSession()
93    {
94  110 try
95    {
96  110 Session mySession = theSessionFactory.getCurrentSession();
97   
98  110 if (theSessionAdapter == null || theSessionAdapter.getAdaptee()
99    != mySession)
100    {
101  11 theSessionAdapter = new HibernateSessionAdapter(mySession);
102    }
103   
104  110 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    }