Search in sources :

Example 26 with MethodInvocation

use of org.aopalliance.intercept.MethodInvocation in project shiro by apache.

the class SecureRemoteInvocationFactoryTest method testSessionManagerProxyNonStartRemoteInvocation.

@Test
public void testSessionManagerProxyNonStartRemoteInvocation() throws Exception {
    SecureRemoteInvocationFactory factory = new SecureRemoteInvocationFactory();
    MethodInvocation mi = createMock(MethodInvocation.class);
    Method method = getMethod("getSession", SessionManager.class);
    expect(mi.getMethod()).andReturn(method).anyTimes();
    String dummySessionId = UUID.randomUUID().toString();
    SessionKey sessionKey = new DefaultSessionKey(dummySessionId);
    Object[] args = { sessionKey };
    expect(mi.getArguments()).andReturn(args).anyTimes();
    replay(mi);
    RemoteInvocation ri = factory.createRemoteInvocation(mi);
    verify(mi);
    assertEquals(dummySessionId, ri.getAttribute(SecureRemoteInvocationFactory.SESSION_ID_KEY));
}
Also used : RemoteInvocation(org.springframework.remoting.support.RemoteInvocation) DefaultSessionKey(org.apache.shiro.session.mgt.DefaultSessionKey) SessionKey(org.apache.shiro.session.mgt.SessionKey) MethodInvocation(org.aopalliance.intercept.MethodInvocation) Method(java.lang.reflect.Method) DefaultSessionKey(org.apache.shiro.session.mgt.DefaultSessionKey) Test(org.junit.Test)

Example 27 with MethodInvocation

use of org.aopalliance.intercept.MethodInvocation in project camunda-bpm-platform by camunda.

the class ProcessScope method createDirtyCheckingProxy.

private Object createDirtyCheckingProxy(final String name, final Object scopedObject) throws Throwable {
    ProxyFactory proxyFactoryBean = new ProxyFactory(scopedObject);
    proxyFactoryBean.setProxyTargetClass(this.proxyTargetClass);
    proxyFactoryBean.addAdvice(new MethodInterceptor() {

        public Object invoke(MethodInvocation methodInvocation) throws Throwable {
            Object result = methodInvocation.proceed();
            persistVariable(name, scopedObject);
            return result;
        }
    });
    return proxyFactoryBean.getProxy(this.classLoader);
}
Also used : MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) ProxyFactory(org.springframework.aop.framework.ProxyFactory) MethodInvocation(org.aopalliance.intercept.MethodInvocation) ScopedObject(org.springframework.aop.scope.ScopedObject)

Example 28 with MethodInvocation

use of org.aopalliance.intercept.MethodInvocation in project spring-cloud-stream by spring-cloud.

the class DefaultPollableMessageSource method setSource.

public void setSource(MessageSource<?> source) {
    ProxyFactory pf = new ProxyFactory(source);
    class ReceiveAdvice implements MethodInterceptor {

        private final List<ChannelInterceptor> interceptors = new ArrayList<>();

        @Override
        public Object invoke(MethodInvocation invocation) throws Throwable {
            Object result = invocation.proceed();
            if (result instanceof Message) {
                Message<?> received = (Message<?>) result;
                for (ChannelInterceptor interceptor : this.interceptors) {
                    received = interceptor.preSend(received, null);
                    if (received == null) {
                        return null;
                    }
                }
                return received;
            }
            return result;
        }
    }
    final ReceiveAdvice advice = new ReceiveAdvice();
    advice.interceptors.addAll(this.interceptors);
    NameMatchMethodPointcutAdvisor sourceAdvisor = new NameMatchMethodPointcutAdvisor(advice);
    sourceAdvisor.addMethodName("receive");
    pf.addAdvisor(sourceAdvisor);
    this.source = (MessageSource<?>) pf.getProxy();
}
Also used : MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) Message(org.springframework.messaging.Message) ProxyFactory(org.springframework.aop.framework.ProxyFactory) ChannelInterceptor(org.springframework.messaging.support.ChannelInterceptor) NameMatchMethodPointcutAdvisor(org.springframework.aop.support.NameMatchMethodPointcutAdvisor) ArrayList(java.util.ArrayList) List(java.util.List) MethodInvocation(org.aopalliance.intercept.MethodInvocation)

Example 29 with MethodInvocation

use of org.aopalliance.intercept.MethodInvocation in project guice by google.

the class MethodInterceptionTest method testGetElements_interceptorBindings.

