    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 org.apache.commons.pool2.UsageTracking;
    020
    021import net.sf.cglib.proxy.Enhancer;
    022import net.sf.cglib.proxy.Factory;
    023
    024/**
    025 * Provides proxy objects using CGLib.
    026 *
    027 * @param <T> type of the pooled object to be proxied
    028 *
    029 * @since 2.0
    030 */
    031public class CglibProxySource<T> implements ProxySource<T> {
    032
    033    private final Class<? extends T> superclass;
    034
    035    /**
    036     * Create a new proxy source for the given class.
    037     *
    038     * @param superclass The class to proxy
    039     */
    040    public CglibProxySource(final Class<? extends T> superclass) {
    041        this.superclass = superclass;
    042    }
    043
    044    @Override
    045    public T createProxy(final T pooledObject, final UsageTracking<T> usageTracking) {
    046        final Enhancer enhancer = new Enhancer();
    047        enhancer.setSuperclass(superclass);
    048
    049        final CglibProxyHandler<T> proxyInterceptor =
    050                new CglibProxyHandler<>(pooledObject, usageTracking);
    051        enhancer.setCallback(proxyInterceptor);
    052
    053        @SuppressWarnings("unchecked")
    054        final
    055        T proxy = (T) enhancer.create();
    056
    057        return proxy;
    058    }
    059
    060
    061    @Override
    062    public T resolveProxy(final T proxy) {
    063        @SuppressWarnings("unchecked")
    064        final
    065        CglibProxyHandler<T> cglibProxyHandler =
    066                (CglibProxyHandler<T>) ((Factory) proxy).getCallback(0);
    067        return cglibProxyHandler.disableProxy();
    068    }
    069
    070    /**
    071     * @since 2.4.3
    072     */
    073    @Override
    074    public String toString() {
    075        final StringBuilder builder = new StringBuilder();
    076        builder.append("CglibProxySource [superclass=");
    077        builder.append(superclass);
    078        builder.append("]");
    079        return builder.toString();
    080    }
    081}