All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.iwbfly.myhttp.proxy.ProxyFactory Maven / Gradle / Ivy

The newest version!
package com.iwbfly.myhttp.proxy;

import com.iwbfly.myhttp.Myhttp;

import java.lang.reflect.Proxy;

/**
 * @author pangyajun
 * @since 2021/10/14 13:47
 **/
public class ProxyFactory {

    private Myhttp.Builder configuration;
    private Class interfaceClass;

    public ProxyFactory(Myhttp.Builder  configuration, Class interfaceClass) {
        this.configuration = configuration;
        this.interfaceClass = interfaceClass;
    }

    public Class getInterfaceClass() {
        return interfaceClass;
    }

    public void setInterfaceClass(Class interfaceClass) {
        this.interfaceClass = interfaceClass;
    }

    public T createInstance() {
        T instance = (T) configuration.getInstanceCache().get(interfaceClass);
        boolean cacheEnabled = configuration.isCacheEnabled();
        if (cacheEnabled && instance != null) {
            return instance;
        }

        synchronized (configuration.getInstanceCache()) {
            instance = (T) configuration.getInstanceCache().get(interfaceClass);
            if (cacheEnabled && instance != null) {
                return instance;
            }
            InterfaceProxyHandler interfaceProxyHandler = new InterfaceProxyHandler(configuration,this, interfaceClass);
            instance = (T) Proxy.newProxyInstance(interfaceClass.getClassLoader(), new Class[]{interfaceClass,MyhttpClientProxy.class}, interfaceProxyHandler);

            if (cacheEnabled) {
                configuration.getInstanceCache().put(interfaceClass, instance);
            }
        }
        return instance;

    }

    public Myhttp.Builder getConfiguration() {
        return configuration;
    }

    public void setConfiguration(Myhttp.Builder configuration) {
        this.configuration = configuration;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy