1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.commons.pool2.proxy; 18 19 import java.lang.reflect.Method; 20 21 import org.apache.commons.pool2.UsageTracking; 22 23 /** 24 * Base implementation for object wrappers when using a 25 * {@link ProxiedObjectPool}. 26 * 27 * @param <T> type of the wrapped pooled object 28 * 29 * @since 2.0 30 */ 31 class BaseProxyHandler<T> { 32 33 private volatile T pooledObject; 34 private final UsageTracking<T> usageTracking; 35 36 37 /** 38 * Constructs a new wrapper for the given pooled object. 39 * 40 * @param pooledObject The object to wrap 41 * @param usageTracking The instance, if any (usually the object pool) to 42 * be provided with usage tracking information for this 43 * wrapped object 44 */ 45 BaseProxyHandler(final T pooledObject, final UsageTracking<T> usageTracking) { 46 this.pooledObject = pooledObject; 47 this.usageTracking = usageTracking; 48 } 49 50 51 /** 52 * Disable the proxy wrapper. Called when the object has been returned to 53 * the pool. Further use of the wrapper should result in an 54 * {@link IllegalStateException}. 55 * 56 * @return the object that this proxy was wrapping 57 */ 58 T disableProxy() { 59 final T result = pooledObject; 60 pooledObject = null; 61 return result; 62 } 63 64 65 /** 66 * Invoke the given method on the wrapped object. 67 * 68 * @param method The method to invoke 69 * @param args The arguments to the method 70 * @return The result of the method call 71 * @throws Throwable If the method invocation fails 72 */ 73 Object doInvoke(final Method method, final Object[] args) throws Throwable { 74 validateProxiedObject(); 75 final T object = getPooledObject(); 76 if (usageTracking != null) { 77 usageTracking.use(object); 78 } 79 return method.invoke(object, args); 80 } 81 82 83 /** 84 * Gets the wrapped, pooled object. 85 * 86 * @return the underlying pooled object 87 */ 88 T getPooledObject() { 89 return pooledObject; 90 } 91 92 93 /** 94 * @since 2.4.3 95 */ 96 @Override 97 public String toString() { 98 final StringBuilder builder = new StringBuilder(); 99 builder.append(getClass().getName()); 100 builder.append(" [pooledObject="); 101 builder.append(pooledObject); 102 builder.append(", usageTracking="); 103 builder.append(usageTracking); 104 builder.append("]"); 105 return builder.toString(); 106 } 107 108 109 /** 110 * Check that the proxy is still valid (i.e. that {@link #disableProxy()} 111 * has not been called). 112 * 113 * @throws IllegalStateException if {@link #disableProxy()} has been called 114 */ 115 void validateProxiedObject() { 116 if (pooledObject == null) { 117 throw new IllegalStateException("This object may no longer be " + 118 "used as it has been returned to the Object Pool."); 119 } 120 } 121 }