Search in sources :

Example 26 with AroundInvoke

use of javax.interceptor.AroundInvoke in project wildfly by wildfly.

the class MyBaseInterceptor method baseInvoke.

@AroundInvoke
public Object baseInvoke(InvocationContext ctx) throws Exception {
    baseSession2.doit();
    if (baseTm == null) {
        throw new RuntimeException("tm was null");
    }
    if (baseDs == null) {
        throw new RuntimeException("ds was null");
    }
    if (baseEm == null) {
        throw new RuntimeException("em was null");
    }
    if (baseSession == null) {
        throw new RuntimeException("session was null");
    }
    if (baseFactory == null) {
        throw new RuntimeException("factory was null");
    }
    if (baseSessionFactory == null) {
        throw new RuntimeException("sessionFactory was null");
    }
    baseSession2Method.doit();
    if (baseTmMethod == null) {
        throw new RuntimeException("tm was null");
    }
    if (baseDsMethod == null) {
        throw new RuntimeException("ds was null");
    }
    if (baseEmMethod == null) {
        throw new RuntimeException("em was null");
    }
    if (baseSessionMethod == null) {
        throw new RuntimeException("session was null");
    }
    if (baseFactoryMethod == null) {
        throw new RuntimeException("factory was null");
    }
    if (baseSessionFactoryMethod == null) {
        throw new RuntimeException("sessionFactory was null");
    }
    ArrayList list = (ArrayList) ctx.proceed();
    list.add(0, "MyBaseInterceptor");
    return list;
}
Also used : ArrayList(java.util.ArrayList) AroundInvoke(javax.interceptor.AroundInvoke)

Example 27 with AroundInvoke

use of javax.interceptor.AroundInvoke in project tomee by apache.

the class InterceptorMdbBean method aroundInvoke.

@AroundInvoke
public Object aroundInvoke(final InvocationContext ctx) throws Exception {
    final Object[] objArr = ctx.getParameters();
    final Message msg = (Message) objArr[0];
    msg.setBooleanProperty("MethodLevelBusinessMethodInterception", true);
    ctx.setParameters(objArr);
    return ctx.proceed();
}
Also used : Message(javax.jms.Message) AroundInvoke(javax.interceptor.AroundInvoke)

Example 28 with AroundInvoke

use of javax.interceptor.AroundInvoke in project tomee by apache.

the class MdbInterceptor method mdbInterceptor.

@AroundInvoke
public Object mdbInterceptor(final InvocationContext ctx) throws Exception {
    final Object[] objArr = ctx.getParameters();
    final Message msg = (Message) objArr[0];
    msg.clearProperties();
    msg.setBooleanProperty("ClassLevelBusinessMethodInterception", true);
    ctx.setParameters(objArr);
    return ctx.proceed();
}
Also used : Message(javax.jms.Message) AroundInvoke(javax.interceptor.AroundInvoke)

Example 29 with AroundInvoke

use of javax.interceptor.AroundInvoke in project wildfly by wildfly.

the class ServerSecurityInterceptor method aroundInvoke.

@AroundInvoke
public Object aroundInvoke(final InvocationContext invocationContext) throws Exception {
    Principal desiredUser = null;
    RealmUser connectionUser = null;
    Map<String, Object> contextData = invocationContext.getContextData();
    if (contextData.containsKey(DELEGATED_USER_KEY)) {
        desiredUser = new SimplePrincipal((String) contextData.get(DELEGATED_USER_KEY));
        Connection con = RemotingContext.getConnection();
        if (con != null) {
            SecurityIdentity localIdentity = con.getLocalIdentity();
            if (localIdentity != null) {
                connectionUser = new RealmUser(localIdentity.getPrincipal().getName());
            }
        } else {
            throw new IllegalStateException("Delegation user requested but no user on connection found.");
        }
    }
    SecurityContext cachedSecurityContext = null;
    boolean contextSet = false;
    try {
        if (desiredUser != null && connectionUser != null && (desiredUser.getName().equals(connectionUser.getName()) == false)) {
            try {
                // The final part of this check is to verify that the change does actually indicate a change in user.
                // We have been requested to switch user and have successfully identified the user from the connection
                // so now we attempt the switch.
                cachedSecurityContext = SecurityContextAssociation.getSecurityContext();
                final SecurityContext nextContext = SecurityContextFactory.createSecurityContext(desiredUser, new CurrentUserCredential(connectionUser.getName()), new Subject(), "fooSecurityDomain");
                SecurityContextAssociation.setSecurityContext(nextContext);
                // keep track that we switched the security context
                contextSet = true;
                RemotingContext.clear();
            } catch (Exception e) {
                LOGGER.error("Failed to switch security context for user", e);
                // Don't propagate the exception stacktrace back to the client for security reasons
                throw new EJBAccessException("Unable to attempt switching of user.");
            }
        }
        return invocationContext.proceed();
    } finally {
        // switch back to original security context
        if (contextSet) {
            SecurityContextAssociation.setSecurityContext(cachedSecurityContext);
        }
    }
}
Also used : IllegalStateException(javax.resource.spi.IllegalStateException) RealmUser(org.jboss.as.core.security.RealmUser) Connection(org.jboss.remoting3.Connection) Subject(javax.security.auth.Subject) EJBAccessException(javax.ejb.EJBAccessException) IllegalStateException(javax.resource.spi.IllegalStateException) EJBAccessException(javax.ejb.EJBAccessException) SecurityIdentity(org.wildfly.security.auth.server.SecurityIdentity) SecurityContext(org.jboss.security.SecurityContext) Principal(java.security.Principal) SimplePrincipal(org.jboss.security.SimplePrincipal) SimplePrincipal(org.jboss.security.SimplePrincipal) AroundInvoke(javax.interceptor.AroundInvoke)

