use of jakarta.faces.event.ComponentSystemEvent in project myfaces by apache.
the class AnnotationConfigurator method handleNamedEvent.
private void handleNamedEvent(FacesConfigImpl facesConfig, Set<Class<?>> classes) {
for (Class<?> clazz : classes) {
NamedEvent namedEvent = (NamedEvent) clazz.getAnnotation(NamedEvent.class);
if (namedEvent != null) {
// Can only apply @NamedEvent to ComponentSystemEvent subclasses.
if (!ComponentSystemEvent.class.isAssignableFrom(clazz)) {
// Just log this. We'll catch it later in the runtime.
if (log.isLoggable(Level.WARNING)) {
log.warning(clazz.getName() + " is annotated with @jakarta.faces.event.NamedEvent, but does " + "not extend jakarta.faces.event.ComponentSystemEvent");
}
}
// Have to register @NamedEvent annotations with the NamedEventManager class since
// we need to get access to this info later and can't from the dispenser (it's not a
// singleton).
org.apache.myfaces.config.impl.element.NamedEventImpl namedEventConfig = new org.apache.myfaces.config.impl.element.NamedEventImpl();
namedEventConfig.setEventClass(clazz.getName());
namedEventConfig.setShortName(namedEvent.shortName());
facesConfig.addNamedEvent(namedEventConfig);
}
}
}
use of jakarta.faces.event.ComponentSystemEvent in project myfaces by apache.
the class EventHandler method getEventClass.
/**
* Gets the event class defined by the tag (either in the "name" or "type" attribute).
*
* @param context the Facelet context
* @return a Class containing the event class defined by the tag.
*/
@SuppressWarnings("unchecked")
private Class<? extends ComponentSystemEvent> getEventClass(FaceletContext context) {
Class<?> eventClass = null;
String value = null;
if (type.isLiteral() && eventClassLiteral != null) {
// if type is literal it does not change, avoid Reflection and use cached value
return (Class<? extends ComponentSystemEvent>) eventClassLiteral;
}
if (type.isLiteral()) {
value = type.getValue();
} else {
value = (String) type.getValueExpression(context, String.class).getValue(context.getFacesContext().getELContext());
}
Collection<Class<? extends ComponentSystemEvent>> events;
// We can look up the event class by name in the NamedEventManager.
events = RuntimeConfig.getCurrentInstance(context.getFacesContext().getExternalContext()).getNamedEventManager().getNamedEvent(value);
if (events == null) {
try {
eventClass = ClassUtils.forName(value);
if (type.isLiteral()) {
eventClassLiteral = eventClass;
}
} catch (Throwable e) {
throw new TagAttributeException(type, "Couldn't create event class", e);
}
} else if (events.size() > 1) {
StringBuilder classNames = new StringBuilder("[");
Iterator<Class<? extends ComponentSystemEvent>> eventIterator = events.iterator();
while (eventIterator.hasNext()) {
classNames.append(eventIterator.next().getName());
if (eventIterator.hasNext()) {
classNames.append(", ");
} else {
classNames.append(']');
}
}
throw new FacesException("The event name '" + value + "' is mapped to more than one " + " event class: " + classNames.toString());
} else {
eventClass = events.iterator().next();
}
if (!ComponentSystemEvent.class.isAssignableFrom(eventClass)) {
throw new TagAttributeException(type, "Event class " + eventClass.getName() + " is not of type jakarta.faces.event.ComponentSystemEvent");
}
return (Class<? extends ComponentSystemEvent>) eventClass;
}
use of jakarta.faces.event.ComponentSystemEvent in project myfaces by apache.
the class PostAddToViewEventTestCase method testPostAddToViewOnViewRoot.
/**
* Test that UIViewRoot receive a PostAddToViewEvent after the view is
* populated.
*
* @throws Exception
*/
@Test
public void testPostAddToViewOnViewRoot() throws Exception {
startViewRequest("/postAddToViewEvent_1.xhtml");
PostAddToViewEventBean bean = EasyMock.createMock(PostAddToViewEventBean.class);
bean.invokePostAddToViewEvent(EasyMock.isA(ComponentSystemEvent.class));
EasyMock.expectLastCall().andAnswer(() -> {
ComponentSystemEvent e = (ComponentSystemEvent) EasyMock.getCurrentArguments()[0];
Assert.assertTrue(e.getComponent() instanceof UIViewRoot);
Assert.assertEquals(PhaseId.RENDER_RESPONSE, facesContext.getCurrentPhaseId());
return null;
}).once();
EasyMock.replay(bean);
// Put on request map
request.setAttribute("postAddToViewEventBean", bean);
processLifecycleExecuteAndRender();
EasyMock.verify(bean);
UICommand button = (UICommand) facesContext.getViewRoot().findComponent("mainForm:submit");
client.submit(button);
bean = EasyMock.createMock(PostAddToViewEventBean.class);
bean.invokePostAddToViewEvent(EasyMock.isA(ComponentSystemEvent.class));
// but when building initial state.
if (WebConfigParamUtils.getBooleanInitParameter(externalContext, StateManager.PARTIAL_STATE_SAVING_PARAM_NAME)) {
EasyMock.expectLastCall().andAnswer(() -> {
ComponentSystemEvent e = (ComponentSystemEvent) EasyMock.getCurrentArguments()[0];
Assert.assertTrue(e.getComponent() instanceof UIViewRoot);
Assert.assertEquals(PhaseId.RESTORE_VIEW, facesContext.getCurrentPhaseId());
Assert.assertTrue(facesContext.getAttributes().containsKey("jakarta.faces.IS_BUILDING_INITIAL_STATE"));
Assert.assertFalse(FaceletViewDeclarationLanguage.isRefreshingTransientBuild(facesContext));
return null;
}).once();
} else {
// Without PSS the listener should not be called, because it was already
// called on the first request
EasyMock.expectLastCall().andAnswer(() -> {
Assert.fail();
return null;
}).anyTimes();
}
EasyMock.replay(bean);
// Put on request map
request.setAttribute("postAddToViewEventBean", bean);
processLifecycleExecute();
EasyMock.verify(bean);
}
use of jakarta.faces.event.ComponentSystemEvent in project mojarra by eclipse-ee4j.
the class WebsocketFacesListener method processEvent.
/**
* If the websocket has just switched its <code>rendered</code> or <code>connected</code> attribute, then render either
* the <code>open()</code> script or the <code>close()</code> script. During an ajax request with partial rendering,
* it's added as <code><eval></code> by partial response writer, else it's just added as a script component with
* <code>target="body"</code>.
*/
@Override
public void processEvent(SystemEvent event) throws AbortProcessingException {
if (!(event instanceof PreRenderViewEvent)) {
return;
}
FacesContext context = ((ComponentSystemEvent) event).getFacesContext();
Map<String, Boolean> initializedWebsockets = getInitializedWebsockets(context);
if (!context.getPartialViewContext().isAjaxRequest()) {
initializedWebsockets.clear();
}
for (Entry<String, Boolean> initializedWebsocket : initializedWebsockets.entrySet()) {
String clientId = initializedWebsocket.getKey();
UIWebsocket websocket = (UIWebsocket) context.getViewRoot().findComponent(clientId);
boolean connected = websocket.isRendered() && websocket.isConnected();
boolean previouslyConnected = initializedWebsocket.setValue(connected);
if (previouslyConnected != connected) {
String script = String.format(connected ? SCRIPT_OPEN : SCRIPT_CLOSE, clientId);
PartialViewContext pvc = context.getPartialViewContext();
if (pvc.isAjaxRequest() && !pvc.isRenderAll()) {
context.getPartialViewContext().getEvalScripts().add(script);
} else {
UIOutput outputScript = new UIOutput();
outputScript.setRendererType("jakarta.faces.resource.Script");
UIOutput content = new UIOutput();
content.setValue(script);
outputScript.getChildren().add(content);
context.getViewRoot().addComponentResource(context, outputScript, "body");
}
}
}
}
use of jakarta.faces.event.ComponentSystemEvent in project faces by jakartaee.
the class TestServlet method preRemoveFromViewEventIsApproiateListenerPostiveTest.
// ------------------------------------------------------------
// PreRemoveFromViewEvent Tests
public void preRemoveFromViewEventIsApproiateListenerPostiveTest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter pw = response.getWriter();
SystemEventListener testListener = new TestSystemEventListener();
ComponentSystemEvent cse = createEvent(getTestComponent());
String eventName = cse.getClass().getName();
if (cse.isAppropriateListener(testListener)) {
pw.println(JSFTestUtil.PASS);
} else {
pw.println("Test FAILED. " + eventName + ".isAppropriateListener " + "did not return true when SystemEventListener was passed in " + "as a parameter");
}
}
Aggregations