BaseProxyHandler.java

  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. import java.lang.reflect.Method;

  19. import org.apache.commons.pool2.UsageTracking;

  20. /**
  21.  * Base implementation for object wrappers when using a
  22.  * {@link ProxiedObjectPool}.
  23.  *
  24.  * @param <T> type of the wrapped pooled object
  25.  * @since 2.0
  26.  */
  27. class BaseProxyHandler<T> {

  28.     private volatile T pooledObject;
  29.     private final UsageTracking<T> usageTracking;

  30.     /**
  31.      * Constructs a new wrapper for the given pooled object.
  32.      *
  33.      * @param pooledObject  The object to wrap
  34.      * @param usageTracking The instance, if any (usually the object pool) to
  35.      *                      be provided with usage tracking information for this
  36.      *                      wrapped object
  37.      */
  38.     BaseProxyHandler(final T pooledObject, final UsageTracking<T> usageTracking) {
  39.         this.pooledObject = pooledObject;
  40.         this.usageTracking = usageTracking;
  41.     }

  42.     /**
  43.      * Disable the proxy wrapper. Called when the object has been returned to
  44.      * the pool. Further use of the wrapper should result in an
  45.      * {@link IllegalStateException}.
  46.      *
  47.      * @return the object that this proxy was wrapping
  48.      */
  49.     T disableProxy() {
  50.         final T result = pooledObject;
  51.         pooledObject = null;
  52.         return result;
  53.     }

  54.     /**
  55.      * Invoke the given method on the wrapped object.
  56.      *
  57.      * @param method    The method to invoke
  58.      * @param args      The arguments to the method
  59.      * @return          The result of the method call
  60.      * @throws Throwable    If the method invocation fails
  61.      */
  62.     Object doInvoke(final Method method, final Object[] args) throws Throwable {
  63.         validateProxiedObject();
  64.         final T object = getPooledObject();
  65.         if (usageTracking != null) {
  66.             usageTracking.use(object);
  67.         }
  68.         return method.invoke(object, args);
  69.     }

  70.     /**
  71.      * Gets the wrapped, pooled object.
  72.      *
  73.      * @return the underlying pooled object
  74.      */
  75.     T getPooledObject() {
  76.         return pooledObject;
  77.     }

  78.     /**
  79.      * @since 2.4.3
  80.      */
  81.     @Override
  82.     public String toString() {
  83.         final StringBuilder builder = new StringBuilder();
  84.         builder.append(getClass().getName());
  85.         builder.append(" [pooledObject=");
  86.         builder.append(pooledObject);
  87.         builder.append(", usageTracking=");
  88.         builder.append(usageTracking);
  89.         builder.append("]");
  90.         return builder.toString();
  91.     }

  92.     /**
  93.      * Check that the proxy is still valid (i.e. that {@link #disableProxy()}
  94.      * has not been called).
  95.      *
  96.      * @throws IllegalStateException if {@link #disableProxy()} has been called
  97.      */
  98.     void validateProxiedObject() {
  99.         if (pooledObject == null) {
  100.             throw new IllegalStateException("This object may no longer be " +
  101.                     "used as it has been returned to the Object Pool.");
  102.         }
  103.     }
  104. }