@Test
public void testGetElements_interceptorBindings() throws Exception {
    @SuppressWarnings("rawtypes") Matcher<Class> classMatcher = Matchers.subclassesOf(List.class);
    Matcher<Method> methodMatcher = Matchers.returns(Matchers.identicalTo(int.class));
    MethodInterceptor interceptor = new MethodInterceptor() {

        @Override
        public Object invoke(MethodInvocation methodInvocation) throws Throwable {
            return null;
        }
    };
    Injector injector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            bindInterceptor(classMatcher, methodMatcher, interceptor);
        }
    });
    final List<InterceptorBinding> interceptorBindings = new ArrayList<>();
    for (Element element : injector.getElements()) {
        element.acceptVisitor(new DefaultElementVisitor<Void>() {

            @Override
            public Void visit(InterceptorBinding interceptorBinding) {
                interceptorBindings.add(interceptorBinding);
                return null;
            }
        });
    }
    assertThat(interceptorBindings).hasSize(1);
    InterceptorBinding extractedBinding = interceptorBindings.get(0);
    assertSame(classMatcher, extractedBinding.getClassMatcher());
    assertSame(methodMatcher, extractedBinding.getMethodMatcher());
    assertSame(interceptor, extractedBinding.getInterceptors().get(0));
}
Also used : Element(com.google.inject.spi.Element) ArrayList(java.util.ArrayList) MethodInvocation(org.aopalliance.intercept.MethodInvocation) Method(java.lang.reflect.Method) InterceptorBinding(com.google.inject.spi.InterceptorBinding) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) Test(org.junit.Test)

Example 30 with MethodInvocation

use of org.aopalliance.intercept.MethodInvocation in project guice by google.

the class JpaPersistModule method bindFinder.

private <T> void bindFinder(Class<T> iface) {
    if (!isDynamicFinderValid(iface)) {
        return;
    }
    InvocationHandler finderInvoker = new InvocationHandler() {

        @Inject
        JpaFinderProxy finderProxy;

        @Override
        public Object invoke(final Object thisObject, final Method method, final Object[] args) throws Throwable {
            // Don't intercept non-finder methods like equals and hashcode.
            if (!method.isAnnotationPresent(Finder.class)) {
                // and hashcode as a proxy (!) for the proxy's equals and hashcode.
                return method.invoke(this, args);
            }
            return finderProxy.invoke(new MethodInvocation() {

                @Override
                public Method getMethod() {
                    return method;
                }

                @Override
                public Object[] getArguments() {
                    return null == args ? new Object[0] : args;
                }

                @Override
                public Object proceed() throws Throwable {
                    return method.invoke(thisObject, args);
                }

                @Override
                public Object getThis() {
                    throw new UnsupportedOperationException("Bottomless proxies don't expose a this.");
                }

                @Override
                public AccessibleObject getStaticPart() {
                    throw new UnsupportedOperationException();
                }
            });
        }
    };
    requestInjection(finderInvoker);
    // Proxy must produce instance of type given.
    @SuppressWarnings("unchecked") T proxy = (T) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class<?>[] { iface }, finderInvoker);
    bind(iface).toInstance(proxy);
}
Also used : Finder(com.google.inject.persist.finder.Finder) DynamicFinder(com.google.inject.persist.finder.DynamicFinder) MethodInvocation(org.aopalliance.intercept.MethodInvocation) Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler) AccessibleObject(java.lang.reflect.AccessibleObject) AccessibleObject(java.lang.reflect.AccessibleObject)

Aggregations

MethodInvocation (org.aopalliance.intercept.MethodInvocation)117 Test (org.junit.jupiter.api.Test)50 Test (org.junit.Test)35 MethodInterceptor (org.aopalliance.intercept.MethodInterceptor)25 SimpleMethodInvocation (org.springframework.security.util.SimpleMethodInvocation)22 Method (java.lang.reflect.Method)21 ArrayList (java.util.ArrayList)11 Log (org.apache.commons.logging.Log)11 Authentication (org.springframework.security.core.Authentication)10 EvaluationContext (org.springframework.expression.EvaluationContext)9 Expression (org.springframework.expression.Expression)9 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)9 List (java.util.List)7 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)7 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)7 MyThrowsHandler (org.springframework.aop.testfixture.advice.MyThrowsHandler)7 OAuth2Request (org.springframework.security.oauth2.provider.OAuth2Request)7 RemoteInvocation (org.springframework.remoting.support.RemoteInvocation)6 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)5 Promise (ratpack.exec.Promise)5