Skip to content
Permalink
Browse files
GERONIMO-4360 add HintsContextHander, fix a lot of small bugs with wo…
…rk contexts. changes api slightly, needs server code change too

git-svn-id: https://svn.apache.org/repos/asf/geronimo/components/txmanager/trunk@925908 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
djencks committed Mar 21, 2010
1 parent 6baf86c commit 2d2901bc58f185b6bf58f484b7bc39f97699cd78
Showing 7 changed files with 93 additions and 16 deletions.
@@ -23,6 +23,7 @@
import javax.resource.spi.work.WorkManager;
import javax.resource.spi.work.WorkContext;
import javax.transaction.TransactionSynchronizationRegistry;
import org.apache.geronimo.connector.work.GeronimoWorkManager;

/**
* GBean BootstrapContext implementation that refers to externally configured WorkManager
@@ -31,25 +32,29 @@
* @version $Rev$ $Date$
*/
public class GeronimoBootstrapContext implements javax.resource.spi.BootstrapContext {
private final WorkManager workManager;
private final GeronimoWorkManager workManager;
private final XATerminator xATerminator;
private final TransactionSynchronizationRegistry transactionSynchronizationRegistry;

/**
* Default constructor for use as a GBean Endpoint.
*/
public GeronimoBootstrapContext() {
workManager = null;
xATerminator = null;
transactionSynchronizationRegistry = null;
}

/**
* Normal constructor for use as a GBean.
* @param workManager
* @param xaTerminator
* @param transactionSynchronizationRegistry
*/
public GeronimoBootstrapContext(WorkManager workManager, XATerminator xaTerminator) {
public GeronimoBootstrapContext(GeronimoWorkManager workManager, XATerminator xaTerminator, TransactionSynchronizationRegistry transactionSynchronizationRegistry) {
this.workManager = workManager;
this.xATerminator = xaTerminator;
this.transactionSynchronizationRegistry = transactionSynchronizationRegistry;
}


@@ -75,11 +80,11 @@ public Timer createTimer() throws UnavailableException {
}

public TransactionSynchronizationRegistry getTransactionSynchronizationRegistry() {
return null;
return transactionSynchronizationRegistry;
}

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

}
@@ -24,6 +24,7 @@
import javax.resource.spi.work.ExecutionContext;
import javax.resource.spi.work.Work;
import javax.resource.spi.work.WorkCompletedException;
import javax.resource.spi.work.WorkContext;
import javax.resource.spi.work.WorkException;
import javax.resource.spi.work.WorkListener;
import javax.resource.spi.work.WorkManager;
@@ -207,4 +208,13 @@ private void executeWork(WorkerContext work, WorkExecutor workExecutor, Executor
}
}

public boolean isContextSupported(Class<? extends WorkContext> aClass) {
for (WorkContextHandler workContextHandler: workContextHandlers) {
if (workContextHandler.supports(aClass)) {
return true;
}
}
return false;
}

}
@@ -0,0 +1,46 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/


package org.apache.geronimo.connector.work;

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

/**
* @version $Rev$ $Date$
*/
public class HintsContextHandler implements WorkContextHandler<HintsContext> {

public void before(HintsContext workContext) throws WorkCompletedException {
}

public void after(HintsContext workContext) throws WorkCompletedException {
}

public boolean supports(Class<? extends WorkContext> clazz) {
return HintsContext.class.isAssignableFrom(clazz);
}

public boolean required() {
return false;
}

}
@@ -22,6 +22,7 @@

import javax.resource.spi.work.TransactionContext;
import javax.resource.spi.work.WorkCompletedException;
import javax.resource.spi.work.WorkContext;
import javax.transaction.xa.XAException;
import javax.transaction.InvalidTransactionException;
import javax.transaction.SystemException;
@@ -70,8 +71,8 @@ public void after(TransactionContext workContext) throws WorkCompletedException
}
}

