io.dropwizard.hibernate.UnitOfWorkAwareProxyFactory Maven / Gradle / Ivy
package io.dropwizard.hibernate;
import com.github.benmanes.caffeine.cache.CacheLoader;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import net.bytebuddy.ByteBuddy;
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
import net.bytebuddy.implementation.MethodDelegation;
import net.bytebuddy.implementation.bind.annotation.Origin;
import net.bytebuddy.implementation.bind.annotation.RuntimeType;
import net.bytebuddy.implementation.bind.annotation.SuperCall;
import net.bytebuddy.matcher.ElementMatchers;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.hibernate.SessionFactory;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* A factory for creating proxies for components that use Hibernate data access objects
* outside Jersey resources.
* A created proxy will be aware of the {@link UnitOfWork} annotation
* on the original class methods and will open a Hibernate session with a transaction
* around them.
*/
public class UnitOfWorkAwareProxyFactory {
private final Map sessionFactories;
private final LoadingCache, Class> proxyCache;
public UnitOfWorkAwareProxyFactory(String name, SessionFactory sessionFactory) {
this(Caffeine.newBuilder(), name, sessionFactory);
}
public UnitOfWorkAwareProxyFactory(Caffeine