Search in sources :

Example 1 with Advise

use of org.apache.tapestry5.ioc.annotations.Advise 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 Advise

use of org.apache.tapestry5.ioc.annotations.Advise 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 3 with Advise

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

the class ServiceAdvisorImpl method advise.

/**
 * Invokes the configured method, passing the builder. The method will always take, as a parameter, a
 * MethodAdvisor.
 */
@Override
public void advise(MethodAdviceReceiver methodAdviceReceiver) {
    Map<Class, Object> resources = CollectionFactory.newMap(this.resourcesDefaults);
    resources.put(MethodAdviceReceiver.class, methodAdviceReceiver);
    InjectionResources injectionResources = new MapInjectionResources(resources);
    // By design, advise methods return void, so we know that the return value is null.
    invoke(injectionResources);
}
Also used : InjectionResources(org.apache.tapestry5.ioc.internal.util.InjectionResources) MapInjectionResources(org.apache.tapestry5.ioc.internal.util.MapInjectionResources) MapInjectionResources(org.apache.tapestry5.ioc.internal.util.MapInjectionResources)

Example 4 with Advise

use of org.apache.tapestry5.ioc.annotations.Advise 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 Advise

use of org.apache.tapestry5.ioc.annotations.Advise 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)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