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 public void
48 setDaoSessionStrategy(ISessionStrategy aSessionStrategy)
49 throws IllegalArgumentException
50 {
51 Validate.notNull(aSessionStrategy,
52 "'aDaoSessionStrategy' must not be null");
53
54 // shutdown existing
55 if (theSessionStrategy != null)
56 {
57 shutdown();
58 }
59
60 theSessionStrategy = aSessionStrategy;
61 }
62
63 //------------------------------------------------------------------------
64 /**
65 * {@inheritDoc}
66 */
67 public void
68 setDaoTransactionStrategy(ITransactionStrategy aTransactionStrategy)
69 throws IllegalArgumentException
70 {
71 Validate.notNull(aTransactionStrategy,
72 "'aTransactionFactory' must not be null");
73
74 theTransactionStrategy = aTransactionStrategy;
75 }
76
77 //------------------------------------------------------------------------
78 /**
79 * {@inheritDoc}
80 */
81 public void
82 shutdown()
83 throws DaoException
84 {
85 assert theSessionStrategy != null :
86 "'theSessionStrategy' must not be null";
87
88 theSessionStrategy.shutdown();
89
90 theSessionStrategy = null;
91 }
92
93 //------------------------------------------------------------------------
94 /**
95 * {@inheritDoc}
96 */
97 @SuppressWarnings("unchecked")
98 public <Id extends Serializable, Element> IBaseReadOnlyDao<Id, Element>
99 createReadOnlyDao(Class<Element> aPersistentClass)
100 throws IllegalArgumentException
101 {
102 Validate.notNull(aPersistentClass,
103 "'aPersistentClass' must not be null");
104 Validate.isTrue(!aPersistentClass.isInterface(),
105 "'aPersistentClass' must be a class type");
106
107 IBaseReadOnlyDao<Id, Element> myBaseReadOnlyDao = createDao(
108 aPersistentClass);
109
110 assert myBaseReadOnlyDao != null :
111 "myBaseReadOnlyDao must not be null";
112
113 return myBaseReadOnlyDao;
114 }
115
116 //------------------------------------------------------------------------
117 /**
118 * {@inheritDoc}
119 */
120 @SuppressWarnings("unchecked")
121 public <Id extends Serializable, Element> IBaseDao<Id, Element>
122 createDao(Class<Element> aPersistentClass)
123 throws IllegalArgumentException
124 {
125 Validate.notNull(aPersistentClass,
126 "'aPersistentClass' must not be null");
127 Validate.isTrue(!aPersistentClass.getClass().isInterface(),
128 "'aPersistentClass' must be a class type");
129
130 IBaseDao<Id, Element> myBaseDao = null;
131
132 if (theBaseDaoMap.containsKey(aPersistentClass))
133 {
134 // just reuse the existing instance
135 myBaseDao = (IBaseDao<Id, Element>) theBaseDaoMap.get(
136 aPersistentClass);
137 }
138 else
139 {
140 // create new instance and make it available for future calls
141 myBaseDao = createDao(aPersistentClass,
142 theSessionStrategy, theTransactionStrategy);
143 theBaseDaoMap.put(aPersistentClass, myBaseDao);
144 }
145
146 assert myBaseDao != null : "myBaseDao must not be null";
147
148 return myBaseDao;
149 }
150
151 //------------------------------------------------------------------------
152 // protected
153 //------------------------------------------------------------------------
154 protected
155 AbstractDaoFactory()
156 {
157 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 }