    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 *      https://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 * cglib is unmaintained and does not work well (or possibly at all?) in newer JDKs, particularly JDK17+; see https://github.com/cglib/cglib
    026 * <p>
    027 * Provides proxy objects using CGLib.
    028 * </p>
    029 *
    030 * @param <T> type of the pooled object to be proxied
    031 * @since 2.0
    032 */
    033public class CglibProxySource<T> implements ProxySource<T> {
    034
    035    private final Class<? extends T> superclass;
    036
    037    /**
    038     * Constructs a new proxy source for the given class.
    039     *
    040     * @param superclass The class to proxy
    041     */
    042    public CglibProxySource(final Class<? extends T> superclass) {
    043        this.superclass = superclass;
    044    }
    045
    046    @SuppressWarnings("unchecked") // Case to T on return
    047    @Override
    048    public T createProxy(final T pooledObject, final UsageTracking<T> usageTracking) {
    049        final Enhancer enhancer = new Enhancer();
    050        enhancer.setSuperclass(superclass);
    051
    052        final CglibProxyHandler<T> proxyInterceptor =
    053                new CglibProxyHandler<>(pooledObject, usageTracking);
    054        enhancer.setCallback(proxyInterceptor);
    055
    056        return (T) enhancer.create();
    057    }
    058
    059    @Override
    060    public T resolveProxy(final T proxy) {
    061        @SuppressWarnings("unchecked")
    062        final
    063        CglibProxyHandler<T> cglibProxyHandler =
    064                (CglibProxyHandler<T>) ((Factory) proxy).getCallback(0);
    065        return cglibProxyHandler.disableProxy();
    066    }
    067
    068    /**
    069     * @since 2.4.3
    070     */
    071    @Override
    072    public String toString() {
    073        final StringBuilder builder = new StringBuilder();
    074        builder.append("CglibProxySource [superclass=");
    075        builder.append(superclass);
    076        builder.append("]");
    077        return builder.toString();
    078    }
    079}