Search in sources :

Example 16 with ComponentEventCallback

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

the class PageActivationContextCollectorImplTest method tryWithContext.

private void tryWithContext(String pageName, final Object context, Object... expected) {
    ComponentModelSource modelSource = mockComponentModelSource();
    RequestPageCache pageCache = mockRequestPageCache();
    ComponentPageElement element = mockComponentPageElement();
    ComponentModel model = mockComponentModel();
    Page page = mockPage();
    expect(modelSource.getPageModel(pageName)).andReturn(model);
    expect(model.handlesEvent(EventConstants.PASSIVATE)).andReturn(true);
    train_get(pageCache, pageName, page);
    train_getRootElement(page, element);
    IAnswer answer = new IAnswer() {

        public Object answer() throws Throwable {
            Object[] args = EasyMock.getCurrentArguments();
            ComponentEventCallback callback = (ComponentEventCallback) args[2];
            return callback.handleResult(context);
        }
    };
    expect(element.triggerEvent(EasyMock.eq(EventConstants.PASSIVATE), (Object[]) EasyMock.isNull(), EasyMock.isA(ComponentEventCallback.class))).andAnswer(answer);
    replay();
    PageActivationContextCollector collector = new PageActivationContextCollectorImpl(coercer, pageCache, modelSource);
    Object[] actual = collector.collectPageActivationContext(pageName);
    assertArraysEqual(actual, expected);
}
Also used : IAnswer(org.easymock.IAnswer) ComponentPageElement(org.apache.tapestry5.internal.structure.ComponentPageElement) ComponentModel(org.apache.tapestry5.model.ComponentModel) Page(org.apache.tapestry5.internal.structure.Page) ComponentEventCallback(org.apache.tapestry5.ComponentEventCallback)

Example 17 with ComponentEventCallback

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

the class ComponentPageElementImpl method processEventTriggering.

@SuppressWarnings("all")
private boolean processEventTriggering(String eventType, EventContext context, ComponentEventCallback callback) {
    boolean result = false;
    ComponentPageElement component = this;
    String componentId = "";
    // Provide a default handler for when the provided handler is null.
    final ComponentEventCallback providedHandler = callback == null ? new NotificationEventCallback(eventType, completeId) : callback;
    ComponentEventCallback wrapped = new ComponentEventCallback() {

        public boolean handleResult(Object result) {
            if (result instanceof Boolean)
                return (Boolean) result;
            return providedHandler.handleResult(result);
        }
    };
    RuntimeException rootException = null;
    // Because I don't like to reassign parameters.
    String currentEventType = eventType;
    EventContext currentContext = context;
    // Track the location of the original component for the event, even as we work our way up
    // the hierarchy. This may not be ideal if we trigger an "exception" event ... or maybe
    // it's right (it's the location of the originally thrown exception).
    Location location = component.getComponentResources().getLocation();
    while (component != null) {
        try {
            Logger logger = component.getEventLogger();
            ComponentEvent event = new ComponentEventImpl(currentEventType, componentId, currentContext, wrapped, elementResources, exactParameterCountMatch, coreResources.getComponentModel(), logger);
            logger.debug(TapestryMarkers.EVENT_DISPATCH, "Dispatch event: {}", event);
            result |= component.dispatchEvent(event);
            if (event.isAborted())
                return result;
        }// not the JVM).
         catch (Exception ex) {
            if (rootException != null)
                throw rootException;
            // We know component is not null and therefore has a component resources that
            // should have a location.
            // Wrap it up to help ensure that a location is available to the event handler
            // method or,
            // more likely, to the exception report page.
            rootException = new ComponentEventException(ex.getMessage(), eventType, context, location, ex);
            // Switch over to triggering an "exception" event, starting in the component that
            // threw the exception.
            currentEventType = "exception";
            currentContext = createParameterContext(rootException);
            continue;
        }
        // On each bubble up, make the event appear to come from the previous component
        // in which the event was triggered.
        componentId = component.getId();
        component = component.getContainerElement();
    }
    if (rootException != null)
        throw rootException;
    return result;
}
Also used : ComponentEventImpl(org.apache.tapestry5.internal.services.ComponentEventImpl) Logger(org.slf4j.Logger) UnknownValueException(org.apache.tapestry5.commons.util.UnknownValueException) TapestryException(org.apache.tapestry5.commons.internal.util.TapestryException) AbstractEventContext(org.apache.tapestry5.internal.AbstractEventContext) NotificationEventCallback(org.apache.tapestry5.internal.util.NotificationEventCallback) Location(org.apache.tapestry5.commons.Location)

Aggregations

ComponentEventCallback (org.apache.tapestry5.ComponentEventCallback)14 ComponentModel (org.apache.tapestry5.model.ComponentModel)14 Logger (org.slf4j.Logger)13 ComponentEvent (org.apache.tapestry5.runtime.ComponentEvent)12 Test (org.testng.annotations.Test)12 EventContext (org.apache.tapestry5.EventContext)8 ComponentPageElementResources (org.apache.tapestry5.internal.structure.ComponentPageElementResources)6 AbstractEventContext (org.apache.tapestry5.internal.AbstractEventContext)2 ComponentPageElement (org.apache.tapestry5.internal.structure.ComponentPageElement)2 Page (org.apache.tapestry5.internal.structure.Page)2 JSONObject (org.apache.tapestry5.json.JSONObject)2 List (java.util.List)1 Location (org.apache.tapestry5.commons.Location)1 TapestryException (org.apache.tapestry5.commons.internal.util.TapestryException)1 UnknownValueException (org.apache.tapestry5.commons.util.UnknownValueException)1 ComponentEventImpl (org.apache.tapestry5.internal.services.ComponentEventImpl)1 NotificationEventCallback (org.apache.tapestry5.internal.util.NotificationEventCallback)1 PartialMarkupRenderer (org.apache.tapestry5.services.PartialMarkupRenderer)1 PartialMarkupRendererFilter (org.apache.tapestry5.services.PartialMarkupRendererFilter)1 IAnswer (org.easymock.IAnswer)1