Skip to content

Commit

Permalink
Support GenericManagerFactory without reflection
Browse files Browse the repository at this point in the history
  • Loading branch information
filiphr committed Jan 30, 2024
1 parent 66cf78a commit 4dab118
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,7 @@ public void initSessionFactories() {
initDbSqlSessionFactory();
}

addSessionFactory(new GenericManagerFactory(EntityCache.class, EntityCacheImpl.class));
addSessionFactory(new GenericManagerFactory(EntityCache.class, EntityCacheImpl::new));

if (isLoggingSessionEnabled()) {
if (!sessionFactories.containsKey(LoggingSession.class)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

package org.flowable.common.engine.impl.persistence;

import java.util.function.Function;
import java.util.function.Supplier;

import org.flowable.common.engine.api.FlowableException;
import org.flowable.common.engine.impl.interceptor.CommandContext;
import org.flowable.common.engine.impl.interceptor.Session;
Expand All @@ -25,13 +28,35 @@
public class GenericManagerFactory implements SessionFactory {

protected Class<? extends Session> typeClass;
protected Class<? extends Session> implementationClass;
protected Function<CommandContext, ? extends Session> sessionCreator;

public GenericManagerFactory(Class<? extends Session> typeClass, Class<? extends Session> implementationClass) {
public GenericManagerFactory(Class<? extends Session> typeClass, Supplier<? extends Session> sessionCreator) {
this(typeClass, context -> sessionCreator.get());
}

public GenericManagerFactory(Class<? extends Session> typeClass, Function<CommandContext, ? extends Session> sessionCreator) {
this.typeClass = typeClass;
this.implementationClass = implementationClass;
this.sessionCreator = sessionCreator;
}

/**
* @deprecated use {@link #GenericManagerFactory(Class, Supplier)} instead
*/
@Deprecated
public GenericManagerFactory(Class<? extends Session> typeClass, Class<? extends Session> implementationClass) {
this(typeClass, commandContext -> {
try {
return implementationClass.getConstructor().newInstance();
} catch (Exception e) {
throw new FlowableException("couldn't instantiate " + implementationClass.getName() + ": " + e.getMessage(), e);
}
});
}

/**
* @deprecated use {@link #GenericManagerFactory(Class, Supplier)} instead
*/
@Deprecated
public GenericManagerFactory(Class<? extends Session> implementationClass) {
this(implementationClass, implementationClass);
}
Expand All @@ -43,10 +68,6 @@ public Class<?> getSessionType() {

@Override
public Session openSession(CommandContext commandContext) {
try {
return implementationClass.getConstructor().newInstance();
} catch (Exception e) {
throw new FlowableException("couldn't instantiate " + implementationClass.getName() + ": " + e.getMessage(), e);
}
return sessionCreator.apply(commandContext);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1355,7 +1355,7 @@ public void initSessionFactories() {
addSessionFactory(new AgendaSessionFactory(agendaFactory));
}

addSessionFactory(new GenericManagerFactory(EntityCache.class, EntityCacheImpl.class));
addSessionFactory(new GenericManagerFactory(EntityCache.class, EntityCacheImpl::new));

commandContextFactory.setSessionFactories(sessionFactories);

Expand Down

0 comments on commit 4dab118

Please sign in to comment.