public Class<TransactionContext> getHandledClass() {
return TransactionContext.class;
public boolean supports(Class<? extends WorkContext> clazz) {
return TransactionContext.class.isAssignableFrom(clazz);
}

public boolean required() {
@@ -32,7 +32,7 @@

void after(E workContext) throws WorkCompletedException;

Class<E> getHandledClass();
boolean supports(Class<? extends WorkContext> clazz);

boolean required();

@@ -312,9 +312,11 @@ public void run() {
try {
txWorkContext.setTransactionTimeout(executionContext.getTransactionTimeout());
} catch (NotSupportedException e) {
throw new WorkRejectedException("Could not read tx timeout");
//not set, continue to not set it.
}
txWorkContext.setXid(executionContext.getXid());
workContexts = Collections.<WorkContext>singletonList(txWorkContext);
log.info("Translated ExecutionContext to TransactionContext");
} else if (adaptee instanceof WorkContextProvider) {
workContexts = ((WorkContextProvider) adaptee).getWorkContexts();
}
@@ -323,41 +325,54 @@ public void run() {
boolean found = false;
for (Iterator<WorkContextHandler> it = workContextHandlers.iterator(); it.hasNext();) {
WorkContextHandler workContextHandler = it.next();
//TODO is this the right way around?
if (workContext.getClass().isAssignableFrom(workContextHandler.getHandledClass())) {
log.info("sorting WorkContextHandler: " + workContextHandler + " for work context: " + workContext);
if (workContextHandler.supports(workContext.getClass())) {
it.remove();
log.info("adding sorted WorkContextHandler: " + workContextHandler);
sortedHandlers.add(workContextHandler);
found = true;
break;
}
}
if (!found) {
throw new WorkCompletedException("Duplicate or unhandled WorkContext: " + workContext, WorkContextErrorCodes.UNSUPPORTED_CONTEXT_TYPE);
for (WorkContextHandler workContextHandler: sortedHandlers) {
if (workContextHandler.supports(workContext.getClass())) {
throw new WorkCompletedException("Duplicate WorkContext: " + workContext, WorkContextErrorCodes.DUPLICATE_CONTEXTS);
}
}
throw new WorkCompletedException("Unhandled WorkContext: " + workContext, WorkContextErrorCodes.UNSUPPORTED_CONTEXT_TYPE);
}
}
for (Iterator<WorkContextHandler> it = workContextHandlers.iterator(); it.hasNext();) {
WorkContextHandler workContextHandler = it.next();
if (!workContextHandler.required()) {
log.info("Removing non-required WorkContextHandler with no context: " + workContextHandler);
it.remove();
}
}
// TODO use a WorkContextLifecycleListener

int i = 0;
for (WorkContext workContext : workContexts) {
sortedHandlers.get(i++).before(workContext);
WorkContextHandler contextHandler = sortedHandlers.get(i++);
log.info("calling before on WorkContextHandler: " + contextHandler + " with workContext: " + workContext);
contextHandler.before(workContext);
}
for (WorkContextHandler workContextHandler: workContextHandlers) {
log.info("calling before on WorkContextHandler: " + workContextHandler + " with null workContext");
workContextHandler.before(null);
}
try {
adaptee.run();
} finally {
int j = 0;
for (WorkContext workContext : workContexts) {
sortedHandlers.get(j++).after(workContext);
WorkContextHandler contextHandler = sortedHandlers.get(j++);
log.info("calling after on WorkContextHandler: " + contextHandler + " with workContext: " + workContext);
contextHandler.after(workContext);
}
for (WorkContextHandler workContextHandler: workContextHandlers) {
log.info("calling after on WorkContextHandler: " + workContextHandler + " with null workContext");
workContextHandler.after(null);
}
}
@@ -99,7 +99,7 @@ public void testGetSetWorkManager() throws Exception {
GeronimoTransactionManager transactionManager = new GeronimoTransactionManager();
TransactionContextHandler txWorkContextHandler = new TransactionContextHandler(transactionManager);
GeronimoWorkManager manager = new GeronimoWorkManager(pool, pool, pool, Collections.<WorkContextHandler>singletonList(txWorkContextHandler));
GeronimoBootstrapContext context = new GeronimoBootstrapContext(manager, transactionManager);
GeronimoBootstrapContext context = new GeronimoBootstrapContext(manager, transactionManager, transactionManager);
WorkManager wm = context.getWorkManager();

assertSame("Make sure it is the same object", manager, wm);
@@ -112,7 +112,7 @@ public void testGetSetXATerminator() throws Exception {
GeronimoTransactionManager transactionManager = new GeronimoTransactionManager();
TransactionContextHandler txWorkContextHandler = new TransactionContextHandler(transactionManager);
GeronimoWorkManager manager = new GeronimoWorkManager(pool, pool, pool, Collections.<WorkContextHandler>singletonList(txWorkContextHandler));
GeronimoBootstrapContext context = new GeronimoBootstrapContext(manager, transactionManager);
GeronimoBootstrapContext context = new GeronimoBootstrapContext(manager, transactionManager, transactionManager);
XATerminator xat = context.getXATerminator();

assertSame("Make sure it is the same object", transactionManager, xat);
@@ -122,7 +122,7 @@ public void testGetSetXATerminator() throws Exception {
* Tests getTimer
*/
public void testGetTimer() throws Exception {
GeronimoBootstrapContext context = new GeronimoBootstrapContext(null, null);
GeronimoBootstrapContext context = new GeronimoBootstrapContext(null, null, null);
Timer t = context.createTimer();
assertNotNull("Object is not null", t);
}

0 comments on commit 2d2901b

Please sign in to comment.