1 package org.csc.phynixx.connection;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 import org.csc.phynixx.common.cast.ImplementorUtils;
25 import org.csc.phynixx.common.exceptions.DelegatedRuntimeException;
26 import org.csc.phynixx.common.logger.IPhynixxLogger;
27 import org.csc.phynixx.common.logger.PhynixxLogManager;
28 import org.csc.phynixx.connection.loggersystem.Dev0Strategy;
29 import org.csc.phynixx.connection.loggersystem.IPhynixxLoggerSystemStrategy;
30
31 import java.util.ArrayList;
32 import java.util.Collections;
33 import java.util.List;
34
35
36
37
38
39
40
41
42
43
44
45
46 public class PhynixxManagedConnectionFactory<C extends IPhynixxConnection> extends PhynixxManagedConnectionListenerAdapter<C> implements IPhynixxManagedConnectionFactory<C>, IPhynixxManagedConnectionListener<C> {
47
48 private static final IPhynixxLogger LOG = PhynixxLogManager.getLogger(PhynixxManagedConnectionFactory.class);
49
50 private IPhynixxConnectionFactory<C> connectionFactory = null;
51
52 private DynaPhynixxManagedConnectionFactory<C> managedConnectionFactory = null;
53 private IPhynixxLoggerSystemStrategy<C> loggerSystemStrategy = new Dev0Strategy();
54
55 private List<IPhynixxConnectionProxyDecorator<C>> managedConnectionDecorators = new ArrayList<IPhynixxConnectionProxyDecorator<C>>();
56
57
58 private boolean autocommitAware= true;
59
60 private boolean synchronizeConnection= true;
61
62 private final AutoCommitDecorator<C> autcommitDecorator=new AutoCommitDecorator<C>();
63
64 private CloseStrategy<C> closeStrategy;
65
66 public PhynixxManagedConnectionFactory() {
67 }
68
69 public PhynixxManagedConnectionFactory(IPhynixxConnectionFactory<C> connectionFactory) {
70 this.setConnectionFactory(connectionFactory);
71 this.setCloseStrategy(new UnpooledConnectionCloseStrategy<C>());
72 }
73
74 public boolean isAutocommitAware() {
75 return autocommitAware;
76 }
77
78 public void setAutocommitAware(boolean autocommitAware) {
79 this.autocommitAware = autocommitAware;
80 }
81
82 public boolean isSynchronizeConnection() {
83 return synchronizeConnection;
84 }
85
86 public void setSynchronizeConnection(boolean synchronizeConnection) {
87 this.synchronizeConnection = synchronizeConnection;
88 }
89
90
91 void setCloseStrategy(CloseStrategy<C> closeStrategy) {
92 this.closeStrategy= closeStrategy;
93 }
94
95 public void setConnectionFactory(IPhynixxConnectionFactory<C> connectionFactory) {
96
97 if(this.connectionFactory!=null) {
98 this.connectionFactory.close();
99 this.connectionFactory=null;
100 }
101
102
103 this.connectionFactory = connectionFactory;
104 }
105
106 private void createManagedConnectionFactory() {
107
108 if(this.getConnectionFactory()==null) {
109 throw new IllegalStateException("Connection Factory has to be defined");
110 }
111
112 if(this.closeStrategy==null) {
113 throw new IllegalStateException("CloseStrategy has to be defined");
114 }
115
116 this.managedConnectionFactory =
117 new DynaPhynixxManagedConnectionFactory<C>(this.connectionFactory.getConnectionInterface(), this.closeStrategy, this.isSynchronizeConnection());
118 }
119
120
121 public IPhynixxConnectionFactory<C> getConnectionFactory() {
122 return connectionFactory;
123 }
124
125 public List<IPhynixxConnectionProxyDecorator<C>> getManagedConnectionDecorators() {
126 return Collections.unmodifiableList(managedConnectionDecorators);
127 }
128
129 public void addManagedConnectionDecorator(
130 IPhynixxConnectionProxyDecorator<C> connectionProxyDecorator) {
131 this.managedConnectionDecorators.add(connectionProxyDecorator);
132 }
133
134 public IPhynixxLoggerSystemStrategy<C> getLoggerSystemStrategy() {
135 return loggerSystemStrategy;
136 }
137
138 public void setLoggerSystemStrategy(IPhynixxLoggerSystemStrategy<C> loggerSystemStrategy) {
139 this.loggerSystemStrategy = loggerSystemStrategy;
140 }
141
142 @Override
143 public C getConnection() {
144 return ImplementorUtils.cast(getManagedConnection(), getConnectionInterface());
145 }
146
147 @Override
148 public IPhynixxManagedConnection<C> getManagedConnection() {
149 return this.instantiateConnection();
150 }
151
152
153 protected IPhynixxManagedConnection<C> instantiateConnection() {
154 try {
155 IPhynixxManagedConnection<C> managedConnection;
156 try {
157
158
159 C connection = PhynixxManagedConnectionFactory.this.getConnectionFactory().getConnection();
160
161
162
163
164 managedConnection = getManagedConnectionFactory().getManagedConnection(connection);
165
166
167
168
169 managedConnection.addConnectionListener(PhynixxManagedConnectionFactory.this);
170
171 managedConnection.setSynchronized(this.isSynchronizeConnection());
172
173 if (PhynixxManagedConnectionFactory.this.loggerSystemStrategy != null) {
174 managedConnection = PhynixxManagedConnectionFactory.this.loggerSystemStrategy.decorate(managedConnection);
175 }
176
177 for (IPhynixxConnectionProxyDecorator<C> decorators : this.managedConnectionDecorators) {
178 managedConnection = decorators.decorate(managedConnection);
179 }
180
181
182 if(this.isAutocommitAware()) {
183 this.autcommitDecorator.decorate(managedConnection);
184 }
185
186
187 managedConnection.reset();
188 } catch (ClassCastException e) {
189 e.printStackTrace();
190 throw new DelegatedRuntimeException(e);
191 }
192
193 return managedConnection;
194
195 } catch (Throwable e) {
196 throw new DelegatedRuntimeException("Instantiating new pooled Proxy", e);
197 }
198 }
199
200 private DynaPhynixxManagedConnectionFactory<C> getManagedConnectionFactory() {
201
202 if(this.managedConnectionFactory==null) {
203 this.createManagedConnectionFactory();
204 }
205 return PhynixxManagedConnectionFactory.this.managedConnectionFactory;
206 }
207
208
209 public Class<C> getConnectionInterface() {
210 return this.getConnectionFactory().getConnectionInterface();
211 }
212
213
214 @Override
215 public void close() {
216 try {
217 if (this.loggerSystemStrategy != null) {
218 this.loggerSystemStrategy.close();
219 }
220 } catch (Exception e) {
221 throw new DelegatedRuntimeException(e);
222 }
223
224 }
225
226
227
228
229 public void connectionReleased(IManagedConnectionEvent<C> event) {
230 IPhynixxManagedConnection<C> proxy = event.getManagedConnection();
231 if (LOG.isDebugEnabled()) {
232 LOG.debug("Proxy " + proxy + " released");
233 }
234
235 }
236
237 @Override
238 public void connectionFreed(IManagedConnectionEvent<C> event) {
239 IPhynixxManagedConnection<C> proxy = event.getManagedConnection();
240 if (LOG.isDebugEnabled()) {
241 LOG.debug("Proxy " + proxy + " set free");
242 }
243 }
244 }