TSK-865: moved HistoryEventProducer to InternalTaskanaEngine

This commit is contained in:
Mustapha Zorgati 2019-10-18 12:58:39 +02:00
parent 9a4f4dd9ef
commit 06bfe215d4
7 changed files with 42 additions and 70 deletions

View File

@ -56,16 +56,22 @@ public interface TaskanaEngine {
TaskanaEngineConfiguration getConfiguration(); TaskanaEngineConfiguration getConfiguration();
/** /**
* The HistoryEventProducer events and emits them to the registered history service providers. * Checks if the history plugin is enabled.
* @return The HistoryEventProducer *
* @return true if the history is enabled. Otherwise false.
*/ */
HistoryEventProducer getHistoryEventProducer(); boolean isHistoryEnabled();
/** /**
* sets the connection management mode for taskana. * sets the connection management mode.
* *
* @param mode * @param mode
* See ConnectionManagementMode * the connection management mode Valid values are:
* <ul>
* <li>PARTICIPATE - taskana participates in global transaction. This is the default mode.</li>
* <li>AUTOCOMMIT - taskana commits each API call separately</li>
* <li>EXPLICIT - commit processing is managed explicitly by the client</li>
* </ul>
*/ */
void setConnectionManagementMode(ConnectionManagementMode mode); void setConnectionManagementMode(ConnectionManagementMode mode);
@ -167,6 +173,13 @@ public interface TaskanaEngine {
*/ */
TaskanaEngine getEngine(); TaskanaEngine getEngine();
/**
* Retrieve HistoryEventProducer.
*
* @return the HistoryEventProducer instance.
*/
HistoryEventProducer getHistoryEventProducer();
} }
} }

View File

