Search in sources :

Example 6 with Event

use of org.apache.tapestry5.runtime.Event in project tapestry-5 by apache.

the class ComponentEventImplTest method coerce_context.

@Test
public void coerce_context() {
    ComponentEventCallback handler = mockComponentEventHandler();
    ComponentPageElementResources resources = mockComponentPageElementResources();
    EventContext context = mockEventContext();
    Integer value = new Integer(27);
    Logger logger = mockLogger();
    ComponentModel model = mockComponentModel();
    train_isDebugEnabled(logger, false);
    train_toClass(resources, "java.lang.Integer", Integer.class);
    train_getCount(context, 2);
    train_get(context, Integer.class, 0, value);
    replay();
    ComponentEvent event = new ComponentEventImpl("eventType", "someId", context, handler, resources, false, model, logger);
    assertSame(event.coerceContext(0, "java.lang.Integer"), value);
    verify();
}
Also used : EventContext(org.apache.tapestry5.EventContext) ComponentModel(org.apache.tapestry5.model.ComponentModel) ComponentPageElementResources(org.apache.tapestry5.internal.structure.ComponentPageElementResources) ComponentEvent(org.apache.tapestry5.runtime.ComponentEvent) Logger(org.slf4j.Logger) ComponentEventCallback(org.apache.tapestry5.ComponentEventCallback) Test(org.testng.annotations.Test)

Example 7 with Event

use of org.apache.tapestry5.runtime.Event in project tapestry-5 by apache.

the class ComponentEventRequestHandlerImpl method handle.

public void handle(ComponentEventRequestParameters parameters) throws IOException {
    Page activePage = cache.get(parameters.getActivePageName());
    if (pageActivator.activatePage(activePage.getRootElement().getComponentResources(), parameters.getPageActivationContext(), resultProcessor)) {
        return;
    }
    Page containerPage = cache.get(parameters.getContainingPageName());
    TrackableComponentEventCallback callback = new ComponentResultProcessorWrapper(resultProcessor);
    environment.push(ComponentEventResultProcessor.class, resultProcessor);
    environment.push(TrackableComponentEventCallback.class, callback);
    ComponentPageElement element = containerPage.getComponentElementByNestedId(parameters.getNestedComponentId());
    boolean handled = element.triggerContextEvent(parameters.getEventType(), parameters.getEventContext(), callback);
    if (!handled) {
        throw new TapestryException(String.format("Request event '%s' (on component %s) was not handled; you must provide a matching event handler method in the component or in one of its containers.", parameters.getEventType(), element.getCompleteId()), element, null);
    }
    environment.pop(TrackableComponentEventCallback.class);
    environment.pop(ComponentEventResultProcessor.class);
    if (callback.isAborted()) {
        callback.rethrow();
        return;
    }
    if (!response.isCommitted()) {
        resultProcessor.processResultValue(activePage.getName());
    }
}
Also used : ComponentPageElement(org.apache.tapestry5.internal.structure.ComponentPageElement) TrackableComponentEventCallback(org.apache.tapestry5.TrackableComponentEventCallback) Page(org.apache.tapestry5.internal.structure.Page) TapestryException(org.apache.tapestry5.commons.internal.util.TapestryException)

Example 8 with Event

use of org.apache.tapestry5.runtime.Event in project tapestry-5 by apache.

the class OnEventWorker method addPublishEventInfo.

private void addPublishEventInfo(Flow<EventHandlerMethod> eventHandlerMethods, MutableComponentModel model) {
    JSONArray publishEvents = new JSONArray();
    for (EventHandlerMethod eventHandlerMethod : eventHandlerMethods) {
        if (eventHandlerMethod.publishEvent != null) {
            publishEvents.add(eventHandlerMethod.eventType.toLowerCase());
        }
    }
    // event information to it.
    if (publishEvents.size() > 0) {
        model.addMixinClassName(PublishServerSideEvents.class.getName(), "after:*");
        model.setMeta(InternalConstants.PUBLISH_COMPONENT_EVENTS_META, publishEvents.toString());
    }
}
Also used : JSONArray(org.apache.tapestry5.json.JSONArray) PublishServerSideEvents(org.apache.tapestry5.corelib.mixins.PublishServerSideEvents)

Example 9 with Event

use of org.apache.tapestry5.runtime.Event in project tapestry-5 by apache.

the class OnEventWorker method implementDispatchMethod.

