Search in sources :

Example 1 with OptimisticLock

use of org.jboss.stm.internal.optimistic.OptimisticLock 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);
            }
        }
    }
}
Also used : TopLevelAction(com.arjuna.ats.arjuna.TopLevelAction) Timeout(org.jboss.stm.annotations.Timeout) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) OptimisticLock(org.jboss.stm.internal.optimistic.OptimisticLock) Lock(com.arjuna.ats.txoj.Lock) ReadLock(org.jboss.stm.annotations.ReadLock) WriteLock(org.jboss.stm.annotations.WriteLock) AtomicAction(com.arjuna.ats.arjuna.AtomicAction) TransactionException(org.jboss.stm.TransactionException) LockException(org.jboss.stm.LockException) Retry(org.jboss.stm.annotations.Retry) OptimisticLock(org.jboss.stm.internal.optimistic.OptimisticLock)

Aggregations

AtomicAction (com.arjuna.ats.arjuna.AtomicAction)1 TopLevelAction (com.arjuna.ats.arjuna.TopLevelAction)1 Lock (com.arjuna.ats.txoj.Lock)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 LockException (org.jboss.stm.LockException)1 TransactionException (org.jboss.stm.TransactionException)1 ReadLock (org.jboss.stm.annotations.ReadLock)1 Retry (org.jboss.stm.annotations.Retry)1 Timeout (org.jboss.stm.annotations.Timeout)1 WriteLock (org.jboss.stm.annotations.WriteLock)1 OptimisticLock (org.jboss.stm.internal.optimistic.OptimisticLock)1