dev.vality.woody.api.proxy.CPool2TargetProvider Maven / Gradle / Ivy
package dev.vality.woody.api.proxy;
import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.ObjectPool;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.AbandonedConfig;
import org.apache.commons.pool2.impl.DefaultPooledObject;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import java.util.function.Supplier;
public class CPool2TargetProvider implements InvocationTargetProvider {
private final ObjectPool pool;
private final Class targetType;
public static CPool2TargetProvider newInstance(Class targetType,
Supplier> objFactoryFunc,
GenericObjectPoolConfig config,
AbandonedConfig abandonedConfig) {
if (config == null) {
return new CPool2TargetProvider<>(targetType, objFactoryFunc);
} else if (abandonedConfig == null) {
return new CPool2TargetProvider<>(targetType, objFactoryFunc, config);
} else {
return new CPool2TargetProvider<>(targetType, objFactoryFunc, config, abandonedConfig);
}
}
public CPool2TargetProvider(Class targetType, Supplier> objFactoryFunc) {
this.targetType = targetType;
this.pool = new GenericObjectPool<>(objFactoryFunc.get());
}
public CPool2TargetProvider(Class targetType,
Supplier> objFactoryFunc,
GenericObjectPoolConfig config) {
this.targetType = targetType;
this.pool = new GenericObjectPool<>(objFactoryFunc.get(), config);
}
public CPool2TargetProvider(Class targetType,
Supplier> objFactoryFunc,
GenericObjectPoolConfig config,
AbandonedConfig abandonedConfig) {
this.targetType = targetType;
this.pool = new GenericObjectPool<>(objFactoryFunc.get(), config, abandonedConfig);
}
@Override
public T getTarget() {
try {
return pool.borrowObject();
} catch (Exception e) {
if (e instanceof InterruptedException) {
Thread.currentThread().interrupt();
}
throw new IllegalStateException("Failed to borrow object", e);
}
}
@Override
public Class getTargetType() {
return targetType;
}
@Override
public void releaseTarget(T target) {
try {
pool.returnObject(target);
} catch (Exception e) {
if (e instanceof InterruptedException) {
Thread.currentThread().interrupt();
}
throw new IllegalStateException("Failed to release object", e);
}
}
public void close() {
pool.close();
}
@Override
public boolean isSingleTarget() {
return false;
}
public static class TargetObjectFactory extends BasePooledObjectFactory {
private final Supplier targetSupplier;
public TargetObjectFactory(Supplier targetSupplier) {
this.targetSupplier = targetSupplier;
}
@Override
public T create() {
return targetSupplier.get();
}
@Override
public PooledObject wrap(T target) {
return new DefaultPooledObject<>(target);
}
}
}