@ -15,9 +15,9 @@ import pro.taskana.history.api.TaskanaHistoryEvent;
public final class HistoryEventProducer { public final class HistoryEventProducer {
private static final Logger LOGGER = LoggerFactory.getLogger(HistoryEventProducer.class); private static final Logger LOGGER = LoggerFactory.getLogger(HistoryEventProducer.class);
private static HistoryEventProducer emitterInstance; private static HistoryEventProducer singleton;
private static boolean enabled = false;
private ServiceLoader<TaskanaHistory> serviceLoader; private ServiceLoader<TaskanaHistory> serviceLoader;
private boolean enabled = false;
private HistoryEventProducer(TaskanaEngineConfiguration taskanaEngineConfiguration) { private HistoryEventProducer(TaskanaEngineConfiguration taskanaEngineConfiguration) {
serviceLoader = ServiceLoader.load(TaskanaHistory.class); serviceLoader = ServiceLoader.load(TaskanaHistory.class);
@ -32,17 +32,13 @@ public final class HistoryEventProducer {
} }
public static synchronized HistoryEventProducer getInstance(TaskanaEngineConfiguration taskanaEngineConfiguration) { public static synchronized HistoryEventProducer getInstance(TaskanaEngineConfiguration taskanaEngineConfiguration) {
if (emitterInstance == null) { if (singleton == null) {
emitterInstance = new HistoryEventProducer(taskanaEngineConfiguration); singleton = new HistoryEventProducer(taskanaEngineConfiguration);
} }
return emitterInstance; return singleton;
} }
public static boolean isHistoryEnabled() { public static boolean isHistoryEnabled() {
return getInstance(null).isEnabled();
}
public boolean isEnabled() {
return enabled; return enabled;
} }

View File

@ -105,7 +105,7 @@ public class TaskServiceImpl implements TaskService {
this.workbasketService = taskanaEngine.getEngine().getWorkbasketService(); this.workbasketService = taskanaEngine.getEngine().getWorkbasketService();
this.attachmentMapper = attachmentMapper; this.attachmentMapper = attachmentMapper;
this.classificationService = taskanaEngine.getEngine().getClassificationService(); this.classificationService = taskanaEngine.getEngine().getClassificationService();
this.historyEventProducer = taskanaEngine.getEngine().getHistoryEventProducer(); this.historyEventProducer = taskanaEngine.getHistoryEventProducer();
} }
@Override @Override

View File

@ -56,20 +56,20 @@ public class TaskanaEngineImpl implements TaskanaEngine {
private static final String DEFAULT = "default"; private static final String DEFAULT = "default";
private static final Logger LOGGER = LoggerFactory.getLogger(TaskanaEngineImpl.class); private static final Logger LOGGER = LoggerFactory.getLogger(TaskanaEngineImpl.class);
protected static ThreadLocal<Deque<SqlSessionManager>> sessionStack = new ThreadLocal<>(); private static ThreadLocal<Deque<SqlSessionManager>> sessionStack = new ThreadLocal<>();
protected TaskanaEngineConfiguration taskanaEngineConfiguration; protected TaskanaEngineConfiguration taskanaEngineConfiguration;
protected TransactionFactory transactionFactory; protected TransactionFactory transactionFactory;
protected SqlSessionManager sessionManager; protected SqlSessionManager sessionManager;
protected ConnectionManagementMode mode = ConnectionManagementMode.PARTICIPATE; protected ConnectionManagementMode mode = ConnectionManagementMode.PARTICIPATE;
protected java.sql.Connection connection = null; protected java.sql.Connection connection = null;
protected HistoryEventProducer historyEventProducer; private HistoryEventProducer historyEventProducer;
private Internal internal; private Internal internal;
protected TaskanaEngineImpl(TaskanaEngineConfiguration taskanaEngineConfiguration) { protected TaskanaEngineImpl(TaskanaEngineConfiguration taskanaEngineConfiguration) {
this.taskanaEngineConfiguration = taskanaEngineConfiguration; this.taskanaEngineConfiguration = taskanaEngineConfiguration;
createTransactionFactory(taskanaEngineConfiguration.getUseManagedTransactions()); createTransactionFactory(taskanaEngineConfiguration.getUseManagedTransactions());
this.sessionManager = createSqlSessionManager(); this.sessionManager = createSqlSessionManager();
this.historyEventProducer = HistoryEventProducer.getInstance(taskanaEngineConfiguration); historyEventProducer = HistoryEventProducer.getInstance(taskanaEngineConfiguration);
this.internal = new Internal(); this.internal = new Internal();
} }
@ -89,7 +89,7 @@ public class TaskanaEngineImpl implements TaskanaEngine {
* *
* @return Stack of SqlSessionManager * @return Stack of SqlSessionManager
*/ */
protected static Deque<SqlSessionManager> getSessionStack() { private static Deque<SqlSessionManager> getSessionStack() {
Deque<SqlSessionManager> stack = sessionStack.get(); Deque<SqlSessionManager> stack = sessionStack.get();
if (stack == null) { if (stack == null) {
stack = new ArrayDeque<>(); stack = new ArrayDeque<>();
@ -98,7 +98,7 @@ public class TaskanaEngineImpl implements TaskanaEngine {
return stack; return stack;
} }
protected static SqlSessionManager getSessionFromStack() { private static SqlSessionManager getSessionFromStack() {
Deque<SqlSessionManager> stack = getSessionStack(); Deque<SqlSessionManager> stack = getSessionStack();
if (stack.isEmpty()) { if (stack.isEmpty()) {
return null; return null;
@ -106,11 +106,11 @@ public class TaskanaEngineImpl implements TaskanaEngine {
return stack.peek(); return stack.peek();
} }
protected static void pushSessionToStack(SqlSessionManager session) { private static void pushSessionToStack(SqlSessionManager session) {
getSessionStack().push(session); getSessionStack().push(session);
} }
protected static void popSessionFromStack() { private static void popSessionFromStack() {
Deque<SqlSessionManager> stack = getSessionStack(); Deque<SqlSessionManager> stack = getSessionStack();
if (!stack.isEmpty()) { if (!stack.isEmpty()) {
stack.pop(); stack.pop();
@ -171,21 +171,10 @@ public class TaskanaEngineImpl implements TaskanaEngine {
} }
@Override @Override
public HistoryEventProducer getHistoryEventProducer() { public boolean isHistoryEnabled() {
return historyEventProducer; return HistoryEventProducer.isHistoryEnabled();
} }
/**
* sets the connection management mode.
*
* @param mode
* - the connection management mode Valid values are:
* <ul>
* <li>PARTICIPATE - taskana participates in global transaction. This is the default mode.</li>
* <li>AUTOCOMMIT - taskana commits each API call separately</li>
* <li>EXPLICIT - commit processing is managed explicitly by the client</li>
* </ul>
*/
@Override @Override
public void setConnectionManagementMode(ConnectionManagementMode mode) { public void setConnectionManagementMode(ConnectionManagementMode mode) {
if (this.mode == ConnectionManagementMode.EXPLICIT && connection != null if (this.mode == ConnectionManagementMode.EXPLICIT && connection != null
@ -198,15 +187,6 @@ public class TaskanaEngineImpl implements TaskanaEngine {
this.mode = mode; this.mode = mode;
} }
/**
* Set the database connection to be used by taskana. If this Api is called, taskana uses the connection passed by
* the client for database access in all subsequent API calls until the client resets this connection. Control over
* commit and rollback is the responsibility of the client. In order to close the connection, the client can call
* TaskanaEngine.closeConnection() or TaskanaEngine.setConnection(null). Both calls have the same effect.
*
* @param connection
* The connection that passed into TaskanaEngine
*/
@Override @Override
public void setConnection(java.sql.Connection connection) throws SQLException { public void setConnection(java.sql.Connection connection) throws SQLException {
if (connection != null) { if (connection != null) {
@ -221,10 +201,6 @@ public class TaskanaEngineImpl implements TaskanaEngine {
} }
} }
/**
* closes the connection to the database in mode EXPLICIT. In mode EXPLICIT, closes the client's connection, sets it
* to null and switches to mode PARTICIPATE Has the same effect as setConnection(null)
*/
@Override @Override
public void closeConnection() { public void closeConnection() {
if (this.mode == ConnectionManagementMode.EXPLICIT) { if (this.mode == ConnectionManagementMode.EXPLICIT) {
@ -236,14 +212,6 @@ public class TaskanaEngineImpl implements TaskanaEngine {
} }
} }
/**
* Checks whether current user is member of any of the specified roles.
*
* @param roles
* The roles that are checked for membership of the current user
* @throws NotAuthorizedException
* If the current user is not member of any specified role
*/
@Override @Override
public void checkRoleMembership(TaskanaRole... roles) throws NotAuthorizedException { public void checkRoleMembership(TaskanaRole... roles) throws NotAuthorizedException {
if (!isUserInRole(roles)) { if (!isUserInRole(roles)) {
@ -258,13 +226,6 @@ public class TaskanaEngineImpl implements TaskanaEngine {
} }
} }
/**
* check whether the current user is member of one of the roles specified.
*
* @param roles
* The roles that are checked for membership of the current user
* @return true if the current user is a member of at least one of the specified groups
*/
@Override @Override
public boolean isUserInRole(TaskanaRole... roles) { public boolean isUserInRole(TaskanaRole... roles) {
if (!getConfiguration().isSecurityEnabled()) { if (!getConfiguration().isSecurityEnabled()) {
@ -407,5 +368,10 @@ public class TaskanaEngineImpl implements TaskanaEngine {
return TaskanaEngineImpl.this; return TaskanaEngineImpl.this;
} }
@Override
public HistoryEventProducer getHistoryEventProducer() {
return historyEventProducer;
}
} }
} }

View File

@ -5,7 +5,6 @@ import static org.junit.Assert.assertFalse;
import org.junit.Test; import org.junit.Test;
import acceptance.AbstractAccTest; import acceptance.AbstractAccTest;
import pro.taskana.history.HistoryEventProducer;
/** /**
* Acceptance test for historyEventProducer class. * Acceptance test for historyEventProducer class.
@ -14,7 +13,6 @@ public class TaskEventProducerTest extends AbstractAccTest {
@Test @Test
public void testHistoryEventProducerIsNotEnabled() { public void testHistoryEventProducerIsNotEnabled() {
HistoryEventProducer historyEventProducer = taskanaEngine.getHistoryEventProducer(); assertFalse(taskanaEngine.isHistoryEnabled());
assertFalse(historyEventProducer.isEnabled());
} }
} }

View File

@ -120,8 +120,8 @@ public class TaskServiceImplTest {
verify(workbasketServiceMock, times(1)).getWorkbasket(destinationWorkbasket.getId()); verify(workbasketServiceMock, times(1)).getWorkbasket(destinationWorkbasket.getId());
verify(taskMapperMock, times(1)).update(any()); verify(taskMapperMock, times(1)).update(any());
verify(taskanaEngineInternalMock, times(1)).returnConnection(); verify(taskanaEngineInternalMock, times(1)).returnConnection();
verify(taskanaEngineInternalMock, times(3)).getEngine(); verify(taskanaEngineInternalMock, times(2)).getEngine();
verify(taskanaEngineMock).getHistoryEventProducer(); verify(taskanaEngineInternalMock).getHistoryEventProducer();
verify(taskanaEngineMock).getWorkbasketService(); verify(taskanaEngineMock).getWorkbasketService();
verify(taskanaEngineMock).getClassificationService(); verify(taskanaEngineMock).getClassificationService();
verifyNoMoreInteractions(attachmentMapperMock, taskanaEngineConfigurationMock, taskanaEngineMock, verifyNoMoreInteractions(attachmentMapperMock, taskanaEngineConfigurationMock, taskanaEngineMock,

View File

@ -100,8 +100,7 @@ public class TaskanaEngineController {
@GetMapping(path = "/history-provider-enabled") @GetMapping(path = "/history-provider-enabled")
public ResponseEntity<Boolean> getIsHistoryProviderEnabled() { public ResponseEntity<Boolean> getIsHistoryProviderEnabled() {
ResponseEntity<Boolean> response = ResponseEntity.ok( ResponseEntity<Boolean> response = ResponseEntity.ok(taskanaEngine.isHistoryEnabled());
((TaskanaEngineImpl) taskanaEngine).getHistoryEventProducer().isEnabled());
LOGGER.debug("Exit from getIsHistoryProviderEnabled(), returning {}", response); LOGGER.debug("Exit from getIsHistoryProviderEnabled(), returning {}", response);
return response; return response;
} }