Skip to content
Permalink
Browse files
update to spec
  • Loading branch information
djencks committed Aug 22, 2009
1 parent 4d877c5 commit c69cfd049c17c5f8d7cfb305a5315db10fb902b4
Showing 7 changed files with 90 additions and 80 deletions.
@@ -21,6 +21,8 @@
import javax.resource.spi.UnavailableException;
import javax.resource.spi.XATerminator;
import javax.resource.spi.work.WorkManager;
import javax.resource.spi.work.WorkContext;
import javax.transaction.TransactionSynchronizationRegistry;

/**
* GBean BootstrapContext implementation that refers to externally configured WorkManager
@@ -72,4 +74,12 @@ public Timer createTimer() throws UnavailableException {
return new Timer();
}

public TransactionSynchronizationRegistry getTransactionSynchronizationRegistry() {
return null;
}

public boolean isContextSupported(Class<? extends WorkContext> aClass) {
return false;
}

}
@@ -65,7 +65,7 @@ public class GeronimoWorkManager implements WorkManager {
*/
private Executor scheduledWorkExecutorPool;

private final Collection<InflowContextHandler> inflowContextHandlers;
private final Collection<WorkContextHandler> workContextHandlers;


private final WorkExecutor scheduleWorkExecutor = new ScheduleWorkExecutor();
@@ -79,11 +79,11 @@ public GeronimoWorkManager() {
this(null, null, null, null);
}

