Skip to content

Commit

Permalink
[JBTM-2647] Allow enlisting handlers by instance to BAControler
Browse files Browse the repository at this point in the history
  • Loading branch information
Gytis Trikleris committed Apr 6, 2016
1 parent 3a745b3 commit 5c82ef4
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.jboss.narayana.compensations.impl;


import org.jboss.narayana.compensations.api.CompensationHandler;
import org.jboss.narayana.compensations.api.ConfirmationHandler;
import org.jboss.narayana.compensations.api.TransactionLoggedHandler;
Expand All @@ -18,15 +17,18 @@ public interface BAControler {

void completeBusinessActivity(boolean isException) throws Exception;

public boolean isBARunning();
boolean isBARunning();

Object suspend() throws Exception;

public Object suspend() throws Exception;
void resume(Object context) throws Exception;

public void resume(Object context) throws Exception;
Object getCurrentTransaction() throws Exception;

public Object getCurrentTransaction() throws Exception;
ParticipantManager enlist(Class<? extends CompensationHandler> compensationHandlerClass,
Class<? extends ConfirmationHandler> confirmationHandlerClass,
Class<? extends TransactionLoggedHandler> transactionLoggedHandlerClass) throws Exception;

public ParticipantManager enlist(Class<? extends CompensationHandler> compensationHandlerClass,
Class<? extends ConfirmationHandler> confirmationHandlerClass,
Class<? extends TransactionLoggedHandler> transactionLoggedHandlerClass) throws Exception;
ParticipantManager enlist(CompensationHandler compensationHandler, ConfirmationHandler confirmationHandler,
TransactionLoggedHandler transactionLoggedHandler) throws Exception;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ public static BeanManager getBeanManager() {
return CDI.current().getBeanManager();
}

public static <T> T createBeanInstance(Class<T> clazz) {

return BeanManagerUtil.createBeanInstance(clazz, BeanManagerUtil.getBeanManager());
}

public static <T> T createBeanInstance(Class<T> clazz, BeanManager bm) {

Iterator<Bean<?>> iter = bm.getBeans(clazz).iterator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.jboss.narayana.compensations.api.ConfirmationHandler;
import org.jboss.narayana.compensations.api.TransactionLoggedHandler;

import javax.enterprise.inject.spi.BeanManager;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
Expand All @@ -43,35 +42,29 @@ public class ParticipantImpl implements BusinessAgreementWithParticipantCompleti

private static final Map<Object, AtomicInteger> PARTICIPANT_COUNTERS = new HashMap<>();

private Class<? extends CompensationHandler> compensationHandler;
private Class<? extends ConfirmationHandler> confirmationHandler;
private Class<? extends TransactionLoggedHandler> transactionLoggedHandler;
private CompensationHandler compensationHandler;

private ConfirmationHandler confirmationHandler;

private TransactionLoggedHandler transactionLoggedHandler;

private BeanManager beanManager;
private ClassLoader applicationClassloader;

private Object currentTX;

public ParticipantImpl(Class<? extends CompensationHandler> compensationHandlerClass, Class<? extends ConfirmationHandler> confirmationHandlerClass, Class<? extends TransactionLoggedHandler> transactionLoggedHandlerClass, Object currentTX) {
public ParticipantImpl(CompensationHandler compensationHandler, ConfirmationHandler confirmationHandler,
TransactionLoggedHandler transactionLoggedHandler, Object currentTX) {

this.compensationHandler = compensationHandlerClass;
this.confirmationHandler = confirmationHandlerClass;
this.transactionLoggedHandler = transactionLoggedHandlerClass;
this.compensationHandler = compensationHandler;
this.confirmationHandler = confirmationHandler;
this.transactionLoggedHandler = transactionLoggedHandler;
this.currentTX = currentTX;

beanManager = BeanManagerUtil.getBeanManager();
applicationClassloader = Thread.currentThread().getContextClassLoader();

incrementParticipantsCounter();
}

private <T extends Object> T instantiate(Class<T> clazz) {

if (clazz == null) {
return null;
}
return BeanManagerUtil.createBeanInstance(clazz, beanManager);
}

@Override
public void confirmCompleted(boolean confirmed) {

Expand All @@ -80,8 +73,7 @@ public void confirmCompleted(boolean confirmed) {
ClassLoader origClassLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(applicationClassloader);

TransactionLoggedHandler handler = instantiate(transactionLoggedHandler);
handler.transactionLogged(confirmed);
transactionLoggedHandler.transactionLogged(confirmed);

Thread.currentThread().setContextClassLoader(origClassLoader);
}
Expand All @@ -96,8 +88,7 @@ public void close() throws WrongStateException, SystemException {
Thread.currentThread().setContextClassLoader(applicationClassloader);
CompensationContext.setTxContextToExtend(currentTX);

ConfirmationHandler handler = instantiate(confirmationHandler);
handler.confirm();
confirmationHandler.confirm();

Thread.currentThread().setContextClassLoader(origClassLoader);
}
Expand All @@ -120,8 +111,7 @@ public void compensate() throws FaultedException, WrongStateException, SystemExc
Thread.currentThread().setContextClassLoader(applicationClassloader);
CompensationContext.setTxContextToExtend(currentTX);

CompensationHandler handler = instantiate(compensationHandler);
handler.compensate();
compensationHandler.compensate();

Thread.currentThread().setContextClassLoader(origClassLoader);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.jboss.narayana.compensations.api.TransactionCompensatedException;
import org.jboss.narayana.compensations.api.TransactionLoggedHandler;
import org.jboss.narayana.compensations.impl.BAControler;
import org.jboss.narayana.compensations.impl.BeanManagerUtil;
import org.jboss.narayana.compensations.impl.CompensationManagerImpl;
import org.jboss.narayana.compensations.impl.CompensationManagerState;
import org.jboss.narayana.compensations.impl.ParticipantManager;
Expand Down Expand Up @@ -88,15 +89,37 @@ public Object getCurrentTransaction() throws Exception {
}

@Override
public ParticipantManager enlist(Class<? extends CompensationHandler> compensationHandlerClass, Class<? extends ConfirmationHandler> confirmationHandlerClass, Class<? extends TransactionLoggedHandler> transactionLoggedHandlerClass) throws Exception {
public ParticipantManager enlist(Class<? extends CompensationHandler> compensationHandlerClass,
Class<? extends ConfirmationHandler> confirmationHandlerClass,
Class<? extends TransactionLoggedHandler> transactionLoggedHandlerClass) throws Exception {

CompensationHandler compensationHandler = instantiate(compensationHandlerClass);
ConfirmationHandler confirmationHandler = instantiate(confirmationHandlerClass);
TransactionLoggedHandler transactionLoggedHandler = instantiate(transactionLoggedHandlerClass);

return enlist(compensationHandler, confirmationHandler, transactionLoggedHandler);
}

@Override
public ParticipantManager enlist(CompensationHandler compensationHandler, ConfirmationHandler confirmationHandler,
TransactionLoggedHandler transactionLoggedHandler) throws Exception {

String participantId = String.valueOf(UUID.randomUUID());
LocalParticipant participant = new LocalParticipant(compensationHandlerClass, confirmationHandlerClass, transactionLoggedHandlerClass, getCurrentTransaction(), participantId);
LocalParticipant participant = new LocalParticipant(compensationHandler, confirmationHandler, transactionLoggedHandler,
getCurrentTransaction(), participantId);

CoordinatorManagerFactory.coordinatorManager().enlistParticipant(participant);

return new LocalParticipantManager(participantId);
}

private <T> T instantiate(Class<T> clazz) {

if (clazz == null) {
return null;
}

return BeanManagerUtil.createBeanInstance(clazz);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,14 @@
*/
public class LocalParticipant implements BAParticipant, Participant {


private ParticipantImpl participant;

private String participantId;

public LocalParticipant(Class<? extends CompensationHandler> compensationHandlerClass,
Class<? extends ConfirmationHandler> confirmationHandlerClass,
Class<? extends TransactionLoggedHandler> transactionLoggedHandlerClass,
Object currentTX, String participantId) {
public LocalParticipant(CompensationHandler compensationHandler, ConfirmationHandler confirmationHandler,
TransactionLoggedHandler transactionLoggedHandler, Object currentTX, String participantId) {

participant = new ParticipantImpl(compensationHandlerClass, confirmationHandlerClass, transactionLoggedHandlerClass, currentTX);
participant = new ParticipantImpl(compensationHandler, confirmationHandler, transactionLoggedHandler, currentTX);
this.participantId = participantId;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.jboss.narayana.compensations.api.TransactionCompensatedException;
import org.jboss.narayana.compensations.api.TransactionLoggedHandler;
import org.jboss.narayana.compensations.impl.BAControler;
import org.jboss.narayana.compensations.impl.BeanManagerUtil;
import org.jboss.narayana.compensations.impl.CompensationManagerImpl;
import org.jboss.narayana.compensations.impl.CompensationManagerState;
import org.jboss.narayana.compensations.impl.ParticipantManager;
Expand Down Expand Up @@ -99,10 +100,32 @@ public ParticipantManager enlist(Class<? extends CompensationHandler> compensati
Class<? extends ConfirmationHandler> confirmationHandlerClass,
Class<? extends TransactionLoggedHandler> transactionLoggedHandlerClass) throws Exception {

RemoteParticipant p = new RemoteParticipant(compensationHandlerClass, confirmationHandlerClass, transactionLoggedHandlerClass, getCurrentTransaction());
BAParticipantManager pm = BusinessActivityManagerFactory.businessActivityManager().
enlistForBusinessAgreementWithParticipantCompletion(p, String.valueOf(UUID.randomUUID()));
return new RemoteParticipantManager(pm);
CompensationHandler compensationHandler = instantiate(compensationHandlerClass);
ConfirmationHandler confirmationHandler = instantiate(confirmationHandlerClass);
TransactionLoggedHandler transactionLoggedHandler = instantiate(transactionLoggedHandlerClass);

return enlist(compensationHandler, confirmationHandler, transactionLoggedHandler);
}

@Override
public ParticipantManager enlist(CompensationHandler compensationHandler, ConfirmationHandler confirmationHandler,
TransactionLoggedHandler transactionLoggedHandler) throws Exception {

RemoteParticipant participant = new RemoteParticipant(compensationHandler, confirmationHandler,
transactionLoggedHandler, getCurrentTransaction());
BAParticipantManager baParticipantManager = BusinessActivityManagerFactory.businessActivityManager()
.enlistForBusinessAgreementWithParticipantCompletion(participant, String.valueOf(UUID.randomUUID()));

return new RemoteParticipantManager(baParticipantManager);
}

private <T> T instantiate(Class<T> clazz) {

if (clazz == null) {
return null;
}

return BeanManagerUtil.createBeanInstance(clazz);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,10 @@ public class RemoteParticipant implements BAParticipant, BusinessAgreementWithPa

private ParticipantImpl participant;

public RemoteParticipant(Class<? extends CompensationHandler> compensationHandlerClass,
Class<? extends ConfirmationHandler> confirmationHandlerClass,
Class<? extends TransactionLoggedHandler> transactionLoggedHandlerClass, Object currentTX) {
public RemoteParticipant(CompensationHandler compensationHandler, ConfirmationHandler confirmationHandler,
TransactionLoggedHandler transactionLoggedHandler, Object currentTX) {

participant = new ParticipantImpl(compensationHandlerClass, confirmationHandlerClass, transactionLoggedHandlerClass, currentTX);
participant = new ParticipantImpl(compensationHandler, confirmationHandler, transactionLoggedHandler, currentTX);
}


Expand Down

0 comments on commit 5c82ef4

Please sign in to comment.