Search in sources :

Example 16 with AroundInvoke

use of javax.interceptor.AroundInvoke in project Payara by payara.

the class TransactionalInterceptorRequired method transactional.

@AroundInvoke
public Object transactional(InvocationContext ctx) throws Exception {
    _logger.log(FINE, CDI_JTA_REQUIRED);
    if (isLifeCycleMethod(ctx)) {
        return proceed(ctx);
    }
    setTransactionalTransactionOperationsManger(false);
    try {
        boolean isTransactionStarted = false;
        if (getTransactionManager().getTransaction() == null) {
            _logger.log(FINE, CDI_JTA_MBREQUIRED);
            try {
                getTransactionManager().begin();
                TransactionManager transactionManager = getTransactionManager();
                if (transactionManager instanceof TransactionManagerHelper) {
                    ((TransactionManagerHelper) transactionManager).preInvokeTx(true);
                }
            } catch (Exception exception) {
                _logger.log(FINE, CDI_JTA_MBREQUIREDBT, exception);
                throw new TransactionalException("Managed bean with Transactional annotation and TxType of REQUIRED " + "encountered exception during begin " + exception, exception);
            }
            isTransactionStarted = true;
        }
        Object proceed = null;
        try {
            proceed = proceed(ctx);
        } finally {
            if (isTransactionStarted) {
                try {
                    TransactionManager transactionManager = getTransactionManager();
                    if (transactionManager instanceof TransactionManagerHelper) {
                        ((TransactionManagerHelper) transactionManager).postInvokeTx(false, true);
                    }
                    // Exception handling for proceed method call above can set TM/TRX as setRollbackOnly
                    if (getTransactionManager().getTransaction().getStatus() == STATUS_MARKED_ROLLBACK) {
                        getTransactionManager().rollback();
                    } else {
                        getTransactionManager().commit();
                    }
                } catch (Exception exception) {
                    _logger.log(FINE, CDI_JTA_MBREQUIREDCT, exception);
                    throw new TransactionalException("Managed bean with Transactional annotation and TxType of REQUIRED " + "encountered exception during commit " + exception, exception);
                }
            }
        }
        return proceed;
    } finally {
        resetTransactionOperationsManager();
    }
}
Also used : TransactionManagerHelper(com.sun.enterprise.transaction.TransactionManagerHelper) TransactionalException(javax.transaction.TransactionalException) TransactionManager(javax.transaction.TransactionManager) TransactionalException(javax.transaction.TransactionalException) AroundInvoke(javax.interceptor.AroundInvoke)

Example 17 with AroundInvoke

use of javax.interceptor.AroundInvoke in project Payara by payara.

the class TransactionalInterceptorMandatory method transactional.

@AroundInvoke
public Object transactional(InvocationContext ctx) throws Exception {
    _logger.log(FINE, CDI_JTA_MANDATORY);
    if (isLifeCycleMethod(ctx)) {
        return proceed(ctx);
    }
    setTransactionalTransactionOperationsManger(false);
    try {
        if (getTransactionManager().getTransaction() == null) {
            throw new TransactionalException("TransactionRequiredException thrown from TxType.MANDATORY transactional interceptor.", new TransactionRequiredException("Managed bean with Transactional annotation and TxType of " + "MANDATORY called outside of a transaction context"));
        }
        return proceed(ctx);
    } finally {
        resetTransactionOperationsManager();
    }
}
Also used : TransactionRequiredException(javax.transaction.TransactionRequiredException) TransactionalException(javax.transaction.TransactionalException) AroundInvoke(javax.interceptor.AroundInvoke)

Example 18 with AroundInvoke

use of javax.interceptor.AroundInvoke in project Payara by payara.

the class TransactionalInterceptorNotSupported method transactional.

@AroundInvoke
public Object transactional(InvocationContext ctx) throws Exception {
    _logger.log(FINE, CDI_JTA_NOTSUPPORTED);
    if (isLifeCycleMethod(ctx)) {
        return proceed(ctx);
    }
    setTransactionalTransactionOperationsManger(true);
    try {
        Transaction transaction = null;
        if (getTransactionManager().getTransaction() != null) {
            _logger.log(FINE, CDI_JTA_MBNOTSUPPORTED);
            try {
                transaction = getTransactionManager().suspend();
            } catch (Exception exception) {
                _logger.log(FINE, CDI_JTA_MBNOTSUPPORTEDTX, exception);
                throw new TransactionalException("Managed bean with Transactional annotation and TxType of NOT_SUPPORTED " + "called inside a transaction context.  Suspending transaction failed due to " + exception, exception);
            }
        }
        Object proceed = null;
        try {
            proceed = proceed(ctx);
        } finally {
            if (transaction != null) {
                try {
                    getTransactionManager().resume(transaction);
                } catch (Exception exception) {
                    throw new TransactionalException("Managed bean with Transactional annotation and TxType of NOT_SUPPORTED " + "encountered exception during resume " + exception, exception);
                }
            }
        }
        return proceed;
    } finally {
        resetTransactionOperationsManager();
    }
}
Also used : Transaction(javax.transaction.Transaction) TransactionalException(javax.transaction.TransactionalException) TransactionalException(javax.transaction.TransactionalException) AroundInvoke(javax.interceptor.AroundInvoke)

