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 IGenericDaoFactory}
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 HibernateDaoFactory
35  extends AbstractDaoFactory
36  implements IGenericDaoFactory
37  {
38      //------------------------------------------------------------------------
39      // public
40      //------------------------------------------------------------------------
41      /**
42       * Returns Singleton instance of {@link HibernateDaoFactory}
43       * 
44       * @return Singleton instance of {@link HibernateDaoFactory}
45       */
46      public static HibernateDaoFactory
47      getInstance()
48      {
49          return INSTANCE;
50      }    
51      
52      //------------------------------------------------------------------------
53      /** 
54       * {@inheritDoc}
55       */
56      @Override
57      public <Id extends Serializable, Element> IGenericReadOnlyDao<Id, Element>
58      createReadOnlyDao(Class<Element> aPersistentClass)
59      throws IllegalArgumentException
60      {
61          IGenericReadOnlyDao<Id, Element> myGenericReadOnlyDao = createDao(
62              aPersistentClass);
63                  
64          assert myGenericReadOnlyDao != null : 
65              "myGenericReadOnlyDao must not be null";
66          
67          return myGenericReadOnlyDao;
68      }    
69      
70      //------------------------------------------------------------------------
71      /** 
72       * {@inheritDoc}
73       */
74      @SuppressWarnings("unchecked")
75      @Override
76      public <Id extends Serializable, Element> IGenericDao<Id, Element>
77      createDao(Class<Element> aPersistentClass)
78      throws IllegalArgumentException
79      {
80          IGenericDao<Id, Element> myGenericDao = (IGenericDao<Id, Element>) 
81              super.createDao(aPersistentClass);
82                  
83          assert myGenericDao != null : "myGenericDao must not be null";
84  
85          return myGenericDao;
86      }    
87          
88      //------------------------------------------------------------------------
89      // protected
90      //------------------------------------------------------------------------
91      /**
92       * Constructs {@link HibernateDaoFactory} to consume Session instances 
93       * from the given {@link ISessionStrategy} and Transaction instances from 
94       * the {@link ITransactionStrategy}
95       * 
96       * @param aSessionStrategy {@link ISessionStrategy} to consume 
97       *        Session instances from
98       * @param aTransactionStrategy {@link ITransactionStrategy} to 
99       *        consume Transaction instances from
100      * @throws IllegalArgumentException 'aDaoSessionStrategy' must not be null
101      * @throws IllegalArgumentException 'aDaoTransactionStrategy' must not 
102      *         be null
103      */
104     protected 
105     HibernateDaoFactory(ISessionStrategy aSessionStrategy, 
106         ITransactionStrategy aTransactionStrategy)
107     throws IllegalArgumentException
108     {
109         setDaoSessionStrategy(aSessionStrategy);
110         setDaoTransactionStrategy(aTransactionStrategy);
111     }           
112 
113     //------------------------------------------------------------------------
114     /**
115      * Defaults to JSE mode
116      */
117     protected
118     HibernateDaoFactory()
119     {
120         ISessionStrategy myDaoSessionStrategy = 
121             new HibernateCurrentSessionStrategy();
122         ITransactionStrategy myDaoTransactionStrategy = 
123             new HibernateConfiguredTransactionStrategy(myDaoSessionStrategy);
124 
125         setDaoSessionStrategy(myDaoSessionStrategy);
126         setDaoTransactionStrategy(myDaoTransactionStrategy);
127     }           
128     
129     //------------------------------------------------------------------------
130     // protected
131     //------------------------------------------------------------------------
132     @Override
133     protected <Id extends Serializable, Element> IGenericDao<Id, Element>
134     createDao(Class<Element> aPersistentClass, 
135         ISessionStrategy aSessionStrategy, 
136             ITransactionStrategy aTransactionStrategy)
137     {
138         assert aPersistentClass != null : "'aPersistentClass' must not be null";
139         assert aSessionStrategy != null : "'aSessionStrategy' must not be null";
140         assert aTransactionStrategy != null : 
141             "'aTransactionStrategy' must not be null";
142         
143         return new HibernateGenericDao<Id, Element>(aPersistentClass, 
144             aSessionStrategy, aTransactionStrategy);
145     }    
146     
147     //------------------------------------------------------------------------
148     // members
149     //------------------------------------------------------------------------
150     private static final HibernateDaoFactory INSTANCE = 
151         new HibernateDaoFactory();
152 }