Example 30 with AroundInvoke

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

the class BulkheadInterceptor method intercept.

@AroundInvoke
public Object intercept(InvocationContext invocationContext) throws Exception {
    Object proceededInvocationContext = null;
    FaultToleranceService faultToleranceService = Globals.getDefaultBaseServiceLocator().getService(FaultToleranceService.class);
    InvocationManager invocationManager = Globals.getDefaultBaseServiceLocator().getService(InvocationManager.class);
    Config config = null;
    try {
        config = ConfigProvider.getConfig();
    } catch (IllegalArgumentException ex) {
        logger.log(Level.INFO, "No config could be found", ex);
    }
    try {
        String appName = faultToleranceService.getApplicationName(invocationManager, invocationContext);
        // Attempt to proceed the InvocationContext with Bulkhead semantics if Fault Tolerance is enabled
        if (faultToleranceService.isFaultToleranceEnabled(appName, config)) {
            logger.log(Level.FINER, "Proceeding invocation with bulkhead semantics");
            proceededInvocationContext = bulkhead(invocationContext);
        } else {
            // If fault tolerance isn't enabled, just proceed as normal
            logger.log(Level.FINE, "Fault Tolerance not enabled for {0}, proceeding normally without bulkhead.", faultToleranceService.getApplicationName(invocationManager, invocationContext));
            proceededInvocationContext = invocationContext.proceed();
        }
    } catch (Exception ex) {
        Retry retry = FaultToleranceCdiUtils.getAnnotation(beanManager, Retry.class, invocationContext);
        if (retry != null) {
            logger.log(Level.FINE, "Retry annotation found on method, propagating error upwards.");
            throw ex;
        } else {
            // If an exception was thrown, check if the method is annotated with @Fallback
            Fallback fallback = FaultToleranceCdiUtils.getAnnotation(beanManager, Fallback.class, invocationContext);
            // If the method was annotated with Fallback, attempt it, otherwise just propagate the exception upwards
            if (fallback != null) {
                logger.log(Level.FINE, "Fallback annotation found on method, and no Retry annotation - " + "falling back from Bulkhead");
                FallbackPolicy fallbackPolicy = new FallbackPolicy(fallback, config, invocationContext);
                proceededInvocationContext = fallbackPolicy.fallback(invocationContext);
            } else {
                logger.log(Level.FINE, "Fallback annotation not found on method, propagating error upwards.", ex);
                throw ex;
            }
        }
    }
    return proceededInvocationContext;
}
Also used : FallbackPolicy(fish.payara.microprofile.faulttolerance.interceptors.fallback.FallbackPolicy) Config(org.eclipse.microprofile.config.Config) InvocationManager(org.glassfish.api.invocation.InvocationManager) Fallback(org.eclipse.microprofile.faulttolerance.Fallback) Retry(org.eclipse.microprofile.faulttolerance.Retry) FaultToleranceService(fish.payara.microprofile.faulttolerance.FaultToleranceService) BulkheadException(org.eclipse.microprofile.faulttolerance.exceptions.BulkheadException) 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