Search in sources :

Example 1 with MethodAdvice

use of org.apache.tapestry5.plastic.MethodAdvice in project flowlogix by flowlogix.

the class AjaxAnnotationWorker method transform.

@Override
public void transform(PlasticClass plasticClass, TransformationSupport support, MutableComponentModel model) {
    boolean hasTrackerMixin = model.getMixinClassNames().contains(SessionTracker.class.getName());
    for (PlasticMethod method : plasticClass.getMethodsWithAnnotation(AJAX.class)) {
        final AJAX annotation = method.getAnnotation(AJAX.class);
        final boolean isVoid = method.isVoid();
        if (annotation.requireSession() && (!hasTrackerMixin)) {
            model.addMixinClassName(SessionTracker.class.getName());
            hasTrackerMixin = true;
        }
        method.addAdvice(new MethodAdvice() {

            @Override
            @SneakyThrows(IOException.class)
            public void advise(MethodInvocation invocation) {
                if (!request.isXHR() || annotation.requireSession() == false) {
                    invocation.proceed();
                } else {
                    // do not invoke on bad sessions
                    if (SessionTrackerUtil.isValidSession(rg.getActivePageName(), rg.getRequest().getSession(false))) {
                        invocation.proceed();
                    } else {
                        showSessionExpiredMessage = true;
                        SessionTrackerUtil.redirectToSelf(rg, linkSource);
                        if (!isVoid) {
                            invocation.setReturnValue(null);
                        }
                        return;
                    }
                }
                Object result = null;
                if (!isVoid) {
                    result = invocation.getReturnValue();
                }
                if (!request.isXHR()) {
                    if (result != null) {
                        result = defaultForReturnType(result.getClass());
                    }
                } else {
                    if (annotation.discardAfter()) {
                        cs.getActivePage().getComponentResources().discardPersistentFieldChanges();
                    }
                }
                if (!isVoid) {
                    invocation.setReturnValue(result);
                }
            }
        });
    }
}
Also used : SneakyThrows(lombok.SneakyThrows) MethodInvocation(org.apache.tapestry5.plastic.MethodInvocation) SessionTracker(com.flowlogix.web.mixins.SessionTracker) PlasticMethod(org.apache.tapestry5.plastic.PlasticMethod) MethodAdvice(org.apache.tapestry5.plastic.MethodAdvice) IOException(java.io.IOException) AJAX(com.flowlogix.web.services.annotations.AJAX)

Example 2 with MethodAdvice

use of org.apache.tapestry5.plastic.MethodAdvice in project tapestry-5 by apache.

the class LogWorker method transform.

public void transform(PlasticClass plasticClass, TransformationSupport support, MutableComponentModel model) {
    List<PlasticMethod> methods = plasticClass.getMethodsWithAnnotation(Log.class);
    if (methods.isEmpty()) {
        return;
    }
    final MethodAdvice loggingAdvice = new LoggingAdvice(model.getLogger(), exceptionTracker);
    for (PlasticMethod method : methods) {
        method.addAdvice(loggingAdvice);
    }
}
Also used : LoggingAdvice(org.apache.tapestry5.ioc.internal.services.LoggingAdvice) PlasticMethod(org.apache.tapestry5.plastic.PlasticMethod) MethodAdvice(org.apache.tapestry5.plastic.MethodAdvice)

Example 3 with MethodAdvice

use of org.apache.tapestry5.plastic.MethodAdvice in project tapestry-5 by apache.

the class CachedWorker method createFactory.

private MethodResultCacheFactory createFactory(PlasticClass plasticClass, final String watch, PlasticMethod method) {
    // will suffice.
    if (watch.equals("")) {
        return nonWatchFactory;
    }
    // Because of the watch, its necessary to create a factory for instances of this component and method.
    final FieldHandle bindingFieldHandle = plasticClass.introduceField(Binding.class, "cache$watchBinding$" + method.getDescription().methodName).getHandle();
    // Each component instance will get its own Binding instance. That handles both different locales,
    // and reuse of a component (with a cached method) within a page or across pages. However, the binding can't be initialized
    // until the page loads.
    plasticClass.introduceInterface(PageLifecycleListener.class);
    plasticClass.introduceMethod(TransformConstants.CONTAINING_PAGE_DID_LOAD_DESCRIPTION).addAdvice(new MethodAdvice() {

        public void advise(MethodInvocation invocation) {
            ComponentResources resources = invocation.getInstanceContext().get(ComponentResources.class);
            Binding binding = bindingSource.newBinding("@Cached watch", resources, BindingConstants.PROP, watch);
            bindingFieldHandle.set(invocation.getInstance(), binding);
            invocation.proceed();
        }
    });
    return new MethodResultCacheFactory() {

        public MethodResultCache create(Object instance) {
            Binding binding = (Binding) bindingFieldHandle.get(instance);
            return new WatchedBindingMethodResultCache(binding);
        }
    };
}
Also used : Binding(org.apache.tapestry5.Binding) ComponentResources(org.apache.tapestry5.ComponentResources)