public GeronimoWorkManager(Executor sync, Executor start, Executor sched, Collection<InflowContextHandler> inflowContextHandlers) {
public GeronimoWorkManager(Executor sync, Executor start, Executor sched, Collection<WorkContextHandler> workContextHandlers) {
syncWorkExecutorPool = sync;
startWorkExecutorPool = start;
scheduledWorkExecutorPool = sched;
this.inflowContextHandlers = inflowContextHandlers == null ? Collections.<InflowContextHandler>emptyList() : inflowContextHandlers;
this.workContextHandlers = workContextHandlers == null ? Collections.<WorkContextHandler>emptyList() : workContextHandlers;
}

public void doStart() throws Exception {
@@ -116,7 +116,7 @@ public Executor getScheduledWorkExecutorPool() {
* @see javax.resource.spi.work.WorkManager#doWork(javax.resource.spi.work.Work)
*/
public void doWork(Work work) throws WorkException {
executeWork(new WorkerContext(work, inflowContextHandlers), syncWorkExecutor, syncWorkExecutorPool);
executeWork(new WorkerContext(work, workContextHandlers), syncWorkExecutor, syncWorkExecutorPool);
}

/* (non-Javadoc)
@@ -129,7 +129,7 @@ public void doWork(
WorkListener workListener)
throws WorkException {
WorkerContext workWrapper =
new WorkerContext(work, startTimeout, execContext, workListener, inflowContextHandlers);
new WorkerContext(work, startTimeout, execContext, workListener, workContextHandlers);
workWrapper.setThreadPriority(Thread.currentThread().getPriority());
executeWork(workWrapper, syncWorkExecutor, syncWorkExecutorPool);
}
@@ -138,7 +138,7 @@ public void doWork(
* @see javax.resource.spi.work.WorkManager#startWork(javax.resource.spi.work.Work)
*/
public long startWork(Work work) throws WorkException {
WorkerContext workWrapper = new WorkerContext(work, inflowContextHandlers);
WorkerContext workWrapper = new WorkerContext(work, workContextHandlers);
workWrapper.setThreadPriority(Thread.currentThread().getPriority());
executeWork(workWrapper, startWorkExecutor, startWorkExecutorPool);
return System.currentTimeMillis() - workWrapper.getAcceptedTime();
@@ -154,7 +154,7 @@ public long startWork(
WorkListener workListener)
throws WorkException {
WorkerContext workWrapper =
new WorkerContext(work, startTimeout, execContext, workListener, inflowContextHandlers);
new WorkerContext(work, startTimeout, execContext, workListener, workContextHandlers);
workWrapper.setThreadPriority(Thread.currentThread().getPriority());
executeWork(workWrapper, startWorkExecutor, startWorkExecutorPool);
return System.currentTimeMillis() - workWrapper.getAcceptedTime();
@@ -164,7 +164,7 @@ public long startWork(
* @see javax.resource.spi.work.WorkManager#scheduleWork(javax.resource.spi.work.Work)
*/
public void scheduleWork(Work work) throws WorkException {
WorkerContext workWrapper = new WorkerContext(work, inflowContextHandlers);
WorkerContext workWrapper = new WorkerContext(work, workContextHandlers);
workWrapper.setThreadPriority(Thread.currentThread().getPriority());
executeWork(workWrapper, scheduleWorkExecutor, scheduledWorkExecutorPool);
}
@@ -179,7 +179,7 @@ public void scheduleWork(
WorkListener workListener)
throws WorkException {
WorkerContext workWrapper =
new WorkerContext(work, startTimeout, execContext, workListener, inflowContextHandlers);
new WorkerContext(work, startTimeout, execContext, workListener, workContextHandlers);
workWrapper.setThreadPriority(Thread.currentThread().getPriority());
executeWork(workWrapper, scheduleWorkExecutor, scheduledWorkExecutorPool);
}
@@ -20,7 +20,7 @@

package org.apache.geronimo.connector.work;

import javax.resource.spi.work.TransactionInflowContext;
import javax.resource.spi.work.TransactionContext;
import javax.resource.spi.work.WorkCompletedException;
import javax.transaction.xa.XAException;
import javax.transaction.InvalidTransactionException;
@@ -32,46 +32,46 @@
/**
* @version $Rev$ $Date$
*/
public class TransactionInflowContextHandler implements InflowContextHandler<TransactionInflowContext>{
public class TransactionContextHandler implements WorkContextHandler<TransactionContext>{

private final XAWork xaWork;

public TransactionInflowContextHandler(XAWork xaWork) {
public TransactionContextHandler(XAWork xaWork) {
this.xaWork = xaWork;
}

public void before(TransactionInflowContext inflowContext) throws WorkCompletedException {
if (inflowContext.getXid() != null) {
public void before(TransactionContext workContext) throws WorkCompletedException {
if (workContext.getXid() != null) {
try {
long transactionTimeout = inflowContext.getTransactionTimeout();
long transactionTimeout = workContext.getTransactionTimeout();
//translate -1 value to 0 to indicate default transaction timeout.
xaWork.begin(inflowContext.getXid(), transactionTimeout < 0 ? 0 : transactionTimeout);
xaWork.begin(workContext.getXid(), transactionTimeout < 0 ? 0 : transactionTimeout);
} catch (XAException e) {
throw (WorkCompletedException)new WorkCompletedException("Transaction import failed for xid " + inflowContext.getXid(), WorkCompletedException.TX_RECREATE_FAILED).initCause(e);
throw (WorkCompletedException)new WorkCompletedException("Transaction import failed for xid " + workContext.getXid(), WorkCompletedException.TX_RECREATE_FAILED).initCause(e);
} catch (InvalidTransactionException e) {
throw (WorkCompletedException)new WorkCompletedException("Transaction import failed for xid " + inflowContext.getXid(), WorkCompletedException.TX_RECREATE_FAILED).initCause(e);
throw (WorkCompletedException)new WorkCompletedException("Transaction import failed for xid " + workContext.getXid(), WorkCompletedException.TX_RECREATE_FAILED).initCause(e);
} catch (SystemException e) {
throw (WorkCompletedException)new WorkCompletedException("Transaction import failed for xid " + inflowContext.getXid(), WorkCompletedException.TX_RECREATE_FAILED).initCause(e);
throw (WorkCompletedException)new WorkCompletedException("Transaction import failed for xid " + workContext.getXid(), WorkCompletedException.TX_RECREATE_FAILED).initCause(e);
} catch (ImportedTransactionActiveException e) {
throw (WorkCompletedException)new WorkCompletedException("Transaction already active for xid " + inflowContext.getXid(), WorkCompletedException.TX_CONCURRENT_WORK_DISALLOWED).initCause(e);
throw (WorkCompletedException)new WorkCompletedException("Transaction already active for xid " + workContext.getXid(), WorkCompletedException.TX_CONCURRENT_WORK_DISALLOWED).initCause(e);
}
}
}

public void after(TransactionInflowContext inflowContext) throws WorkCompletedException {
if (inflowContext.getXid() != null) {
public void after(TransactionContext workContext) throws WorkCompletedException {
if (workContext.getXid() != null) {
try {
xaWork.end(inflowContext.getXid());
xaWork.end(workContext.getXid());
} catch (XAException e) {
throw (WorkCompletedException)new WorkCompletedException("Transaction end failed for xid " + inflowContext.getXid(), WorkCompletedException.TX_RECREATE_FAILED).initCause(e);
throw (WorkCompletedException)new WorkCompletedException("Transaction end failed for xid " + workContext.getXid(), WorkCompletedException.TX_RECREATE_FAILED).initCause(e);
} catch (SystemException e) {
throw (WorkCompletedException)new WorkCompletedException("Transaction end failed for xid " + inflowContext.getXid(), WorkCompletedException.TX_RECREATE_FAILED).initCause(e);
throw (WorkCompletedException)new WorkCompletedException("Transaction end failed for xid " + workContext.getXid(), WorkCompletedException.TX_RECREATE_FAILED).initCause(e);
}
}
}

public Class<TransactionInflowContext> getHandledClass() {
return TransactionInflowContext.class;
public Class<TransactionContext> getHandledClass() {
return TransactionContext.class;
}

public boolean required() {
@@ -20,17 +20,17 @@

package org.apache.geronimo.connector.work;

import javax.resource.spi.work.InflowContext;
import javax.resource.spi.work.WorkContext;
import javax.resource.spi.work.WorkCompletedException;

/**
* @version $Rev$ $Date$
*/
public interface InflowContextHandler<E extends InflowContext> {
public interface WorkContextHandler<E extends WorkContext> {

void before(E inflowContext) throws WorkCompletedException;
void before(E workContext) throws WorkCompletedException;

void after(E inflowContext) throws WorkCompletedException;
void after(E workContext) throws WorkCompletedException;

Class<E> getHandledClass();

@@ -26,9 +26,9 @@

import javax.resource.NotSupportedException;
import javax.resource.spi.work.ExecutionContext;
import javax.resource.spi.work.InflowContext;
import javax.resource.spi.work.InflowContextProvider;
import javax.resource.spi.work.TransactionInflowContext;
import javax.resource.spi.work.WorkContext;
import javax.resource.spi.work.WorkContextProvider;
import javax.resource.spi.work.TransactionContext;
import javax.resource.spi.work.Work;
import javax.resource.spi.work.WorkAdapter;
import javax.resource.spi.work.WorkCompletedException;
@@ -50,7 +50,7 @@ public class WorkerContext implements Work {

private static final Logger log = LoggerFactory.getLogger(WorkerContext.class);

private static final List<InflowContext> NO_INFLOW_CONTEXT = Collections.emptyList();
private static final List<WorkContext> NO_INFLOW_CONTEXT = Collections.emptyList();

/**
* Null WorkListener used as the default WorkListener.
@@ -123,55 +123,55 @@ public void workRejected(WorkEvent event) {
*/
private final ExecutionContext executionContext;

private final List<InflowContextHandler> inflowContextHandlers;
private final List<WorkContextHandler> workContextHandlers;


/**
* Create a WorkWrapper.
* TODO include a InflowContextLifecycleListener
* TODO include a WorkContextLifecycleListener
* @param work Work to be wrapped.
* @param inflowContextHandlers InflowContextHandlers supported by this work manager
* @param workContextHandlers WorkContextHandlers supported by this work manager
*/
public WorkerContext(Work work, Collection<InflowContextHandler> inflowContextHandlers) {
public WorkerContext(Work work, Collection<WorkContextHandler> workContextHandlers) {
adaptee = work;
this.inflowContextHandlers = new ArrayList<InflowContextHandler>(inflowContextHandlers);
this.workContextHandlers = new ArrayList<WorkContextHandler>(workContextHandlers);
executionContext = null;
workListener = NULL_WORK_LISTENER;
}

/**
* Create a WorkWrapper with the specified execution context.
*
* TODO include a InflowContextLifecycleListener
* TODO include a WorkContextLifecycleListener
* @param aWork Work to be wrapped.
* @param aStartTimeout a time duration (in milliseconds) within which the
* execution of the Work instance must start.
* @param execContext an object containing the execution context with which
* the submitted Work instance must be executed.
* @param workListener an object which would be notified when the various
* @param inflowContextHandlers InflowContextHandlers supported by this work manager
* @throws javax.resource.spi.work.WorkRejectedException if executionContext supplied yet Work implements InflowContextProvider
* @param workContextHandlers WorkContextHandlers supported by this work manager
* @throws javax.resource.spi.work.WorkRejectedException if executionContext supplied yet Work implements WorkContextProvider
*/
public WorkerContext(Work aWork,
long aStartTimeout,
ExecutionContext execContext,
WorkListener workListener, Collection<InflowContextHandler> inflowContextHandlers) throws WorkRejectedException {
WorkListener workListener, Collection<WorkContextHandler> workContextHandlers) throws WorkRejectedException {
adaptee = aWork;
startTimeOut = aStartTimeout;
if (null == workListener) {
this.workListener = NULL_WORK_LISTENER;
} else {
this.workListener = workListener;
}
if (aWork instanceof InflowContextProvider) {
if (aWork instanceof WorkContextProvider) {
if (execContext != null) {
throw new WorkRejectedException("Execution context provided but Work implements InflowContextProvider");
throw new WorkRejectedException("Execution context provided but Work implements WorkContextProvider");
}
executionContext = null;
} else {
executionContext = execContext;
}
this.inflowContextHandlers = new ArrayList<InflowContextHandler>(inflowContextHandlers);
this.workContextHandlers = new ArrayList<WorkContextHandler>(workContextHandlers);
}

/* (non-Javadoc)
@@ -305,59 +305,59 @@ public void run() {
//Implementation note: we assume this is being called without an interesting TransactionContext,
//and ignore/replace whatever is associated with the current thread.
try {
List<InflowContext> inflowContexts = NO_INFLOW_CONTEXT;
List<WorkContext> workContexts = NO_INFLOW_CONTEXT;
if (executionContext != null) {
TransactionInflowContext txInflowContext = new TransactionInflowContext();
TransactionContext txWorkContext = new TransactionContext();
try {
txInflowContext.setTransactionTimeout(executionContext.getTransactionTimeout());
txWorkContext.setTransactionTimeout(executionContext.getTransactionTimeout());
} catch (NotSupportedException e) {
throw new WorkRejectedException("Could not read tx timeout");
}
inflowContexts = Collections.<InflowContext>singletonList(txInflowContext);
} else if (adaptee instanceof InflowContextProvider) {
inflowContexts = ((InflowContextProvider) adaptee).getInflowContexts();
workContexts = Collections.<WorkContext>singletonList(txWorkContext);
} else if (adaptee instanceof WorkContextProvider) {
workContexts = ((WorkContextProvider) adaptee).getWorkContexts();
}
List<InflowContextHandler> sortedHandlers = new ArrayList<InflowContextHandler>(inflowContexts.size());
for (InflowContext inflowContext : inflowContexts) {
List<WorkContextHandler> sortedHandlers = new ArrayList<WorkContextHandler>(workContexts.size());
for (WorkContext workContext : workContexts) {
boolean found = false;
for (Iterator<InflowContextHandler> it = inflowContextHandlers.iterator(); it.hasNext();) {
InflowContextHandler inflowContextHandler = it.next();
for (Iterator<WorkContextHandler> it = workContextHandlers.iterator(); it.hasNext();) {
WorkContextHandler workContextHandler = it.next();
//TODO is this the right way around?
if (inflowContext.getClass().isAssignableFrom(inflowContextHandler.getHandledClass())) {
if (workContext.getClass().isAssignableFrom(workContextHandler.getHandledClass())) {
it.remove();
sortedHandlers.add(inflowContextHandler);
sortedHandlers.add(workContextHandler);
found = true;
break;
}
}
if (!found) {
throw new WorkCompletedException("Duplicate or unhandled InflowContext: " + inflowContext);
throw new WorkCompletedException("Duplicate or unhandled WorkContext: " + workContext);
}
}
for (Iterator<InflowContextHandler> it = inflowContextHandlers.iterator(); it.hasNext();) {
InflowContextHandler inflowContextHandler = it.next();
if (!inflowContextHandler.required()) {
for (Iterator<WorkContextHandler> it = workContextHandlers.iterator(); it.hasNext();) {
WorkContextHandler workContextHandler = it.next();
if (!workContextHandler.required()) {
it.remove();
}
}
// TODO use a InflowContextLifecycleListener
// TODO use a WorkContextLifecycleListener

int i = 0;
for (InflowContext inflowContext : inflowContexts) {
sortedHandlers.get(i++).before(inflowContext);
for (WorkContext workContext : workContexts) {
sortedHandlers.get(i++).before(workContext);
}
for (InflowContextHandler inflowContextHandler: inflowContextHandlers) {
inflowContextHandler.before(null);
for (WorkContextHandler workContextHandler: workContextHandlers) {
workContextHandler.before(null);
}
try {
adaptee.run();
} finally {
int j = 0;
for (InflowContext inflowContext : inflowContexts) {
sortedHandlers.get(j++).after(inflowContext);
for (WorkContext workContext : workContexts) {
sortedHandlers.get(j++).after(workContext);
}
for (InflowContextHandler inflowContextHandler: inflowContextHandlers) {
inflowContextHandler.after(null);
for (WorkContextHandler workContextHandler: workContextHandlers) {
workContextHandler.after(null);
}
}

0 comments on commit c69cfd0

Please sign in to comment.