use of org.jboss.stm.annotations.Timeout in project narayana by jbosstm.
the class InvocationHandler method invoke.
public Object invoke(Object proxy, java.lang.reflect.Method method, Object[] args) throws Throwable {
if (_txObject == null)
throw new LockException("Transactional object is null!");
AtomicAction currentTx = null;
synchronized (_txObject) {
synchronized (_theObject) {
AtomicAction act = null;
if (_nestedTransactions) {
act = new AtomicAction();
act.begin();
} else {
if (_nestedTopLevel) {
act = new TopLevelAction();
act.begin();
}
}
try {
LockInformation cachedLock = _cachedMethods.get(method);
if (BasicAction.Current() != null) {
Method theMethod = null;
/*
* Look for the corresponding method in the original object and
* check the annotations applied there.
*
* Check to see if we've cached this before.
*/
int lockType = -1;
boolean lockFree = false;
boolean transactionFree = false;
if (cachedLock == null) {
for (Method mt : _methods) {
if (mt.getName().equals(method.getName())) {
if (mt.getReturnType().equals(method.getReturnType())) {
if (Arrays.equals(mt.getParameterTypes(), method.getParameterTypes()))
theMethod = mt;
}
}
}
if (theMethod == null)
throw new LockException("Could not locate method " + method);
if (theMethod.isAnnotationPresent(ReadLock.class))
lockType = LockMode.READ;
else {
if (theMethod.isAnnotationPresent(WriteLock.class))
lockType = LockMode.WRITE;
else {
if (theMethod.isAnnotationPresent(TransactionFree.class))
transactionFree = true;
else {
if (theMethod.isAnnotationPresent(LockFree.class))
lockFree = true;
}
}
}
if (!lockFree && !transactionFree) {
int timeout = LockManager.defaultSleepTime;
int retry = LockManager.defaultRetry;
if (theMethod.isAnnotationPresent(Timeout.class))
timeout = theMethod.getAnnotation(Timeout.class).period();
if (theMethod.isAnnotationPresent(Retry.class))
retry = theMethod.getAnnotation(Retry.class).count();
if (// default to WRITE
lockType == -1)
lockType = LockMode.WRITE;
cachedLock = new LockInformation(lockType, timeout, retry);
_cachedMethods.put(method, cachedLock);
} else {
if (transactionFree)
currentTx = AtomicAction.suspend();
}
}
if (!lockFree && !transactionFree) {
int result = _txObject.setlock((_optimistic ? new OptimisticLock(cachedLock._lockType) : new Lock(cachedLock._lockType)), cachedLock._retry, cachedLock._timeout);
if (result != GRANTED) {
throw new LockException(Thread.currentThread() + " could not set " + LockMode.stringForm(cachedLock._lockType) + " lock. Got: " + LockResult.stringForm(result));
}
}
}
try {
return method.invoke(_theObject, args);
} catch (InvocationTargetException e) {
if (txojLogger.logger.isTraceEnabled()) {
Throwable ae = e.getCause() != null ? e.getCause() : e;
txojLogger.logger.tracef("STM InvocationHandler::invoke application method %s threw exception %s", method.getName(), ae.getMessage());
}
throw e.getCause() != null ? e.getCause() : e;
}
} finally {
if (act != null) {
int status = act.commit();
if ((status != ActionStatus.COMMITTED) && (status != ActionStatus.COMMITTING)) {
if (currentTx != null)
AtomicAction.resume(currentTx);
throw new TransactionException("Failed to commit container transaction!", status);
}
}
if (currentTx != null)
AtomicAction.resume(currentTx);
}
}
}
}
Aggregations