private void implementDispatchMethod(final PlasticClass plasticClass, final boolean isRoot, final MutableComponentModel model, final Flow<EventHandlerMethod> eventHandlerMethods) {
    plasticClass.introduceMethod(TransformConstants.DISPATCH_COMPONENT_EVENT_DESCRIPTION).changeImplementation(new InstructionBuilderCallback() {

        public void doBuild(InstructionBuilder builder) {
            builder.startVariable("boolean", new LocalVariableCallback() {

                public void doBuild(LocalVariable resultVariable, InstructionBuilder builder) {
                    if (!isRoot) {
                        // As a subclass, there will be a base class implementation (possibly empty).
                        builder.loadThis().loadArguments().invokeSpecial(plasticClass.getSuperClassName(), TransformConstants.DISPATCH_COMPONENT_EVENT_DESCRIPTION);
                        // First store the result of the super() call into the variable.
                        builder.storeVariable(resultVariable);
                        builder.loadArgument(0).invoke(Event.class, boolean.class, "isAborted");
                        builder.when(Condition.NON_ZERO, RETURN_TRUE);
                    } else {
                        // No event handler method has yet been invoked.
                        builder.loadConstant(false).storeVariable(resultVariable);
                    }
                    boolean hasRestEndpointEventHandlerMethod = false;
                    JSONArray restEndpointEventHandlerMethods = null;
                    for (EventHandlerMethod method : eventHandlerMethods) {
                        method.buildMatchAndInvocation(builder, resultVariable);
                        model.addEventHandler(method.eventType);
                        if (method.handleActivationEventContext) {
                            model.doHandleActivationEventContext();
                        }
                        // We're collecting this info for all components, even considering REST
                        // events are only triggered in pages, because we can have REST event
                        // handler methods in base classes too, and we need this info
                        // for generating complete, correct OpenAPI documentation.
                        final OnEvent onEvent = method.method.getAnnotation(OnEvent.class);
                        final String methodName = method.method.getDescription().methodName;
                        if (isRestEndpointEventHandlerMethod(onEvent, methodName)) {
                            hasRestEndpointEventHandlerMethod = true;
                            if (restEndpointEventHandlerMethods == null) {
                                restEndpointEventHandlerMethods = new JSONArray();
                            }
                            JSONObject methodMeta = new JSONObject();
                            methodMeta.put("name", methodName);
                            JSONArray parameters = new JSONArray();
                            for (MethodParameter parameter : method.method.getParameters()) {
                                parameters.add(parameter.getType());
                            }
                            methodMeta.put("parameters", parameters);
                            restEndpointEventHandlerMethods.add(methodMeta);
                        }
                    }
                    // memory by not setting it to all component models.
                    if (model.isPage()) {
                        model.setMeta(InternalConstants.REST_ENDPOINT_EVENT_HANDLER_METHOD_PRESENT, hasRestEndpointEventHandlerMethod ? InternalConstants.TRUE : InternalConstants.FALSE);
                    }
                    // methods in components, something that would be ignored anyway.
                    if (restEndpointEventHandlerMethods != null) {
                        model.setMeta(InternalConstants.REST_ENDPOINT_EVENT_HANDLER_METHODS, restEndpointEventHandlerMethods.toCompactString());
                    }
                    builder.loadVariable(resultVariable).returnResult();
                }
            });
        }
    });
}
Also used : LocalVariableCallback(org.apache.tapestry5.plastic.LocalVariableCallback) InstructionBuilder(org.apache.tapestry5.plastic.InstructionBuilder) JSONObject(org.apache.tapestry5.json.JSONObject) LocalVariable(org.apache.tapestry5.plastic.LocalVariable) JSONArray(org.apache.tapestry5.json.JSONArray) MethodParameter(org.apache.tapestry5.plastic.MethodParameter) InstructionBuilderCallback(org.apache.tapestry5.plastic.InstructionBuilderCallback) OnEvent(org.apache.tapestry5.annotations.OnEvent)

Example 10 with Event

use of org.apache.tapestry5.runtime.Event 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)

Aggregations

ComponentEvent (org.apache.tapestry5.runtime.ComponentEvent)19 ComponentModel (org.apache.tapestry5.model.ComponentModel)14 Logger (org.slf4j.Logger)14 Test (org.testng.annotations.Test)14 ComponentEventCallback (org.apache.tapestry5.ComponentEventCallback)13 JSONObject (org.apache.tapestry5.json.JSONObject)11 EventContext (org.apache.tapestry5.EventContext)9 TapestryException (org.apache.tapestry5.commons.internal.util.TapestryException)6 ComponentPageElementResources (org.apache.tapestry5.internal.structure.ComponentPageElementResources)6 Component (org.apache.tapestry5.runtime.Component)6 ComponentResources (org.apache.tapestry5.ComponentResources)4 UnknownValueException (org.apache.tapestry5.commons.util.UnknownValueException)4 JSONArray (org.apache.tapestry5.json.JSONArray)4 ComponentEventHandler (org.apache.tapestry5.services.ComponentEventHandler)4 TrackableComponentEventCallback (org.apache.tapestry5.TrackableComponentEventCallback)3 ComponentPageElement (org.apache.tapestry5.internal.structure.ComponentPageElement)3 Page (org.apache.tapestry5.internal.structure.Page)3 SimpleDateFormat (java.text.SimpleDateFormat)2 Date (java.util.Date)2 Link (org.apache.tapestry5.Link)2