Search in sources :

Example 6 with Advise

use of org.apache.tapestry5.ioc.annotations.Advise in project tapestry-5 by apache.

the class JCacheModule method advise.

private static void advise(Class<? extends Annotation> annotationClass, ObjectLocator objectLocator, Class<? extends CacheMethodAdvice> adviceClass, MethodAdviceReceiver methodAdviceReceiver) {
    // TAP5-2466: create the advice on-demand to avoid recursion issues
    MethodAdvice advice = null;
    if (methodAdviceReceiver.getClassAnnotationProvider().getAnnotation(annotationClass) != null) {
        advice = build(objectLocator, adviceClass);
        methodAdviceReceiver.adviseAllMethods(advice);
    } else {
        for (Method method : methodAdviceReceiver.getInterface().getMethods()) {
            if (methodAdviceReceiver.getMethodAnnotation(method, annotationClass) != null) {
                if (advice == null) {
                    advice = build(objectLocator, adviceClass);
                }
                methodAdviceReceiver.adviseMethod(method, advice);
            }
        }
    }
}
Also used : CacheMethodAdvice(org.apache.tapestry5.jcache.internal.CacheMethodAdvice) CacheRemoveAllMethodAdvice(org.apache.tapestry5.jcache.internal.CacheRemoveAllMethodAdvice) CacheRemoveMethodAdvice(org.apache.tapestry5.jcache.internal.CacheRemoveMethodAdvice) MethodAdvice(org.apache.tapestry5.plastic.MethodAdvice) CacheResultMethodAdvice(org.apache.tapestry5.jcache.internal.CacheResultMethodAdvice) CachePutMethodAdvice(org.apache.tapestry5.jcache.internal.CachePutMethodAdvice) Method(java.lang.reflect.Method)

Example 7 with Advise

use of org.apache.tapestry5.ioc.annotations.Advise in project tapestry-5 by apache.

the class DecorateByMarkerModule method doDecorate.

private static <T> T doDecorate(final String decoratorId, ServiceResources resources, T delegate, AspectDecorator aspectDecorator) {
    Class<T> serviceInterface = resources.getServiceInterface();
    AspectInterceptorBuilder<T> builder = aspectDecorator.createBuilder(serviceInterface, delegate, String.format("<Interceptor for %s(%s)>", resources.getServiceId(), serviceInterface.getName()));
    builder.adviseAllMethods(new MethodAdvice() {

        @Override
        public void advise(MethodInvocation invocation) {
            invocation.proceed();
            Object result = invocation.getReturnValue();
            invocation.setReturnValue(String.format("Decorated by %s[%s]", decoratorId, result));
        }
    });
    return builder.build();
}
Also used : MethodInvocation(org.apache.tapestry5.plastic.MethodInvocation) MethodAdvice(org.apache.tapestry5.plastic.MethodAdvice)

Example 8 with Advise

use of org.apache.tapestry5.ioc.annotations.Advise in project tapestry-5 by apache.

the class DecorateByMarkerModule2 method doDecorate.

private static <T> T doDecorate(final String decoratorId, ServiceResources resources, T delegate, AspectDecorator aspectDecorator) {
    Class<T> serviceInterface = resources.getServiceInterface();
    AspectInterceptorBuilder<T> builder = aspectDecorator.createBuilder(serviceInterface, delegate, String.format("<Interceptor for %s(%s)>", resources.getServiceId(), serviceInterface.getName()));
    builder.adviseAllMethods(new MethodAdvice() {

        @Override
        public void advise(MethodInvocation invocation) {
            invocation.proceed();
            Object result = invocation.getReturnValue();
            invocation.setReturnValue(String.format("Decorated by %s[%s]", decoratorId, result));
        }
    });
    return builder.build();
}
Also used : MethodInvocation(org.apache.tapestry5.plastic.MethodInvocation) MethodAdvice(org.apache.tapestry5.plastic.MethodAdvice)

Example 9 with Advise

use of org.apache.tapestry5.ioc.annotations.Advise in project tapestry-5 by apache.

the class AdviceDemoModule method adviseGreeter.

