Clover Coverage Report - perfectjpattern(Aggregated)
Coverage timestamp: Sat Feb 28 2009 14:35:07 CET
../../../../../img/srcFileCovDistChart10.png 0% of files have more coverage
21   172   8   3.5
4   91   0.38   6
6     1.33  
1    
16.2% of code in this file is excluded from these metrics.
 
  AbstractDaoFactory       Line # 38 21 16.2% 8 2 93.5% 0.9354839
 
  (17)
 
1    //----------------------------------------------------------------------
2    //
3    // PerfectJPattern: "Design patterns are good but components are better!"
4    // AbstractDaoFactory.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.io.*;
24    import java.util.*;
25   
26    import org.apache.commons.lang.*;
27    import org.perfectjpattern.core.api.creational.singleton.*;
28    import org.perfectjpattern.jee.api.integration.dao.*;
29   
30    /**
31    * Abstract reusable base partial implementation for {@link IBaseDaoFactory}
32    * concrete implementations
33    *
34    * @author <a href="mailto:bravegag@hotmail.com">Giovanni Azua</a>
35    * @version $Revision: 1.0 $Date: Feb 19, 2009 12:16:49 PM $
36    */
37    public abstract
 
38    class AbstractDaoFactory
39    implements IBaseDaoFactory, ISingleton
40    {
41    //------------------------------------------------------------------------
42    // public
43    //------------------------------------------------------------------------
44    /**
45    * {@inheritDoc}
46    */
 
47  5 toggle public void
48    setDaoSessionStrategy(ISessionStrategy aSessionStrategy)
49    throws IllegalArgumentException
50    {
51  5 Validate.notNull(aSessionStrategy,
52    "'aDaoSessionStrategy' must not be null");
53   
54    // shutdown existing
55  5 if (theSessionStrategy != null)
56    {
57  0 shutdown();
58    }
59   
60  5 theSessionStrategy = aSessionStrategy;
61    }
62   
63    //------------------------------------------------------------------------
64    /**
65    * {@inheritDoc}
66    */
 
67  5 toggle public void
68    setDaoTransactionStrategy(ITransactionStrategy aTransactionStrategy)
69    throws IllegalArgumentException
70    {
71  5 Validate.notNull(aTransactionStrategy,
72    "'aTransactionFactory' must not be null");
73   
74  5 theTransactionStrategy = aTransactionStrategy;
75    }
76   
77    //------------------------------------------------------------------------
78    /**
79    * {@inheritDoc}
80    */
 
81  3 toggle public void
82    shutdown()
83    throws DaoException
84    {
85    assert theSessionStrategy != null :
86    "'theSessionStrategy' must not be null";
87   
88  3 theSessionStrategy.shutdown();
89   
90  3 theSessionStrategy = null;
91    }
92   
93    //------------------------------------------------------------------------
94    /**
95    * {@inheritDoc}
96    */
 
97  4 toggle @SuppressWarnings("unchecked")
98    public <Id extends Serializable, Element> IBaseReadOnlyDao<Id, Element>
99    createReadOnlyDao(Class<Element> aPersistentClass)
100    throws IllegalArgumentException
101    {
102  4 Validate.notNull(aPersistentClass,
103    "'aPersistentClass' must not be null");
104  4 Validate.isTrue(!aPersistentClass.isInterface(),
105    "'aPersistentClass' must be a class type");
106   
107  4 IBaseReadOnlyDao<Id, Element> myBaseReadOnlyDao = createDao(
108    aPersistentClass);
109   
110    assert myBaseReadOnlyDao != null :
111    "myBaseReadOnlyDao must not be null";
112   
113  4 return myBaseReadOnlyDao;
114    }
115   
116    //------------------------------------------------------------------------
117    /**
118    * {@inheritDoc}
119    */
 
120  33 toggle @SuppressWarnings("unchecked")
121    public <Id extends Serializable, Element> IBaseDao<Id, Element>
122    createDao(Class<Element> aPersistentClass)
123    throws IllegalArgumentException
124    {
125  33 Validate.notNull(aPersistentClass,
126    "'aPersistentClass' must not be null");
127  33 Validate.isTrue(!aPersistentClass.getClass().isInterface(),
128    "'aPersistentClass' must be a class type");
129   
130  33 IBaseDao<Id, Element> myBaseDao = null;
131   
132  33 if (theBaseDaoMap.containsKey(aPersistentClass))
133    {
134    // just reuse the existing instance
135  29 myBaseDao = (IBaseDao<Id, Element>) theBaseDaoMap.get(
136    aPersistentClass);
137    }
138    else
139    {
140    // create new instance and make it available for future calls
141  4 myBaseDao = createDao(aPersistentClass,
142    theSessionStrategy, theTransactionStrategy);
143  4 theBaseDaoMap.put(aPersistentClass, myBaseDao);
144    }
145   
146    assert myBaseDao != null : "myBaseDao must not be null";
147   
148  33 return myBaseDao;
149    }
150   
151    //------------------------------------------------------------------------
152    // protected
153    //------------------------------------------------------------------------
 
154  4 toggle protected
155    AbstractDaoFactory()
156    {
157  4 theBaseDaoMap = new HashMap<Class<?>, IBaseDao<?, ?>>();
158    }
159   
160    //------------------------------------------------------------------------
161    protected abstract <Id extends Serializable, Element> IBaseDao<Id, Element>
162    createDao(Class<Element> aPersistentClass,
163    ISessionStrategy aSessionStrategy,
164    ITransactionStrategy aTransactionStrategy);
165   
166    //------------------------------------------------------------------------
167    // members
168    //------------------------------------------------------------------------
169    private ISessionStrategy theSessionStrategy;
170    private ITransactionStrategy theTransactionStrategy;
171    private final Map<Class<?>, IBaseDao<?, ?>> theBaseDaoMap;
172    }