    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.pool2.proxy;
    018
    019import java.lang.reflect.Proxy;
    020import java.util.Arrays;
    021
    022import org.apache.commons.pool2.UsageTracking;
    023
    024/**
    025 * Provides proxy objects using Java reflection.
    026 *
    027 * @param <T> type of the pooled object to be proxied
    028 *
    029 * @since 2.0
    030 */
    031public class JdkProxySource<T> implements ProxySource<T> {
    032
    033    private final ClassLoader classLoader;
    034    private final Class<?>[] interfaces;
    035
    036
    037    /**
    038     * Create a new proxy source for the given interfaces.
    039     *
    040     * @param classLoader The class loader with which to create the proxy
    041     * @param interfaces  The interfaces to proxy
    042     */
    043    public JdkProxySource(final ClassLoader classLoader, final Class<?>[] interfaces) {
    044        this.classLoader = classLoader;
    045        // Defensive copy
    046        this.interfaces = new Class<?>[interfaces.length];
    047        System.arraycopy(interfaces, 0, this.interfaces, 0, interfaces.length);
    048    }
    049
    050
    051    @Override
    052    public T createProxy(final T pooledObject, final UsageTracking<T> usageTracking) {
    053        @SuppressWarnings("unchecked")
    054        final
    055        T proxy = (T) Proxy.newProxyInstance(classLoader, interfaces,
    056                new JdkProxyHandler<>(pooledObject, usageTracking));
    057        return proxy;
    058    }
    059
    060
    061    @Override
    062    public T resolveProxy(final T proxy) {
    063        @SuppressWarnings("unchecked")
    064        final
    065        JdkProxyHandler<T> jdkProxyHandler =
    066                (JdkProxyHandler<T>) Proxy.getInvocationHandler(proxy);
    067        final T pooledObject = jdkProxyHandler.disableProxy();
    068        return pooledObject;
    069    }
    070
    071
    072    /**
    073     * @since 2.4.3
    074     */
    075    @Override
    076    public String toString() {
    077        final StringBuilder builder = new StringBuilder();
    078        builder.append("JdkProxySource [classLoader=");
    079        builder.append(classLoader);
    080        builder.append(", interfaces=");
    081        builder.append(Arrays.toString(interfaces));
    082        builder.append("]");
    083        return builder.toString();
    084    }
    085}