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.apache.commons.pool2.BasePooledObjectFactory;
25 import org.apache.commons.pool2.PooledObject;
26 import org.apache.commons.pool2.PooledObjectFactory;
27 import org.apache.commons.pool2.impl.DefaultPooledObject;
28 import org.apache.commons.pool2.impl.GenericObjectPool;
29 import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
30 import org.csc.phynixx.common.exceptions.DelegatedRuntimeException;
31 import org.csc.phynixx.common.logger.IPhynixxLogger;
32 import org.csc.phynixx.common.logger.PhynixxLogManager;
33
34
35
36
37
38
39 public class PooledPhynixxManagedConnectionFactory<C extends IPhynixxConnection> extends PhynixxManagedConnectionFactory<C> {
40
41
42 private static final IPhynixxLogger LOG = PhynixxLogManager.getLogger(PooledPhynixxManagedConnectionFactory.class);
43
44 private GenericObjectPool<IPhynixxManagedConnection<C>> genericObjectPool = null;
45
46
47
48
49
50
51 private static class MyPoolableObjectFactory<X extends IPhynixxConnection> extends BasePooledObjectFactory<IPhynixxManagedConnection<X>> implements PooledObjectFactory<IPhynixxManagedConnection<X>> {
52
53 PooledPhynixxManagedConnectionFactory<X> managedConnectionFactory;
54
55 private MyPoolableObjectFactory(PooledPhynixxManagedConnectionFactory<X> managedConnectionFactory) {
56 this.managedConnectionFactory = managedConnectionFactory;
57 }
58
59 public void activateObject(PooledObject<IPhynixxManagedConnection<X>> obj) throws Exception {
60
61
62 if (LOG.isDebugEnabled()) {
63 LOG.debug("Activated " + obj);
64 }
65 obj.getObject().reopen();
66 }
67
68 public void destroyObject(PooledObject<IPhynixxManagedConnection<X>> obj) throws Exception {
69 IPhynixxManagedConnection<X> ch = obj.getObject();
70 ch.free();
71 }
72
73 @Override
74 public IPhynixxManagedConnection<X> create() throws Exception {
75 return this.managedConnectionFactory.instantiateConnection();
76 }
77
78 @Override
79 public PooledObject<IPhynixxManagedConnection<X>> wrap(IPhynixxManagedConnection<X> obj) {
80 return new DefaultPooledObject<IPhynixxManagedConnection<X>>(obj);
81 }
82
83 public void passivateObject(PooledObject<IPhynixxManagedConnection<X>> obj) throws Exception {
84 if (LOG.isDebugEnabled()) {
85 LOG.debug("Passivated " + obj.getObject());
86 }
87 obj.getObject().reset();
88 }
89
90 public boolean validateObject(PooledObject<IPhynixxManagedConnection<X>> obj) {
91 return obj.getObject() != null && !(obj.getObject().isClosed());
92 }
93
94 }
95
96 public PooledPhynixxManagedConnectionFactory() {
97 super();
98 }
99
100
101 public PooledPhynixxManagedConnectionFactory(IPhynixxConnectionFactory connectionFactory) {
102 this(connectionFactory, null);
103 }
104
105 public PooledPhynixxManagedConnectionFactory(IPhynixxConnectionFactory<C> connectionFactory,
106 GenericObjectPoolConfig genericPoolConfig) {
107 super(connectionFactory);
108 GenericObjectPoolConfig cfg = genericPoolConfig;
109 if (cfg == null) {
110 cfg = new GenericObjectPoolConfig();
111 }
112 this.genericObjectPool = new GenericObjectPool(new MyPoolableObjectFactory<C>(this), cfg);
113
114 this.setCloseStrategy(new PooledConnectionCloseStrategy<C>());
115 }
116
117
118
119
120
121
122
123 public void setGenericPoolConfig(GenericObjectPoolConfig cfg) throws Exception {
124 if (this.genericObjectPool != null) {
125 this.genericObjectPool.close();
126 }
127 if (cfg == null) {
128 cfg = new GenericObjectPoolConfig();
129 }
130 this.genericObjectPool = new GenericObjectPool(new MyPoolableObjectFactory<C>(this), cfg);
131 }
132
133 @Override
134 public IPhynixxManagedConnection<C> getManagedConnection() {
135 try {
136
137 return this.genericObjectPool.borrowObject();
138 } catch (Throwable e) {
139 throw new DelegatedRuntimeException("Instantiating new pooled Proxy", e);
140 }
141 }
142
143
144
145
146
147
148 public void releaseConnection(IPhynixxManagedConnection<C> connection) {
149 if (connection == null || !connection.hasCoreConnection()) {
150 return;
151 }
152 try {
153
154 this.genericObjectPool.returnObject(connection);
155
156
157 } catch (Exception e) {
158 throw new DelegatedRuntimeException(e);
159 }
160
161 if (LOG.isDebugEnabled()) {
162 LOG.debug("Proxy " + connection + " returned to Pool");
163 }
164 }
165
166 public void freeConnection(IPhynixxManagedConnection<C> connection) {
167 if (connection == null) {
168 return;
169 }
170 try {
171 this.genericObjectPool.invalidateObject(connection);
172 } catch (Exception e) {
173 throw new DelegatedRuntimeException(e);
174 }
175 }
176
177 public void close() {
178 try {
179 this.genericObjectPool.close();
180 super.close();
181 } catch (Exception e) {
182 throw new DelegatedRuntimeException(e);
183 }
184
185 }
186
187 public Class<C> getConnectionInterface() {
188 return this.getConnectionFactory().getConnectionInterface();
189 }
190
191
192
193
194
195
196 public void connectionReleased(IManagedConnectionEvent<C> event) {
197 IPhynixxManagedConnection<C> proxy = event.getManagedConnection();
198 if (!proxy.hasCoreConnection()) {
199 return;
200 } else {
201 this.releaseConnection(proxy);
202 }
203 if (LOG.isDebugEnabled()) {
204 LOG.debug("Proxy " + proxy + " released");
205 }
206
207 }
208
209
210
211
212 public void connectionFreed(IManagedConnectionEvent<C> event) {
213 IPhynixxManagedConnection<C> proxy = event.getManagedConnection();
214 if (!proxy.hasCoreConnection()) {
215 return;
216 } else {
217 this.freeConnection(proxy);
218 }
219 if (LOG.isDebugEnabled()) {
220 LOG.debug("Proxy " + proxy + " released");
221 }
222
223 }
224
225
226
227 }