001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.jca.support;
023:
024: import javax.management.Attribute;
025: import javax.management.MBeanServerConnection;
026: import javax.management.ObjectName;
027: import javax.resource.spi.ManagedConnectionFactory;
028:
029: import org.jboss.logging.Logger;
030: import org.jboss.resource.connectionmanager.BaseConnectionManager2;
031: import org.jboss.resource.connectionmanager.CachedConnectionManager;
032: import org.jboss.resource.connectionmanager.InternalManagedConnectionPool;
033: import org.jboss.resource.connectionmanager.JBossManagedConnectionPool;
034: import org.jboss.resource.connectionmanager.ManagedConnectionPool;
035:
036: /**
037: * A PoolHelper.
038: *
039: * @author <a href="weston.price@jboss.com">Weston Price</a>
040: * @version $Revision: 57211 $
041: */
042: public class PoolHelper {
043: static Logger log = Logger.getLogger(PoolHelper.class);
044:
045: public static final String POOL_ATT_BLOCK_TIME = "BlockingTimeoutMillis";
046: public static final String POOL_ATT_BACKGROUND_VAL_MIN = "BackGroundValidationMinutes";
047: public static final String POOL_ATT_PREFILL = "PreFill";
048: public static final String POOL_ATT_MIN_CONN_COUNT = "MinSize";
049: public static final String POOL_ATT_DESTROYED_COUNT = "ConnectionDestroyedCount";
050: public static final String POOL_ATT_CONN_COUNT = "ConnectionCount";
051: public static final String POOL_ATT_BACKGROUND_VAL = "BackGroundValidation";
052:
053: private MBeanServerConnection server;
054: private ObjectName poolName;
055:
056: private static final int DEFAULT_MIN = 0;
057:
058: private static final int DEFAULT_MAX = 20;
059:
060: private static final int DEFAULT_BLOCK = 1000;
061:
062: private static final long DEFAULT_IDLE = 1000;
063:
064: private static final boolean DEFAULT_PREFILL = false;
065:
066: private static final PoolType DEFAULT_POOL_TYPE = PoolType.ONE_POOL;
067:
068: private PoolHelper(MBeanServerConnection server, ObjectName poolName) {
069: this .server = server;
070: this .poolName = poolName;
071:
072: }
073:
074: public static PoolHelper getInstance(MBeanServerConnection server) {
075:
076: return new PoolHelper(server, null);
077:
078: }
079:
080: public static PoolHelper getInstance(MBeanServerConnection server,
081: ObjectName poolName) {
082: return new PoolHelper(server, poolName);
083:
084: }
085:
086: public static boolean comparePoolValues(
087: MBeanServerConnection server, ObjectName poolName,
088: String firstAtt, String secondAtt) throws Exception {
089:
090: Object first = getAttribute(server, poolName, firstAtt);
091: Object second = getAttribute(server, poolName, secondAtt);
092: return first.equals(second);
093:
094: }
095:
096: public Object getAttribute(String attribute) throws Exception {
097:
098: return getAttribute(poolName, attribute);
099:
100: }
101:
102: public Object getAttribute(ObjectName name, String attribute)
103: throws Exception {
104:
105: return server.getAttribute(name, attribute);
106:
107: }
108:
109: // public static Integer getConnCount(MBeanServerConnection server, ObjectName poolName) throws Exception
110: // {
111: //
112: //
113: // }
114:
115: public static void sleepForValidation(long millis) throws Exception {
116: Thread.sleep(millis);
117:
118: }
119:
120: public static Integer getConnectionCount(
121: MBeanServerConnection server, ObjectName poolName)
122: throws Exception {
123: return (Integer) getAttribute(server, poolName,
124: POOL_ATT_CONN_COUNT);
125:
126: }
127:
128: public Integer getDestroyed() throws Exception {
129: return getDestroyed(server, poolName);
130:
131: }
132:
133: public static Integer getDestroyed(MBeanServerConnection server,
134: ObjectName poolName) throws Exception {
135:
136: return (Integer) getAttribute(server, poolName,
137: POOL_ATT_DESTROYED_COUNT);
138:
139: }
140:
141: public Integer getMinSize() throws Exception {
142: return getMinSize(server, poolName);
143:
144: }
145:
146: public static Integer getMinSize(MBeanServerConnection server,
147: ObjectName poolName) throws Exception {
148:
149: return (Integer) getAttribute(server, poolName,
150: POOL_ATT_MIN_CONN_COUNT);
151:
152: }
153:
154: public Long getBackgroundValMinutes() throws Exception {
155: return getBackgroundValMinutes(server, poolName);
156:
157: }
158:
159: public static Long getBackgroundValMinutes(
160: MBeanServerConnection server, ObjectName poolName)
161: throws Exception {
162: return (Long) getAttribute(server, poolName,
163: POOL_ATT_BACKGROUND_VAL_MIN);
164:
165: }
166:
167: public static Integer getBlockingTimeout(
168: MBeanServerConnection server, ObjectName poolName)
169: throws Exception {
170: return (Integer) getAttribute(server, poolName,
171: POOL_ATT_BLOCK_TIME);
172:
173: }
174:
175: public void setPoolAttributeAndFlush(String attName, Object attValue)
176: throws Exception {
177: setPoolAttributeAndFlush(server, poolName, attName, attValue);
178:
179: }
180:
181: public static void setPoolAttributeAndFlush(
182: MBeanServerConnection server, ObjectName pool,
183: String attName, Object attValue) throws Exception {
184:
185: setAttribute(server, pool, attName, attValue);
186: flush(server, pool);
187:
188: }
189:
190: public static Object getAttribute(MBeanServerConnection server,
191: ObjectName poolName, String attName) throws Exception {
192: log.debug("Getting pool attribute " + attName);
193: Object result = server.getAttribute(poolName, attName);
194: log.debug("Retrieved pool attribute " + attName
195: + " with value " + result);
196:
197: return result;
198: }
199:
200: public static void setAttribute(MBeanServerConnection server,
201: ObjectName objectName, String attName, Object attValue)
202: throws Exception {
203:
204: server.setAttribute(objectName,
205: new Attribute(attName, attValue));
206:
207: }
208:
209: public Integer getConnectionCount() throws Exception {
210: return getConnectionCount(server, poolName);
211:
212: }
213:
214: public static void flush(MBeanServerConnection server,
215: ObjectName pool) throws Exception {
216: server.invoke(pool, "flush", new Object[0], new String[0]);
217:
218: }
219:
220: public static InternalManagedConnectionPool.PoolParams getPoolParams() {
221:
222: InternalManagedConnectionPool.PoolParams params = new InternalManagedConnectionPool.PoolParams();
223: params.minSize = DEFAULT_MIN;
224: params.maxSize = DEFAULT_MAX;
225: params.blockingTimeout = DEFAULT_BLOCK;
226: params.idleTimeout = DEFAULT_IDLE;
227: params.prefill = DEFAULT_PREFILL;
228: return params;
229:
230: }
231:
232: public static InternalManagedConnectionPool.PoolParams getPoolParams(
233: boolean prefill) {
234:
235: return getPoolParams(DEFAULT_MIN, DEFAULT_MAX, DEFAULT_BLOCK,
236: DEFAULT_IDLE, prefill);
237: }
238:
239: public static InternalManagedConnectionPool.PoolParams getPoolParams(
240: long idleTimeout, boolean prefill) {
241:
242: return getPoolParams(DEFAULT_MIN, DEFAULT_MAX, DEFAULT_BLOCK,
243: idleTimeout, prefill);
244: }
245:
246: public static InternalManagedConnectionPool.PoolParams getPoolParams(
247: int blockingTimeout, long idleTimeout, boolean prefill) {
248:
249: return getPoolParams(DEFAULT_MIN, DEFAULT_MAX, blockingTimeout,
250: idleTimeout, prefill);
251: }
252:
253: public static InternalManagedConnectionPool.PoolParams getPoolParams(
254: int maxSize, int blockingTimeout, long idleTimeout,
255: boolean prefill) {
256:
257: return getPoolParams(DEFAULT_MIN, maxSize, blockingTimeout,
258: idleTimeout, prefill);
259: }
260:
261: public static InternalManagedConnectionPool.PoolParams getPoolParams(
262: int minSize, int maxSize, int blockingTimeout,
263: long idleTimeout, boolean prefill) {
264:
265: InternalManagedConnectionPool.PoolParams params = getPoolParams();
266: params.minSize = minSize;
267: params.maxSize = maxSize;
268: params.blockingTimeout = blockingTimeout;
269: params.idleTimeout = idleTimeout;
270: params.prefill = prefill;
271: return params;
272:
273: }
274:
275: public static ManagedConnectionFactory getManagedConnectionFactory(
276: String className) throws Exception {
277:
278: return getManagedConnectionFactory(Class.forName(className));
279: }
280:
281: public static ManagedConnectionFactory getManagedConnectionFactory(
282: Class clazz) throws Exception {
283:
284: return (ManagedConnectionFactory) clazz.newInstance();
285:
286: }
287:
288: public static ManagedConnectionPool getManagedConnectionPool(
289: int minSize, int maxSize, int blockingTimeout,
290: long idleTimeout, boolean prefill,
291: ManagedConnectionFactory mcf, boolean noTxnSeperatePool,
292: Logger log) {
293:
294: InternalManagedConnectionPool.PoolParams pp = getPoolParams(
295: minSize, maxSize, blockingTimeout, idleTimeout, prefill);
296: return getManagedConnectionPool(DEFAULT_POOL_TYPE, mcf,
297: noTxnSeperatePool, pp, log);
298: }
299:
300: public static ManagedConnectionPool getManagedConnectionPool(
301: PoolType type, ManagedConnectionFactory mcf,
302: boolean noTxnSeperatePool,
303: InternalManagedConnectionPool.PoolParams pp, Logger log) {
304:
305: ManagedConnectionPool mcp = null;
306:
307: if (type.equals(PoolType.ONE_POOL)) {
308:
309: mcp = new JBossManagedConnectionPool.OnePool(mcf, pp,
310: noTxnSeperatePool, log);
311:
312: } else if (type.equals(PoolType.CRI_POOL)) {
313:
314: mcp = new JBossManagedConnectionPool.PoolByCri(mcf, pp,
315: noTxnSeperatePool, log);
316:
317: } else if (type.equals(PoolType.SUB_POOL)) {
318:
319: mcp = new JBossManagedConnectionPool.PoolBySubject(mcf, pp,
320: noTxnSeperatePool, log);
321:
322: } else if (type.equals(PoolType.SUB_CRI_POOL)) {
323:
324: mcp = new JBossManagedConnectionPool.PoolBySubjectAndCri(
325: mcf, pp, noTxnSeperatePool, log);
326:
327: }
328:
329: return mcp;
330: }
331:
332: public static BaseConnectionManager2 getCM() {
333:
334: return null;
335: }
336:
337: public static CachedConnectionManager getCachedConnectionManager() {
338:
339: return new CachedConnectionManager();
340:
341: }
342:
343: public static class PoolType {
344:
345: private final int type;
346:
347: private PoolType(int type) {
348:
349: this .type = type;
350: }
351:
352: public static final PoolType ONE_POOL = new PoolType(0);
353:
354: public static final PoolType CRI_POOL = new PoolType(1);
355:
356: public static final PoolType SUB_POOL = new PoolType(2);
357:
358: public static final PoolType SUB_CRI_POOL = new PoolType(3);
359:
360: }
361: }
|