Example 19 with AroundInvoke

use of javax.interceptor.AroundInvoke in project Payara by payara.

the class CacheResultInterceptor method cacheResult.

@AroundInvoke
@SuppressWarnings("unchecked")
public Object cacheResult(InvocationContext ctx) throws Throwable {
    if (!isEnabled()) {
        return ctx.proceed();
    }
    // get my annotation
    CacheResult annotation = ctx.getMethod().getAnnotation(CacheResult.class);
    PayaraCacheKeyInvocationContext<CacheResult> pctx = new PayaraCacheKeyInvocationContext<>(ctx, annotation);
    CacheResolverFactory resolverF = pctx.getFactory();
    CacheResolver cacheResolver = resolverF.getCacheResolver(pctx);
    Cache cache = cacheResolver.resolveCache(pctx);
    boolean cacheExceptions = (annotation.exceptionCacheName() != null && annotation.exceptionCacheName().length() > 0);
    CacheKeyGenerator generator = pctx.getGenerator();
    GeneratedCacheKey key = generator.generateCacheKey(pctx);
    if (!annotation.skipGet()) {
        Object cacheResult = cache.get(key);
        if (cacheResult != null) {
            return cacheResult;
        } else {
            // check exception cache
            if (cacheExceptions) {
                Cache exceptionCache = resolverF.getExceptionCacheResolver(pctx).resolveCache(pctx);
                Throwable e = (Throwable) exceptionCache.get(key);
                if (e != null) {
                    throw e;
                }
            }
        }
    }
    // call the method
    Object result = null;
    try {
        result = ctx.proceed();
        cache.put(key, result);
    } catch (Throwable e) {
        if (cacheExceptions) {
            Cache exceptionCache = resolverF.getExceptionCacheResolver(pctx).resolveCache(pctx);
            if (shouldICache(annotation.cachedExceptions(), annotation.nonCachedExceptions(), e, true)) {
                exceptionCache.put(key, e);
            }
        }
        throw e;
    }
    return result;
}
Also used : CacheResult(javax.cache.annotation.CacheResult) CacheResolver(javax.cache.annotation.CacheResolver) PayaraCacheKeyInvocationContext(fish.payara.cdi.jsr107.implementation.PayaraCacheKeyInvocationContext) CacheKeyGenerator(javax.cache.annotation.CacheKeyGenerator) CacheResolverFactory(javax.cache.annotation.CacheResolverFactory) Cache(javax.cache.Cache) GeneratedCacheKey(javax.cache.annotation.GeneratedCacheKey) AroundInvoke(javax.interceptor.AroundInvoke)

Example 20 with AroundInvoke

use of javax.interceptor.AroundInvoke in project Payara by payara.

the class RolesPermittedInterceptor method method.

/**
 * Method invoked whenever a method annotated with @Roles, or a method
 * within a class annotated with @Roles is called.
 *
 * @param invocationContext Context provided by Weld.
 * @return Proceed to next interceptor in chain.
 * @throws java.lang.Exception
 * @throws fish.payara.cdi.auth.roles.CallerAccessException if access is not permitted
 */
@AroundInvoke
public Object method(InvocationContext invocationContext) throws Exception {
    RolesPermitted roles = getRolesPermitted(invocationContext);
    boolean isAccessPermitted = checkAccessPermitted(roles, invocationContext);
    if (!isAccessPermitted) {
        throw new CallerAccessException("Caller was not permitted access to a protected resource");
    }
    return invocationContext.proceed();
}
Also used : RolesPermitted(fish.payara.cdi.auth.roles.RolesPermitted) CallerAccessException(fish.payara.cdi.auth.roles.CallerAccessException) AroundInvoke(javax.interceptor.AroundInvoke)

Aggregations

AroundInvoke (javax.interceptor.AroundInvoke)52 Method (java.lang.reflect.Method)10 InvocationManager (org.glassfish.api.invocation.InvocationManager)6 FaultToleranceService (fish.payara.microprofile.faulttolerance.FaultToleranceService)5 FallbackPolicy (fish.payara.microprofile.faulttolerance.interceptors.fallback.FallbackPolicy)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)5 TransactionalException (javax.transaction.TransactionalException)5 Config (org.eclipse.microprofile.config.Config)5 Fallback (org.eclipse.microprofile.faulttolerance.Fallback)5 PayaraCacheKeyInvocationContext (fish.payara.cdi.jsr107.implementation.PayaraCacheKeyInvocationContext)4 Meter (com.codahale.metrics.Meter)2 Timer (com.codahale.metrics.Timer)2 TransactionManagerHelper (com.sun.enterprise.transaction.TransactionManagerHelper)2 CallerAccessException (fish.payara.cdi.auth.roles.CallerAccessException)2 RolesPermitted (fish.payara.cdi.auth.roles.RolesPermitted)2 Parameter (java.lang.reflect.Parameter)2 Principal (java.security.Principal)2 ArrayList (java.util.ArrayList)2 NoSuchElementException (java.util.NoSuchElementException)2 Message (javax.jms.Message)2