View Javadoc

1   //----------------------------------------------------------------------
2   // 
3   // PerfectJPattern: "Design patterns are good but components are better!" 
4   // HibernateDaoFactory.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  
25  import org.perfectjpattern.jee.api.integration.dao.*;
26  
27  /**
28   * Concrete implementation for {@link IBaseDaoFactory}
29   * 
30   * @author <a href="mailto:bravegag@hotmail.com">Giovanni Azua</a>
31   * @version $ $Date: Dec 5, 2008 5:11:45 PM $
32   */
33  public
34  class JpaDaoFactory
35  extends AbstractDaoFactory
36  {
37      //------------------------------------------------------------------------
38      // public
39      //------------------------------------------------------------------------
40      /**
41       * Returns Singleton instance of {@link JpaDaoFactory}
42       * 
43       * @return Singleton instance of {@link JpaDaoFactory}
44       */
45      public static JpaDaoFactory
46      getInstance()
47      {
48          return INSTANCE;
49      }    
50      
51      //------------------------------------------------------------------------
52      // protected
53      //------------------------------------------------------------------------
54      /**
55       * Constructs {@link JpaDaoFactory} to consume Session instances 
56       * from the given {@link ISessionStrategy} and Transaction instances from 
57       * the {@link ITransactionStrategy}
58       * 
59       * @param aSessionStrategy {@link ISessionStrategy} to consume 
60       *        Session instances from
61       * @param aTransactionStrategy {@link ITransactionStrategy} to 
62       *        consume Transaction instances from
63       * @throws IllegalArgumentException 'aDaoSessionStrategy' must not be null
64       * @throws IllegalArgumentException 'aDaoTransactionStrategy' must not 
65       *         be null
66       */
67      protected 
68      JpaDaoFactory(ISessionStrategy aSessionStrategy, 
69          ITransactionStrategy aTransactionStrategy)
70      throws IllegalArgumentException
71      {
72          setDaoSessionStrategy(aSessionStrategy);
73          setDaoTransactionStrategy(aTransactionStrategy);
74      }           
75  
76      //------------------------------------------------------------------------
77      /**
78       * Defaults to JSE mode
79       */
80      protected
81      JpaDaoFactory()
82      {
83          ISessionStrategy mySessionStrategy = new JpaSessionStrategy();
84          ITransactionStrategy myTransactionStrategy = 
85              new JpaTransactionStrategy(mySessionStrategy);
86  
87          setDaoSessionStrategy(mySessionStrategy);
88          setDaoTransactionStrategy(myTransactionStrategy);
89      }           
90      
91      //------------------------------------------------------------------------
92      protected <Id extends Serializable, Element> IBaseDao<Id, Element>
93      createDao(Class<Element> aPersistentClass, 
94          ISessionStrategy aSessionStrategy, 
95              ITransactionStrategy aTransactionStrategy)
96      {
97          assert aPersistentClass != null : "'aPersistentClass' must not be null";
98          assert aSessionStrategy != null : "'aSessionStrategy' must not be null";
99          assert aTransactionStrategy != null : 
100             "'aTransactionStrategy' must not be null";
101         
102         return new JpaBaseDao<Id, Element>(aPersistentClass, 
103             aSessionStrategy, aTransactionStrategy);
104     }
105     
106     //------------------------------------------------------------------------
107     // members
108     //------------------------------------------------------------------------
109     private static final JpaDaoFactory INSTANCE = new JpaDaoFactory();
110 }