1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
32
33
34
35
36
37 public abstract
38 class AbstractDaoFactory
39 implements IBaseDaoFactory, ISingleton
40 {
41
42
43
44
45
46
47 public void
48 setDaoSessionStrategy(ISessionStrategy aSessionStrategy)
49 throws IllegalArgumentException
50 {
51 Validate.notNull(aSessionStrategy,
52 "'aDaoSessionStrategy' must not be null");
53
54
55 if (theSessionStrategy != null)
56 {
57 shutdown();
58 }
59
60 theSessionStrategy = aSessionStrategy;
61 }
62
63
64
65
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
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
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
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
135 myBaseDao = (IBaseDao<Id, Element>) theBaseDaoMap.get(
136 aPersistentClass);
137 }
138 else
139 {
140
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
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
168
169 private ISessionStrategy theSessionStrategy;
170 private ITransactionStrategy theTransactionStrategy;
171 private final Map<Class<?>, IBaseDao<?, ?>> theBaseDaoMap;
172 }