@Order("after:Logging")
public static void adviseGreeter(MethodAdviceReceiver receiver) {
    MethodAdvice advice = new MethodAdvice() {

        @Override
        public void advise(MethodInvocation invocation) {
            invocation.proceed();
            String result = (String) invocation.getReturnValue();
            if (result != null)
                invocation.setReturnValue(result.toUpperCase());
        }
    };
    for (Method m : receiver.getInterface().getMethods()) {
        if (m.getReturnType().equals(String.class)) {
            receiver.adviseMethod(m, advice);
        }
    }
}
Also used : MethodInvocation(org.apache.tapestry5.plastic.MethodInvocation) MethodAdvice(org.apache.tapestry5.plastic.MethodAdvice) Method(java.lang.reflect.Method) Order(org.apache.tapestry5.ioc.annotations.Order)

Example 10 with Advise

use of org.apache.tapestry5.ioc.annotations.Advise in project tapestry-5 by apache.

the class DefaultModuleDefImpl method addAdvisorDef.

private void addAdvisorDef(Method method) {
    Advise annotation = method.getAnnotation(Advise.class);
    Class serviceInterface = annotation == null ? null : annotation.serviceInterface();
    // TODO: methods just named "decorate"
    String advisorId = annotation == null ? stripMethodPrefix(method, ADVISE_METHOD_NAME_PREFIX) : extractId(serviceInterface, annotation.id());
    // TODO: Check for duplicates
    Class returnType = method.getReturnType();
    if (!returnType.equals(void.class))
        throw new RuntimeException(String.format("Advise method %s does not return void.", toString(method)));
    boolean found = false;
    for (Class pt : method.getParameterTypes()) {
        if (pt.equals(MethodAdviceReceiver.class)) {
            found = true;
            break;
        }
    }
    if (!found)
        throw new RuntimeException(String.format("Advise method %s must take a parameter of type %s.", toString(method), MethodAdviceReceiver.class.getName()));
    Set<Class> markers = extractMarkers(method, Advise.class);
    AdvisorDef def = new AdvisorDefImpl(method, extractPatterns(advisorId, method), extractConstraints(method), proxyFactory, advisorId, serviceInterface, markers);
    advisorDefs.put(advisorId, def);
}
Also used : MethodAdviceReceiver(org.apache.tapestry5.ioc.MethodAdviceReceiver) Advise(org.apache.tapestry5.ioc.annotations.Advise) AdvisorDef(org.apache.tapestry5.ioc.AdvisorDef)

Aggregations

MethodAdvice (org.apache.tapestry5.plastic.MethodAdvice)7 MethodInvocation (org.apache.tapestry5.plastic.MethodInvocation)6 Method (java.lang.reflect.Method)3 Advise (org.apache.tapestry5.ioc.annotations.Advise)3 SessionTracker (com.flowlogix.web.mixins.SessionTracker)1 AJAX (com.flowlogix.web.services.annotations.AJAX)1 IOException (java.io.IOException)1 SneakyThrows (lombok.SneakyThrows)1 Binding (org.apache.tapestry5.Binding)1 ComponentResources (org.apache.tapestry5.ComponentResources)1 ObjectCreator (org.apache.tapestry5.commons.ObjectCreator)1 AdvisorDef (org.apache.tapestry5.ioc.AdvisorDef)1 MethodAdviceReceiver (org.apache.tapestry5.ioc.MethodAdviceReceiver)1 IntermediateType (org.apache.tapestry5.ioc.annotations.IntermediateType)1 Order (org.apache.tapestry5.ioc.annotations.Order)1 InjectionResources (org.apache.tapestry5.ioc.internal.util.InjectionResources)1 MapInjectionResources (org.apache.tapestry5.ioc.internal.util.MapInjectionResources)1 CacheMethodAdvice (org.apache.tapestry5.jcache.internal.CacheMethodAdvice)1 CachePutMethodAdvice (org.apache.tapestry5.jcache.internal.CachePutMethodAdvice)1 CacheRemoveAllMethodAdvice (org.apache.tapestry5.jcache.internal.CacheRemoveAllMethodAdvice)1