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();
}
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());
}
}
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());
}
}
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();
}
});
}
});
}
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.
}
Aggregations