Search in sources :

Example 1 with FieldHandle

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

the class PageActivationContextWorker method transformFields.

private void transformFields(TransformationSupport support, List<PlasticField> fields) {
    List<PlasticField> sortedFields = CollectionFactory.newList(fields);
    Collections.sort(sortedFields, INDEX_COMPARATOR);
    validateSortedFields(sortedFields);
    PlasticField firstField = sortedFields.get(0);
    PageActivationContext firstAnnotation = firstField.getAnnotation(PageActivationContext.class);
    // these arrays reduce memory usage and allow the PlasticField instances to be garbage collected
    FieldHandle[] handles = new FieldHandle[sortedFields.size()];
    String[] typeNames = new String[sortedFields.size()];
    int i = 0;
    for (PlasticField field : sortedFields) {
        handles[i] = field.getHandle();
        typeNames[i] = field.getTypeName();
        ++i;
    }
    if (firstAnnotation.activate()) {
        support.addEventHandler(EventConstants.ACTIVATE, 1, "PageActivationContextWorker activate event handler", createActivationHandler(handles, typeNames));
    }
    if (firstAnnotation.passivate()) {
        support.addEventHandler(EventConstants.PASSIVATE, 0, "PageActivationContextWorker passivate event handler", createPassivateHandler(handles));
    }
// We don't claim the field, and other workers may even replace it with a FieldConduit.
}
Also used : PageActivationContext(org.apache.tapestry5.annotations.PageActivationContext) PlasticField(org.apache.tapestry5.plastic.PlasticField) FieldHandle(org.apache.tapestry5.plastic.FieldHandle)

Example 2 with FieldHandle

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

the class PageActivationContextWorker method createActivationHandler.

private static ComponentEventHandler createActivationHandler(final FieldHandle[] handles, final String[] fieldTypes) {
    return new ComponentEventHandler() {

        public void handleEvent(Component instance, ComponentEvent event) {
            int count = Math.min(handles.length, event.getEventContext().getCount());
            for (int i = 0; i < count; ++i) {
                String fieldType = fieldTypes[i];
                FieldHandle handle = handles[i];
                Object value = event.coerceContext(i, fieldType);
                handle.set(instance, value);
            }
        }
    };
}
Also used : ComponentEventHandler(org.apache.tapestry5.services.ComponentEventHandler) ComponentEvent(org.apache.tapestry5.runtime.ComponentEvent) Component(org.apache.tapestry5.runtime.Component) FieldHandle(org.apache.tapestry5.plastic.FieldHandle)

Example 3 with FieldHandle

use of org.apache.tapestry5.plastic.FieldHandle 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 FieldHandle

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

the class ActivationRequestParameterWorker method decorateLinks.

@SuppressWarnings("all")
private static void decorateLinks(TransformationSupport support, String fieldName, final FieldHandle handle, final String parameterName, final ValueEncoder encoder, final URLEncoder urlEncoder) {
    ComponentEventHandler handler = new ComponentEventHandler() {

        public void handleEvent(Component instance, ComponentEvent event) {
            Object value = handle.get(instance);
            if (value == null) {
                return;
            }
            Link link = event.getEventContext().get(Link.class, 0);
            String clientValue = encoder.toClient(value);
            // TAP5-1768: escape special characters
            clientValue = urlEncoder.encode(clientValue);
            link.addParameter(parameterName, clientValue);
        }
    };
    support.addEventHandler(EventConstants.DECORATE_COMPONENT_EVENT_LINK, 0, String.format("ActivationRequestParameterWorker decorate component event link event handler for field %s as query parameter '%s'", fieldName, parameterName), handler);
    support.addEventHandler(EventConstants.DECORATE_PAGE_RENDER_LINK, 0, String.format("ActivationRequestParameterWorker decorate page render link event handler for field %s as query parameter '%s'", fieldName, parameterName), handler);
}
Also used : ComponentEventHandler(org.apache.tapestry5.services.ComponentEventHandler) ComponentEvent(org.apache.tapestry5.runtime.ComponentEvent) Component(org.apache.tapestry5.runtime.Component) Link(org.apache.tapestry5.http.Link)

Example 5 with FieldHandle

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

the class ActivationRequestParameterWorker method setValueFromInitializeEventHandler.

@SuppressWarnings("all")
private void setValueFromInitializeEventHandler(final TransformationSupport support, final String fieldName, final boolean required, final FieldHandle handle, final String parameterName, final ValueEncoder encoder, final URLEncoder urlEncoder) {
    ComponentEventHandler handler = new ComponentEventHandler() {

        public void handleEvent(Component instance, ComponentEvent event) {
            String clientValue = request.getParameter(parameterName);
            if (clientValue == null) {
                if (required) {
                    throw new TapestryException(String.format("Activation request parameter field %s is marked as required, but query parameter '%s' is null.", fieldName, parameterName), null);
                }
                return;
            }
            // TAP5-1768: unescape encoded value
            clientValue = urlEncoder.decode(clientValue);
            Object value = encoder.toValue(clientValue);
            handle.set(instance, value);
        }
    };
    support.addEventHandler(EventConstants.ACTIVATE, 0, String.format("Restoring field %s from query parameter '%s'", fieldName, parameterName), handler);
}
Also used : ComponentEventHandler(org.apache.tapestry5.services.ComponentEventHandler) ComponentEvent(org.apache.tapestry5.runtime.ComponentEvent) Component(org.apache.tapestry5.runtime.Component) TapestryException(org.apache.tapestry5.commons.internal.util.TapestryException)

Aggregations

FieldHandle (org.apache.tapestry5.plastic.FieldHandle)4 Component (org.apache.tapestry5.runtime.Component)4 ComponentEvent (org.apache.tapestry5.runtime.ComponentEvent)4 ComponentEventHandler (org.apache.tapestry5.services.ComponentEventHandler)4 Binding (org.apache.tapestry5.Binding)1 ComponentResources (org.apache.tapestry5.ComponentResources)1 ValueEncoder (org.apache.tapestry5.ValueEncoder)1 ActivationRequestParameter (org.apache.tapestry5.annotations.ActivationRequestParameter)1 PageActivationContext (org.apache.tapestry5.annotations.PageActivationContext)1 TapestryException (org.apache.tapestry5.commons.internal.util.TapestryException)1 Link (org.apache.tapestry5.http.Link)1 PlasticClass (org.apache.tapestry5.plastic.PlasticClass)1 PlasticField (org.apache.tapestry5.plastic.PlasticField)1