Example 4 with MethodAdvice

use of org.apache.tapestry5.plastic.MethodAdvice in project tapestry-5 by apache.

the class TapestryModule method componentReplacer.

@Advise(serviceInterface = ComponentInstantiatorSource.class)
public static void componentReplacer(MethodAdviceReceiver methodAdviceReceiver, final ComponentOverride componentReplacer) throws NoSuchMethodException, SecurityException {
    if (componentReplacer.hasReplacements()) {
        MethodAdvice advice = new MethodAdvice() {

            @Override
            public void advise(MethodInvocation invocation) {
                String className = (String) invocation.getParameter(0);
                final Class<?> replacement = componentReplacer.getReplacement(className);
                if (replacement != null) {
                    invocation.setParameter(0, replacement.getName());
                }
                invocation.proceed();
            }
        };
        methodAdviceReceiver.adviseMethod(ComponentInstantiatorSource.class.getMethod("getInstantiator", String.class), advice);
    }
}
Also used : MethodInvocation(org.apache.tapestry5.plastic.MethodInvocation) MethodAdvice(org.apache.tapestry5.plastic.MethodAdvice) Advise(org.apache.tapestry5.ioc.annotations.Advise)

Example 5 with MethodAdvice

use of org.apache.tapestry5.plastic.MethodAdvice in project tapestry-5 by apache.

the class LazyAdvisorImpl method addAdvice.

private void addAdvice(Method method, MethodAdviceReceiver receiver) {
    final Class thunkType = method.getReturnType();
    final String description = String.format("<%s Thunk for %s>", thunkType.getName(), InternalUtils.asString(method));
    MethodAdvice advice = new MethodAdvice() {

        /**
         * When the method is invoked, we don't immediately proceed. Instead, we return a thunk instance
         * that defers its behavior to the lazily invoked invocation.
         */
        @Override
        public void advise(final MethodInvocation invocation) {
            ObjectCreator deferred = new ObjectCreator() {

                @Override
                public Object createObject() {
                    invocation.proceed();
                    return invocation.getReturnValue();
                }
            };
            ObjectCreator cachingObjectCreator = new CachingObjectCreator(deferred);
            Object thunk = thunkCreator.createThunk(thunkType, cachingObjectCreator, description);
            invocation.setReturnValue(thunk);
        }
    };
    receiver.adviseMethod(method, advice);
}
Also used : MethodInvocation(org.apache.tapestry5.plastic.MethodInvocation) MethodAdvice(org.apache.tapestry5.plastic.MethodAdvice) ObjectCreator(org.apache.tapestry5.commons.ObjectCreator)

Aggregations

MethodAdvice (org.apache.tapestry5.plastic.MethodAdvice)9 MethodInvocation (org.apache.tapestry5.plastic.MethodInvocation)6 Method (java.lang.reflect.Method)2 PlasticMethod (org.apache.tapestry5.plastic.PlasticMethod)2 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 Cached (org.apache.tapestry5.annotations.Cached)1 ObjectCreator (org.apache.tapestry5.commons.ObjectCreator)1 Advise (org.apache.tapestry5.ioc.annotations.Advise)1 Order (org.apache.tapestry5.ioc.annotations.Order)1 LoggingAdvice (org.apache.tapestry5.ioc.internal.services.LoggingAdvice)1 PerThreadValue (org.apache.tapestry5.ioc.services.PerThreadValue)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 CacheRemoveMethodAdvice (org.apache.tapestry5.jcache.internal.CacheRemoveMethodAdvice)1