View Javadoc

1   package org.csc.phynixx.connection;
2   
3   /*
4    * #%L
5    * phynixx-connection
6    * %%
7    * Copyright (C) 2014 csc
8    * %%
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * #L%
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   * Factory pools the pure connection. before delivering the connection it ist decorate by the according to {@link PhynixxManagedConnectionFactory}.
36   *
37   * @param <C> Typ of the pure connection
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       * implementation of the contract of generic pools to manage pooled elements
48       *
49       * @author christoph
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              // opens the connection
61              //bobj.getObject().reset();
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      * closes the current pool -if existing- and instanciates a new pool
119      *
120      * @param cfg
121      * @throws Exception
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             // System.out.println(Thread.currentThread()+"borrowObject "+this.genericObjectPool.getNumIdle()+"/"+this.genericObjectPool.getNumActive()+ "(Waiters "+this.genericObjectPool.getNumWaiters()+")");
137             return this.genericObjectPool.borrowObject();
138         } catch (Throwable e) {
139             throw new DelegatedRuntimeException("Instantiating new pooled Proxy", e);
140         }
141     }
142 
143     /**
144      * closes the connection an releases it to the pool
145      *
146      * @param connection
147      */
148     public void releaseConnection(IPhynixxManagedConnection<C> connection) {
149         if (connection == null || !connection.hasCoreConnection()) {
150             return;
151         }
152         try {
153             //StringBuilder builder= new StringBuilder(Thread.currentThread().getId()+".releaseConnection starting from "+this.genericObjectPool.getNumIdle()+"/"+this.genericObjectPool.getNumActive());
154             this.genericObjectPool.returnObject(connection);
155             // LOG.debug(builder.append(" ... to "+Thread.currentThread().getId()+"   "+this.genericObjectPool.getNumIdle()+"/"+this.genericObjectPool.getNumActive()).toString());
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      * the connection is sent back to the pool
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      * the connection is set free an released from the pool
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 }