1 22 package org.jboss.test.jca.support; 23 24 import javax.management.Attribute ; 25 import javax.management.MBeanServerConnection ; 26 import javax.management.ObjectName ; 27 import javax.resource.spi.ManagedConnectionFactory ; 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 42 public class PoolHelper 43 { 44 static Logger log = Logger.getLogger(PoolHelper.class); 45 46 public static final String POOL_ATT_BLOCK_TIME = "BlockingTimeoutMillis"; 47 public static final String POOL_ATT_BACKGROUND_VAL_MIN = "BackGroundValidationMinutes"; 48 public static final String POOL_ATT_PREFILL = "PreFill"; 49 public static final String POOL_ATT_MIN_CONN_COUNT = "MinSize"; 50 public static final String POOL_ATT_DESTROYED_COUNT = "ConnectionDestroyedCount"; 51 public static final String POOL_ATT_CONN_COUNT = "ConnectionCount"; 52 public static final String POOL_ATT_BACKGROUND_VAL = "BackGroundValidation"; 53 54 55 private MBeanServerConnection server; 56 private ObjectName 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 server, ObjectName poolName) 71 { 72 this.server = server; 73 this.poolName = poolName; 74 75 } 76 77 public static PoolHelper getInstance(MBeanServerConnection server) 78 { 79 80 return new PoolHelper(server, null); 81 82 } 83 84 public static PoolHelper getInstance(MBeanServerConnection server, ObjectName poolName) 85 { 86 return new PoolHelper(server, poolName); 87 88 } 89 public static boolean comparePoolValues(MBeanServerConnection server, ObjectName poolName, String firstAtt, String secondAtt) throws Exception 90 { 91 92 Object first = getAttribute(server, poolName, firstAtt); 93 Object second = getAttribute(server, poolName, secondAtt); 94 return first.equals(second); 95 96 97 } 98 99 public Object getAttribute(String attribute) throws Exception 100 { 101 102 return getAttribute(poolName, attribute); 103 104 105 } 106 107 public Object getAttribute(ObjectName name, String attribute) throws Exception 108 { 109 110 return server.getAttribute(name, attribute); 111 112 } 113 114 120 public static void sleepForValidation(long millis) throws Exception 121 { 122 Thread.sleep(millis); 123 124 } 125 126 public static Integer getConnectionCount(MBeanServerConnection server, ObjectName poolName) throws Exception 127 { 128 return (Integer )getAttribute(server, poolName, POOL_ATT_CONN_COUNT); 129 130 } 131 132 public Integer getDestroyed() throws Exception 133 { 134 return getDestroyed(server, poolName); 135 136 } 137 138 public static Integer getDestroyed(MBeanServerConnection server, ObjectName poolName) throws Exception 139 { 140 141 return (Integer )getAttribute(server, poolName, POOL_ATT_DESTROYED_COUNT); 142 143 } 144 145 public Integer getMinSize() throws Exception 146 { 147 return getMinSize(server, poolName); 148 149 } 150 151 public static Integer getMinSize(MBeanServerConnection server, ObjectName poolName) throws Exception 152 { 153 154 return (Integer )getAttribute(server, poolName, POOL_ATT_MIN_CONN_COUNT); 155 156 } 157 158 public Long getBackgroundValMinutes() throws Exception 159 { 160 return getBackgroundValMinutes(server, poolName); 161 162 } 163 164 public static Long getBackgroundValMinutes(MBeanServerConnection server, ObjectName poolName) throws Exception 165 { 166 return (Long )getAttribute(server, poolName, POOL_ATT_BACKGROUND_VAL_MIN); 167 168 } 169 170 171 public static Integer getBlockingTimeout(MBeanServerConnection server, ObjectName poolName) throws Exception 172 { 173 return (Integer )getAttribute(server, poolName, POOL_ATT_BLOCK_TIME); 174 175 } 176 177 public void setPoolAttributeAndFlush(String attName, Object attValue) throws Exception 178 { 179 setPoolAttributeAndFlush(server, poolName, attName, attValue); 180 181 } 182 183 public static void setPoolAttributeAndFlush(MBeanServerConnection server, ObjectName pool, String attName, Object attValue) throws Exception 184 { 185 186 setAttribute(server, pool, attName, attValue); 187 flush(server, pool); 188 189 190 } 191 public static Object getAttribute(MBeanServerConnection server, ObjectName poolName, String attName) throws Exception 192 { 193 log.debug("Getting pool attribute " + attName); 194 Object 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 server, ObjectName objectName, String attName, Object attValue) throws Exception 201 { 202 203 server.setAttribute(objectName, new Attribute (attName, attValue)); 204 205 } 206 207 public Integer getConnectionCount() throws Exception 208 { 209 return getConnectionCount(server, poolName); 210 211 } 212 public static void flush(MBeanServerConnection server, ObjectName pool) throws Exception 213 { 214 server.invoke(pool, "flush", new Object [0], new String [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 getManagedConnectionFactory(String className) throws Exception 271 { 272 273 return getManagedConnectionFactory(Class.forName(className)); 274 } 275 276 public static ManagedConnectionFactory getManagedConnectionFactory(Class clazz) throws Exception 277 { 278 279 return (ManagedConnectionFactory ) clazz.newInstance(); 280 281 } 282 283 public static ManagedConnectionPool getManagedConnectionPool(int minSize, int maxSize, int blockingTimeout, long idleTimeout, boolean prefill, ManagedConnectionFactory 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 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 |