1 /* 2 * Copyright 2010-2012 Julien Nicoulaud <julien.nicoulaud@gmail.com> 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package org.apache.commons.dbcp; 17 18 import org.weakref.jmx.MBeanExporter; 19 import org.weakref.jmx.Managed; 20 21 import javax.management.MBeanServer; 22 import java.lang.management.ManagementFactory; 23 import java.util.UUID; 24 25 /** 26 * Wrapper for {@link BasicDataSource} that exposes some fields and methods as a MBean. 27 * 28 * @author <a href="mailto:julien.nicoulaud@gmail.com">Julien Nicoulaud</a> 29 * @since 0.1 30 */ 31 public class ManagedBasicDataSource extends BasicDataSource { 32 33 /** 34 * The default auto-generated unique name for the exposed MBean. 35 */ 36 public static final String DEFAULT_MBEAN_NAME = "org.apache.commons.dbcp:ManagedBasicDataSource=ManagedBasicDataSource"; 37 38 /** 39 * The name under which this object is exposed to the MBean server. 40 */ 41 protected final String mBeanName; 42 43 /** 44 * Build a new instance of {@link org.apache.commons.dbcp.ManagedBasicDataSource} and expose it as a MBean with an auto-generated unique name. 45 * 46 * @see #DEFAULT_MBEAN_NAME 47 * @see #exportMBean(MBeanServer, String) 48 */ 49 public ManagedBasicDataSource() { 50 this(DEFAULT_MBEAN_NAME + "-" + UUID.randomUUID()); 51 } 52 53 /** 54 * Build a new instance of {@link org.apache.commons.dbcp.ManagedBasicDataSource} and expose it as a MBean with the specified name. 55 * 56 * @param mBeanName the name of the MBean to expose, should be unique accross the target application. 57 * @see #DEFAULT_MBEAN_NAME 58 * @see #exportMBean(MBeanServer, String) 59 */ 60 public ManagedBasicDataSource(String mBeanName) { 61 this(ManagementFactory.getPlatformMBeanServer(), mBeanName); 62 } 63 64 /** 65 * Build a new instance of {@link org.apache.commons.dbcp.ManagedBasicDataSource} and expose it as a MBean with the specified name. 66 * 67 * 68 * @param mBeanServer 69 * @param mBeanName the name of the MBean to expose, should be unique accross the target application. 70 * @see #DEFAULT_MBEAN_NAME 71 * @see #exportMBean(MBeanServer, String) 72 */ 73 public ManagedBasicDataSource(MBeanServer mBeanServer, String mBeanName) { 74 this.mBeanName = mBeanName; 75 exportMBean(mBeanServer, mBeanName); 76 } 77 78 /** 79 * Export this object as a MBean to the platform default MBean server. 80 * 81 * @param mBeanServer 82 * @param name the name of the MBean to expose. 83 */ 84 protected synchronized void exportMBean(MBeanServer mBeanServer, String name) { 85 new MBeanExporter(mBeanServer).export(name, this); 86 } 87 88 /** 89 * Get the name under which this object is exposed to the MBean server. 90 * 91 * @return the MBean object name. 92 */ 93 public String getMBeanName() { 94 return mBeanName; 95 } 96 97 /** 98 * Get the current number of active connections that have been allocated from this data source. 99 * 100 * @return the current number of active connections. 101 */ 102 @Managed(description = "The current number of active connections that have been allocated from this data source.") 103 public synchronized int getNumActive() { 104 return super.getNumActive(); 105 } 106 107 /** 108 * Get the current number of idle connections that are waiting to be allocated from this data source. 109 * 110 * @return the current number of idle connections. 111 */ 112 @Managed(description = "The current number of idle connections that are waiting to be allocated from this data source.") 113 public synchronized int getNumIdle() { 114 return super.getNumIdle(); 115 } 116 117 /** 118 * Get the maximum number of active connections that can be allocated at the same time. 119 * <p/> 120 * <p>A negative number means that there is no limit.</p> 121 * 122 * @return the maximum number of active connections. 123 */ 124 @Managed(description = "The maximum number of active connections that can be allocated at the same time.") 125 public synchronized int getMaxActive() { 126 return super.getMaxActive(); 127 } 128 129 /** 130 * Set the maximum number of active connections that can be allocated at the same time. Use a negative value for no limit. 131 * 132 * @param maxActive the new value for maxActive. 133 * @see #getMaxActive() 134 */ 135 @Managed(description = "Set the maximum number of active connections that can be allocated at the same time. Use a negative value for no limit.") 136 public synchronized void setMaxActive(int maxActive) { 137 super.setMaxActive(maxActive); 138 } 139 140 /** 141 * Get the maximum number of connections that can remain idle in the pool. 142 * <p/> 143 * <p>A negative value indicates that there is no limit.</p> 144 * 145 * @return the maximum number of idle connections. 146 */ 147 @Managed(description = "The maximum number of connections that can remain idle in the pool.") 148 public synchronized int getMaxIdle() { 149 return super.getMaxIdle(); 150 } 151 152 /** 153 * Set the maximum number of connections that can remain idle in the pool. 154 * 155 * @param maxIdle the new value for maxIdle. 156 * @see #getMaxIdle() 157 */ 158 @Managed(description = "Set the maximum number of connections that can remain idle in the pool.") 159 public synchronized void setMaxIdle(int maxIdle) { 160 super.setMaxIdle(maxIdle); 161 } 162 163 /** 164 * Get the minimum number of idle connections in the pool. 165 * 166 * @return the minimum number of idle connections. 167 * @see org.apache.commons.pool.impl.GenericObjectPool#getMinIdle() 168 */ 169 @Managed(description = "The minimum number of idle connections in the pool.") 170 public synchronized int getMinIdle() { 171 return super.getMinIdle(); 172 } 173 174 /** 175 * Set the minimum number of idle connections in the pool. 176 * 177 * @param minIdle the new value for minIdle. 178 * @see org.apache.commons.pool.impl.GenericObjectPool#setMinIdle(int) 179 */ 180 @Managed(description = "The minimum number of idle connections in the pool.") 181 public synchronized void setMinIdle(int minIdle) { 182 super.setMinIdle(minIdle); 183 } 184 185 /** 186 * Get the maximum number of milliseconds that the pool will wait for a connection to be returned before throwing an exception. 187 * <p/> 188 * <p>A value less than or equal to zero means the pool is set to wait indefinitely.</p> 189 * 190 * @return the maxWait property value. 191 */ 192 @Managed(description = "The maximum number of milliseconds that the pool will wait for a connection to be returned before throwing an exception.") 193 public synchronized long getMaxWait() { 194 return super.getMaxWait(); 195 } 196 197 /** 198 * Set the maxWait property. 199 * <p/> 200 * <p>Use -1 to make the pool wait indefinitely.</p> 201 * 202 * @param maxWait the new value for maxWait. 203 * @see #getMaxWait() 204 */ 205 @Managed(description = "Set the maxWait property. Use -1 to make the pool wait indefinitely.") 206 public synchronized void setMaxWait(long maxWait) { 207 super.setMaxWait(maxWait); 208 } 209 210 /** 211 * Get the JDBC connection {@link #url} property. 212 * 213 * @return the {@link #url} passed to the JDBC driver to establish connections. 214 */ 215 @Managed 216 public synchronized String getUrl() { 217 return super.getUrl(); 218 } 219 220 /** 221 * Get the JDBC connection {@link #username} property. 222 * 223 * @return the {@link #username} passed to the JDBC driver to establish connections. 224 */ 225 @Managed 226 public String getUsername() { 227 return super.getUsername(); 228 } 229 }