protected Object intercept()

in meecrowave-jta/src/main/java/org/apache/meecrowave/jta/InterceptorBase.java [53:129]


    protected Object intercept(final InvocationContext ic) throws Exception {
        final boolean forbidsUt = doesForbidUtUsage();
        final RuntimeException oldEx;
        final IllegalStateException illegalStateException;
        if (forbidsUt) {
            illegalStateException = ILLEGAL_STATE_EXCEPTION;
            oldEx = error(illegalStateException);
        } else {
            illegalStateException = null;
            oldEx = null;
        }

        State state = null;
        try {
            state = start();
            final Object proceed = ic.proceed();
            commit(state); // force commit there to ensure we can catch synchro exceptions
            return proceed;
        } catch (final Exception e) {
            if (illegalStateException == e) {
                throw e;
            }

            Exception error = unwrap(e);
            if (error != null && (!config.isHandleExceptionOnlyForClient() || isNewTransaction(state))) {
                final Method method = ic.getMethod();
                if (rollback == null) {
                    synchronized (this) {
                        if (rollback == null) {
                            rollback = new ConcurrentHashMap<>();
                        }
                    }
                }
                Boolean doRollback = rollback.get(method);
                if (doRollback != null) {
                    if (doRollback && isTransactionActive(state.current)) {
                        setRollbackOnly();
                    }
                } else {
                    // computed lazily but we could cache it later for sure if that's really a normal case
                    final AnnotatedType<?> annotatedType = CDI.current().getBeanManager().createAnnotatedType(method.getDeclaringClass());
                    Transactional tx = null;
                    for (final AnnotatedMethod<?> m : annotatedType.getMethods()) {
                        if (method.equals(m.getJavaMember())) {
                            tx = m.getAnnotation(Transactional.class);
                            break;
                        }
                    }
                    if (tx == null) {
                        tx = annotatedType.getAnnotation(Transactional.class);
                    }
                    if (tx != null) {
                        doRollback = new ExceptionPriotiryRules(tx.rollbackOn(), tx.dontRollbackOn()).accept(error, method.getExceptionTypes());
                        rollback.putIfAbsent(method, doRollback);
                        if (doRollback && isTransactionActive(state.current)) {
                            setRollbackOnly();
                        }
                    }
                }
            }
            try {
                commit(state);
            } catch (final Exception ex) {
                // no-op: swallow to keep the right exception
                final Logger logger = Logger.getLogger(getClass().getName());
                if (logger.isLoggable(Level.FINE)) {
                    logger.fine("Swallowing: " + ex.getMessage());
                }
            }

            throw new TransactionalException(e.getMessage(), error);
        } finally {
            if (forbidsUt) {
                resetError(oldEx);
            }
        }
    }