Search in sources :

Example 86 with Context

use of org.apache.tapestry5.http.services.Context 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)

Example 87 with Context

use of org.apache.tapestry5.http.services.Context in project tapestry-5 by apache.

the class PageCallbackTest method callback_with_context.

@Test
public void callback_with_context() {
    EventContext context = new ArrayEventContext(typeCoercer, 1, 2);
    PageRenderLinkSource source = mockPageRenderLinkSource();
    Link link = mockLink();
    expect(source.createPageRenderLinkWithContext("bar", "1", "2")).andReturn(link);
    PageCallback pc = new PageCallback("bar", context);
    assertEquals(pc.toString(), "PageCallback[bar 1/2]");
    replay();
    assertSame(pc.toLink(source), link);
    verify();
}
Also used : ArrayEventContext(org.apache.tapestry5.internal.services.ArrayEventContext) EventContext(org.apache.tapestry5.EventContext) ArrayEventContext(org.apache.tapestry5.internal.services.ArrayEventContext) PageRenderLinkSource(org.apache.tapestry5.services.PageRenderLinkSource) PageCallback(org.apache.tapestry5.PageCallback) Link(org.apache.tapestry5.http.Link) Test(org.testng.annotations.Test)

Example 88 with Context

use of org.apache.tapestry5.http.services.Context in project tapestry-5 by apache.

the class CustomizingContextLoaderTest method specified_context_class_is_compatible.

@Test
public void specified_context_class_is_compatible() {
    ServletContext context = mockServletContext();
    train_getInitParameter(context, ContextLoader.CONTEXT_CLASS_PARAM, TapestryApplicationContext.class.getName());
    replay();
    CustomizingContextLoader ccl = new CustomizingContextLoader(null);
    assertSame(ccl.determineContextClass(context), TapestryApplicationContext.class);
    verify();
}
Also used : ServletContext(javax.servlet.ServletContext) TapestryApplicationContext(org.apache.tapestry5.spring.TapestryApplicationContext) Test(org.testng.annotations.Test)

Example 89 with Context

use of org.apache.tapestry5.http.services.Context in project tapestry-5 by apache.

the class SpringModuleDef method createContributionToMasterObjectProvider.

private ContributionDef createContributionToMasterObjectProvider() {
    ContributionDef def = new AbstractContributionDef() {

        @Override
        public String getServiceId() {
            return "MasterObjectProvider";
        }

        @Override
        public void contribute(ModuleBuilderSource moduleSource, ServiceResources resources, OrderedConfiguration configuration) {
            final OperationTracker tracker = resources.getTracker();
            final ApplicationContext context = resources.getService(SERVICE_ID, ApplicationContext.class);
            final ObjectProvider springBeanProvider = new ObjectProvider() {

                @Override
                public <T> T provide(Class<T> objectType, AnnotationProvider annotationProvider, ObjectLocator locator) {
                    Map beanMap = context.getBeansOfType(objectType);
                    switch(beanMap.size()) {
                        case 0:
                            return null;
                        case 1:
                            Object bean = beanMap.values().iterator().next();
                            return objectType.cast(bean);
                        default:
                            String message = String.format("Spring context contains %d beans assignable to type %s: %s.", beanMap.size(), PlasticUtils.toTypeName(objectType), InternalUtils.joinSorted(beanMap.keySet()));
                            throw new IllegalArgumentException(message);
                    }
                }
            };
            final ObjectProvider springBeanProviderInvoker = new ObjectProvider() {

                @Override
                public <T> T provide(final Class<T> objectType, final AnnotationProvider annotationProvider, final ObjectLocator locator) {
                    return tracker.invoke("Resolving dependency by searching Spring ApplicationContext", new Invokable<T>() {

                        @Override
                        public T invoke() {
                            return springBeanProvider.provide(objectType, annotationProvider, locator);
                        }
                    });
                }
            };
            ObjectProvider outerCheck = new ObjectProvider() {

                @Override
                public <T> T provide(Class<T> objectType, AnnotationProvider annotationProvider, ObjectLocator locator) {
                    if (!applicationContextCreated.get())
                        return null;
                    return springBeanProviderInvoker.provide(objectType, annotationProvider, locator);
                }
            };
            configuration.add("SpringBean", outerCheck, "after:AnnotationBasedContributions", "after:ServiceOverride");
        }
    };
    return def;
}
Also used : OperationTracker(org.apache.tapestry5.ioc.OperationTracker) ModuleBuilderSource(org.apache.tapestry5.ioc.ModuleBuilderSource) ContributionDef(org.apache.tapestry5.ioc.def.ContributionDef) AbstractContributionDef(org.apache.tapestry5.http.internal.AbstractContributionDef) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) ConfigurableWebApplicationContext(org.springframework.web.context.ConfigurableWebApplicationContext) AbstractContributionDef(org.apache.tapestry5.http.internal.AbstractContributionDef) ServiceResources(org.apache.tapestry5.ioc.ServiceResources) Map(java.util.Map)

Aggregations

Test (org.testng.annotations.Test)29 Context (org.apache.tapestry5.http.services.Context)18 Link (org.apache.tapestry5.http.Link)16 EventContext (org.apache.tapestry5.EventContext)12 ComponentEventCallback (org.apache.tapestry5.ComponentEventCallback)9 Request (org.apache.tapestry5.http.services.Request)9 Response (org.apache.tapestry5.http.services.Response)9 ComponentModel (org.apache.tapestry5.model.ComponentModel)9 Logger (org.slf4j.Logger)9 RequestFilter (org.apache.tapestry5.http.services.RequestFilter)8 RequestHandler (org.apache.tapestry5.http.services.RequestHandler)8 ComponentEvent (org.apache.tapestry5.runtime.ComponentEvent)8 ServletContext (javax.servlet.ServletContext)7 HttpServletResponse (javax.servlet.http.HttpServletResponse)7 PageRenderRequestParameters (org.apache.tapestry5.services.PageRenderRequestParameters)7 ComponentResources (org.apache.tapestry5.ComponentResources)6 TapestryException (org.apache.tapestry5.commons.internal.util.TapestryException)6 InternalComponentResources (org.apache.tapestry5.internal.InternalComponentResources)6 JSONObject (org.apache.tapestry5.json.JSONObject)6 LinkCreationListener2 (org.apache.tapestry5.services.LinkCreationListener2)5