KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jca > support > PoolHelper


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2005, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software 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 GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.test.jca.support;
23
24 import javax.management.Attribute JavaDoc;
25 import javax.management.MBeanServerConnection JavaDoc;
26 import javax.management.ObjectName JavaDoc;
27 import javax.resource.spi.ManagedConnectionFactory JavaDoc;
28
29 import org.jboss.logging.Logger;
30 import org.jboss.resource.connectionmanager.BaseConnectionManager2;
31 import org.jboss.resource.connectionmanager.CachedConnectionManager;
32 import org.jboss.resource.connectionmanager.InternalManagedConnectionPool;
33 import org.jboss.resource.connectionmanager.JBossManagedConnectionPool;
34 import org.jboss.resource.connectionmanager.ManagedConnectionPool;
35
36 /**
37  * A PoolHelper.
38  *
39  * @author <a HREF="weston.price@jboss.com">Weston Price</a>
40  * @version $Revision: 45412 $
41  */

42 public class PoolHelper
43 {
44    static Logger log = Logger.getLogger(PoolHelper.class);
45    
46    public static final String JavaDoc POOL_ATT_BLOCK_TIME = "BlockingTimeoutMillis";
47    public static final String JavaDoc POOL_ATT_BACKGROUND_VAL_MIN = "BackGroundValidationMinutes";
48    public static final String JavaDoc POOL_ATT_PREFILL = "PreFill";
49    public static final String JavaDoc POOL_ATT_MIN_CONN_COUNT = "MinSize";
50    public static final String JavaDoc POOL_ATT_DESTROYED_COUNT = "ConnectionDestroyedCount";
51    public static final String JavaDoc POOL_ATT_CONN_COUNT = "ConnectionCount";
52    public static final String JavaDoc POOL_ATT_BACKGROUND_VAL = "BackGroundValidation";
53    
54    
55    private MBeanServerConnection JavaDoc server;
56    private ObjectName JavaDoc poolName;
57    
58    private static final int DEFAULT_MIN = 0;
59
60    private static final int DEFAULT_MAX = 20;
61
62    private static final int DEFAULT_BLOCK = 1000;
63
64    private static final long DEFAULT_IDLE = 1000;
65
66    private static final boolean DEFAULT_PREFILL = false;
67
68    private static final PoolType DEFAULT_POOL_TYPE = PoolType.ONE_POOL;
69
70    private PoolHelper(MBeanServerConnection JavaDoc server, ObjectName JavaDoc poolName)
71    {
72       this.server = server;
73       this.poolName = poolName;
74       
75    }
76
77    public static PoolHelper getInstance(MBeanServerConnection JavaDoc server)
78    {
79       
80       return new PoolHelper(server, null);
81          
82    }
83    
84    public static PoolHelper getInstance(MBeanServerConnection JavaDoc server, ObjectName JavaDoc poolName)
85    {
86       return new PoolHelper(server, poolName);
87       
88    }
89    public static boolean comparePoolValues(MBeanServerConnection JavaDoc server, ObjectName JavaDoc poolName, String JavaDoc firstAtt, String JavaDoc secondAtt) throws Exception JavaDoc
90    {
91       
92       Object JavaDoc first = getAttribute(server, poolName, firstAtt);
93       Object JavaDoc second = getAttribute(server, poolName, secondAtt);
94       return first.equals(second);
95       
96       
97    }
98    
99    public Object JavaDoc getAttribute(String JavaDoc attribute) throws Exception JavaDoc
100    {
101       
102       return getAttribute(poolName, attribute);
103       
104       
105    }
106
107    public Object JavaDoc getAttribute(ObjectName JavaDoc name, String JavaDoc attribute) throws Exception JavaDoc
108    {
109       
110       return server.getAttribute(name, attribute);
111       
112    }
113    
114 // public static Integer getConnCount(MBeanServerConnection server, ObjectName poolName) throws Exception
115
// {
116
//
117
//
118
// }
119

120    public static void sleepForValidation(long millis) throws Exception JavaDoc
121    {
122       Thread.sleep(millis);
123       
124    }
125    
126    public static Integer JavaDoc getConnectionCount(MBeanServerConnection JavaDoc server, ObjectName JavaDoc poolName) throws Exception JavaDoc
127    {
128       return (Integer JavaDoc)getAttribute(server, poolName, POOL_ATT_CONN_COUNT);
129       
130    }
131    
132    public Integer JavaDoc getDestroyed() throws Exception JavaDoc
133    {
134       return getDestroyed(server, poolName);
135       
136    }
137
138    public static Integer JavaDoc getDestroyed(MBeanServerConnection JavaDoc server, ObjectName JavaDoc poolName) throws Exception JavaDoc
139    {
140
141       return (Integer JavaDoc)getAttribute(server, poolName, POOL_ATT_DESTROYED_COUNT);
142       
143    }
144    
145    public Integer JavaDoc getMinSize() throws Exception JavaDoc
146    {
147       return getMinSize(server, poolName);
148       
149    }
150    
151    public static Integer JavaDoc getMinSize(MBeanServerConnection JavaDoc server, ObjectName JavaDoc poolName) throws Exception JavaDoc
152    {
153       
154       return (Integer JavaDoc)getAttribute(server, poolName, POOL_ATT_MIN_CONN_COUNT);
155       
156    }
157    
158    public Long JavaDoc getBackgroundValMinutes() throws Exception JavaDoc
159    {
160       return getBackgroundValMinutes(server, poolName);
161       
162    }
163    
164    public static Long JavaDoc getBackgroundValMinutes(MBeanServerConnection JavaDoc server, ObjectName JavaDoc poolName) throws Exception JavaDoc
165    {
166       return (Long JavaDoc)getAttribute(server, poolName, POOL_ATT_BACKGROUND_VAL_MIN);
167       
168    }
169    
170    
171    public static Integer JavaDoc getBlockingTimeout(MBeanServerConnection JavaDoc server, ObjectName JavaDoc poolName) throws Exception JavaDoc
172    {
173       return (Integer JavaDoc)getAttribute(server, poolName, POOL_ATT_BLOCK_TIME);
174      
175    }
176    
177    public void setPoolAttributeAndFlush(String JavaDoc attName, Object JavaDoc attValue) throws Exception JavaDoc
178    {
179       setPoolAttributeAndFlush(server, poolName, attName, attValue);
180       
181    }
182
183    public static void setPoolAttributeAndFlush(MBeanServerConnection JavaDoc server, ObjectName JavaDoc pool, String JavaDoc attName, Object JavaDoc attValue) throws Exception JavaDoc
184    {
185       
186       setAttribute(server, pool, attName, attValue);
187       flush(server, pool);
188       
189       
190    }
191    public static Object JavaDoc getAttribute(MBeanServerConnection JavaDoc server, ObjectName JavaDoc poolName, String JavaDoc attName) throws Exception JavaDoc
192    {
193       log.debug("Getting pool attribute " + attName);
194       Object JavaDoc result = server.getAttribute(poolName, attName);
195       log.debug("Retrieved pool attribute " + attName + " with value " + result);
196       
197       return result;
198    }
199
200    public static void setAttribute(MBeanServerConnection JavaDoc server, ObjectName JavaDoc objectName, String JavaDoc attName, Object JavaDoc attValue) throws Exception JavaDoc
201    {
202      
203       server.setAttribute(objectName, new Attribute JavaDoc(attName, attValue));
204       
205    }
206    
207    public Integer JavaDoc getConnectionCount() throws Exception JavaDoc
208    {
209      return getConnectionCount(server, poolName);
210      
211    }
212    public static void flush(MBeanServerConnection JavaDoc server, ObjectName JavaDoc pool) throws Exception JavaDoc
213    {
214       server.invoke(pool, "flush", new Object JavaDoc[0], new String JavaDoc[0]);
215       
216    }
217    public static InternalManagedConnectionPool.PoolParams getPoolParams()
218    {
219
220       InternalManagedConnectionPool.PoolParams params = new InternalManagedConnectionPool.PoolParams();
221       params.minSize = DEFAULT_MIN;
222       params.maxSize = DEFAULT_MAX;
223       params.blockingTimeout = DEFAULT_BLOCK;
224       params.idleTimeout = DEFAULT_IDLE;
225       params.prefill = DEFAULT_PREFILL;
226       return params;
227
228    }
229
230    public static InternalManagedConnectionPool.PoolParams getPoolParams(boolean prefill)
231    {
232
233       return getPoolParams(DEFAULT_MIN, DEFAULT_MAX, DEFAULT_BLOCK, DEFAULT_IDLE, prefill);
234    }
235
236    public static InternalManagedConnectionPool.PoolParams getPoolParams(long idleTimeout, boolean prefill)
237    {
238
239       return getPoolParams(DEFAULT_MIN, DEFAULT_MAX, DEFAULT_BLOCK, idleTimeout, prefill);
240    }
241
242    public static InternalManagedConnectionPool.PoolParams getPoolParams(int blockingTimeout, long idleTimeout,
243          boolean prefill)
244    {
245
246       return getPoolParams(DEFAULT_MIN, DEFAULT_MAX, blockingTimeout, idleTimeout, prefill);
247    }
248
249    public static InternalManagedConnectionPool.PoolParams getPoolParams(int maxSize, int blockingTimeout,
250          long idleTimeout, boolean prefill)
251    {
252
253       return getPoolParams(DEFAULT_MIN, maxSize, blockingTimeout, idleTimeout, prefill);
254    }
255
256    public static InternalManagedConnectionPool.PoolParams getPoolParams(int minSize, int maxSize, int blockingTimeout,
257          long idleTimeout, boolean prefill)
258    {
259
260       InternalManagedConnectionPool.PoolParams params = getPoolParams();
261       params.minSize = minSize;
262       params.maxSize = maxSize;
263       params.blockingTimeout = blockingTimeout;
264       params.idleTimeout = idleTimeout;
265       params.prefill = prefill;
266       return params;
267
268    }
269
270    public static ManagedConnectionFactory JavaDoc getManagedConnectionFactory(String JavaDoc className) throws Exception JavaDoc
271    {
272
273       return getManagedConnectionFactory(Class.forName(className));
274    }
275
276    public static ManagedConnectionFactory JavaDoc getManagedConnectionFactory(Class JavaDoc clazz) throws Exception JavaDoc
277    {
278
279       return (ManagedConnectionFactory JavaDoc) clazz.newInstance();
280
281    }
282
283    public static ManagedConnectionPool getManagedConnectionPool(int minSize, int maxSize, int blockingTimeout, long idleTimeout, boolean prefill, ManagedConnectionFactory JavaDoc mcf, boolean noTxnSeperatePool, Logger log){
284       
285       InternalManagedConnectionPool.PoolParams pp = getPoolParams(minSize, maxSize, blockingTimeout, idleTimeout, prefill);
286       return getManagedConnectionPool(DEFAULT_POOL_TYPE, mcf, noTxnSeperatePool, pp, log);
287    }
288    
289    
290    public static ManagedConnectionPool getManagedConnectionPool(PoolType type, ManagedConnectionFactory JavaDoc mcf,
291          boolean noTxnSeperatePool, InternalManagedConnectionPool.PoolParams pp, Logger log)
292    {
293
294       ManagedConnectionPool mcp = null;
295
296       if (type.equals(PoolType.ONE_POOL))
297       {
298
299          mcp = new JBossManagedConnectionPool.OnePool(mcf, pp, noTxnSeperatePool, log);
300
301       }
302       else if (type.equals(PoolType.CRI_POOL))
303       {
304
305          mcp = new JBossManagedConnectionPool.PoolByCri(mcf, pp, noTxnSeperatePool, log);
306
307       }
308       else if (type.equals(PoolType.SUB_POOL))
309       {
310
311          mcp = new JBossManagedConnectionPool.PoolBySubject(mcf, pp, noTxnSeperatePool, log);
312
313       }
314       else if (type.equals(PoolType.SUB_CRI_POOL))
315       {
316
317          mcp = new JBossManagedConnectionPool.PoolBySubjectAndCri(mcf, pp, noTxnSeperatePool, log);
318
319       }
320
321       return mcp;
322    }
323
324    public static BaseConnectionManager2 getCM()
325    {
326
327       return null;
328    }
329
330    public static CachedConnectionManager getCachedConnectionManager(){
331       
332       return new CachedConnectionManager();
333       
334    }
335    
336    public static class PoolType
337    {
338
339       private final int type;
340
341       private PoolType(int type)
342       {
343
344          this.type = type;
345       }
346
347       public static final PoolType ONE_POOL = new PoolType(0);
348
349       public static final PoolType CRI_POOL = new PoolType(1);
350
351       public static final PoolType SUB_POOL = new PoolType(2);
352
353       public static final PoolType SUB_CRI_POOL = new PoolType(3);
354
355    }
356 }
Popular Tags