Search in sources :

Example 1 with SystemEventListener

use of jakarta.faces.event.SystemEventListener in project myfaces by apache.

the class ApplicationImplEventManager method publishEvent.

public void publishEvent(FacesContext facesContext, Class<? extends SystemEvent> systemEventClass, Class<?> sourceBaseType, Object source) {
    // Call events only if event processing is enabled.
    if (!facesContext.isProcessingEvents()) {
        return;
    }
    // spec: If this argument is null the return from source.getClass() must be used as the sourceBaseType.
    if (sourceBaseType == null) {
        sourceBaseType = source.getClass();
    }
    try {
        SystemEvent event = null;
        // component attached listeners
        if (source instanceof SystemEventListenerHolder) {
            List<SystemEventListener> listeners = ((SystemEventListenerHolder) source).getListenersForEventClass(systemEventClass);
            event = processComponentAttachedListeners(facesContext, listeners, systemEventClass, source, event);
        }
        // view attached listeners
        UIViewRoot viewRoot = facesContext.getViewRoot();
        if (viewRoot != null) {
            List<SystemEventListener> listeners = viewRoot.getViewListenersForEventClass(systemEventClass);
            event = processViewAttachedListeners(facesContext, listeners, systemEventClass, source, event);
        }
        // global listeners
        List<EventInfo> eventInfos = globalListeners.get(systemEventClass);
        event = processGlobalListeners(facesContext, eventInfos, systemEventClass, source, event, sourceBaseType);
    } catch (AbortProcessingException e) {
        // If the act of invoking the processListener method causes an AbortProcessingException to be thrown,
        // processing of the listeners must be aborted, no further processing of the listeners for this event must
        // take place, and the exception must be logged with Level.SEVERE.
        log.log(Level.SEVERE, "Event processing was aborted", e);
    }
}
Also used : SystemEvent(jakarta.faces.event.SystemEvent) SystemEventListener(jakarta.faces.event.SystemEventListener) SystemEventListenerHolder(jakarta.faces.event.SystemEventListenerHolder) AbortProcessingException(jakarta.faces.event.AbortProcessingException) UIViewRoot(jakarta.faces.component.UIViewRoot)

Example 2 with SystemEventListener

use of jakarta.faces.event.SystemEventListener in project myfaces by apache.

the class ApplicationImplEventManager method processViewAttachedListeners.

protected SystemEvent processViewAttachedListeners(FacesContext facesContext, List<? extends SystemEventListener> listeners, Class<? extends SystemEvent> systemEventClass, Object source, SystemEvent event) {
    if (listeners == null || listeners.isEmpty()) {
        return event;
    }
    int processedListenerIndex = 0;
    // Do it with a copy because the list could be changed during a event see MYFACES-2935
    List<SystemEventListener> listenersCopy = new ArrayList<>(listeners);
    // and the loop will be complete.
    while (processedListenerIndex < listenersCopy.size()) {
        for (; processedListenerIndex < listenersCopy.size(); processedListenerIndex++) {
            SystemEventListener listener = listenersCopy.get(processedListenerIndex);
            if (listener.isListenerForSource(source)) {
                // Lazy construct the event; zhis same event instance must be passed to all listener instances.
                if (event == null) {
                    event = createEvent(systemEventClass, facesContext, source);
                }
                if (event.isAppropriateListener(listener)) {
                    event.processListener(listener);
                }
            }
        }
        boolean listChanged = false;
        if (listeners.size() == listenersCopy.size()) {
            for (int i = 0; i < listenersCopy.size(); i++) {
                if (listenersCopy.get(i) != listeners.get(i)) {
                    listChanged = true;
                    break;
                }
            }
        } else {
            listChanged = true;
        }
        if (listChanged) {
            for (int i = 0; i < listeners.size(); i++) {
                SystemEventListener listener = listeners.get(i);
                // check if listenersCopy.get(i) is valid
                if (i < listenersCopy.size()) {
                    // so as heuristic, check first if we can find it at the same location
                    if (!listener.equals(listenersCopy.get(i)) && !listenersCopy.contains(listener)) {
                        listenersCopy.add(listener);
                    }
                } else {
                    if (!listenersCopy.contains(listener)) {
                        listenersCopy.add(listener);
                    }
                }
            }
        }
    }
    return event;
}
Also used : SystemEventListener(jakarta.faces.event.SystemEventListener) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 3 with SystemEventListener

use of jakarta.faces.event.SystemEventListener in project myfaces by apache.

the class UIComponent method unsubscribeFromEvent.

public void unsubscribeFromEvent(Class<? extends SystemEvent> eventClass, ComponentSystemEventListener componentListener) {
    /*
         * When doing the comparison to determine if an existing listener is equal to the argument componentListener
         * (and thus must be removed), the equals() method on the existing listener must be invoked, passing the
         * argument componentListener, rather than the other way around.
         * 
         * -=Simon Lessard=- What is that supposed to mean? Are we supposed to keep
         * an internal map of created listener wrappers?
         * -= Leonardo Uribe=- Yes, it is supposed a wrapper should be used to hold listener references, to prevent
         * serialize component instances on the state.
         */
    Assert.notNull(eventClass, "eventClass");
    Assert.notNull(componentListener, "componentListener");
    if (_systemEventListenerClassMap != null) {
        List<SystemEventListener> listeners = _systemEventListenerClassMap.get(eventClass);
        if (listeners != null && !listeners.isEmpty()) {
            for (Iterator<SystemEventListener> it = listeners.iterator(); it.hasNext(); ) {
                ComponentSystemEventListener listener = ((_EventListenerWrapper) it.next()).getComponentSystemEventListener();
                if (listener != null && listener.equals(componentListener)) {
                    it.remove();
                    break;
                }
            }
        }
    }
}
Also used : SystemEventListener(jakarta.faces.event.SystemEventListener) ComponentSystemEventListener(jakarta.faces.event.ComponentSystemEventListener) ComponentSystemEventListener(jakarta.faces.event.ComponentSystemEventListener)

Example 4 with SystemEventListener

use of jakarta.faces.event.SystemEventListener in project mojarra by eclipse-ee4j.

the class ListenerForHandler method apply.

// ----------------------------------- Methods from RuntimeAnnotationHandler
@SuppressWarnings({ "UnusedDeclaration" })
@Override
public void apply(FacesContext ctx, Object... params) {
    Object listener;
    UIComponent target;
    if (params.length == 2) {
        // handling @ListenerFor on a Renderer
        listener = params[0];
        target = (UIComponent) params[1];
    } else {
        // handling @ListenerFor on a UIComponent
        listener = params[0];
        target = (UIComponent) params[0];
    }
    if (listener instanceof ComponentSystemEventListener) {
        for (int i = 0, len = listenersFor.length; i < len; i++) {
            target.subscribeToEvent(listenersFor[i].systemEventClass(), (ComponentSystemEventListener) listener);
        }
    } else if (listener instanceof SystemEventListener) {
        Class sourceClassValue = null;
        Application app = ctx.getApplication();
        for (int i = 0, len = listenersFor.length; i < len; i++) {
            sourceClassValue = listenersFor[i].sourceClass();
            if (sourceClassValue == Void.class) {
                app.subscribeToEvent(listenersFor[i].systemEventClass(), (SystemEventListener) listener);
            } else {
                app.subscribeToEvent(listenersFor[i].systemEventClass(), listenersFor[i].sourceClass(), (SystemEventListener) listener);
            }
        }
    }
}
Also used : ComponentSystemEventListener(jakarta.faces.event.ComponentSystemEventListener) SystemEventListener(jakarta.faces.event.SystemEventListener) ComponentSystemEventListener(jakarta.faces.event.ComponentSystemEventListener) UIComponent(jakarta.faces.component.UIComponent) Application(jakarta.faces.application.Application)

Example 5 with SystemEventListener

use of jakarta.faces.event.SystemEventListener in project mojarra by eclipse-ee4j.

the class UIComponentBase method subscribeToEvent.

/**
 * <p class="changed_added_2_1">
 * Install the listener instance referenced by argument <code>componentListener</code> as a listener for events of type
 * <code>eventClass</code> originating from this specific instance of <code>UIComponent</code>. The default
 * implementation creates an inner {@link SystemEventListener} instance that wraps argument
 * <code>componentListener</code> as the <code>listener</code> argument. This inner class must call through to the
 * argument <code>componentListener</code> in its implementation of {@link SystemEventListener#processEvent} and its
 * implementation of {@link SystemEventListener#isListenerForSource} must return true if the instance class of this
 * <code>UIComponent</code> is assignable from the argument to <code>isListenerForSource</code>.
 * </p>
 * <p class="changed_modified_4_0">
 * The listener instance referenced by argument <code>componentListener</code> may not already be installed as a listener for events of type
 * <code>eventClass</code> originating from this specific instance of <code>UIComponent</code>. When doing the
 * comparison to determine if an existing listener is equal to the argument <code>componentListener</code>,
 * the <code>equals()</code> method on the <em>existing listener</em> must be invoked, passing the
 * argument <code>componentListener</code>, rather than the other way around.
 * </p>
 *
 * @param eventClass the <code>Class</code> of event for which <code>listener</code> must be fired.
 * @param componentListener the implementation of {@link jakarta.faces.event.ComponentSystemEventListener} whose
 * {@link jakarta.faces.event.ComponentSystemEventListener#processEvent} method must be called when events of type
 * <code>facesEventClass</code> are fired.
 *
 * @throws NullPointerException if any of the arguments are <code>null</code>.
 *
 * @since 2.1
 */
@Override
public void subscribeToEvent(Class<? extends SystemEvent> eventClass, ComponentSystemEventListener componentListener) {
    if (isAnyNull(eventClass, componentListener)) {
        throw new NullPointerException();
    }
    if (initialStateMarked()) {
        initialState = false;
    }
    if (listenersByEventClass == null) {
        listenersByEventClass = new HashMap<>(3, 1.0f);
    }
    SystemEventListener facesLifecycleListener = new ComponentSystemEventListenerAdapter(componentListener, this);
    List<SystemEventListener> listenersForEventClass = listenersByEventClass.get(eventClass);
    if (listenersForEventClass == null) {
        listenersForEventClass = new ArrayList<>(3);
        listenersByEventClass.put(eventClass, listenersForEventClass);
    }
    if (!listenersForEventClass.contains(facesLifecycleListener)) {
        listenersForEventClass.add(facesLifecycleListener);
    }
}
Also used : SystemEventListener(jakarta.faces.event.SystemEventListener) ComponentSystemEventListener(jakarta.faces.event.ComponentSystemEventListener)

Aggregations

SystemEventListener (jakarta.faces.event.SystemEventListener)34 PrintWriter (java.io.PrintWriter)17 UIComponent (jakarta.faces.component.UIComponent)15 SystemEvent (jakarta.faces.event.SystemEvent)15 TCKSystemEvent (com.sun.ts.tests.jsf.common.event.TCKSystemEvent)14 TCKSystemEventListener (com.sun.ts.tests.jsf.common.listener.TCKSystemEventListener)14 ELException (jakarta.el.ELException)11 ServletException (jakarta.servlet.ServletException)11 IOException (java.io.IOException)11 Application (jakarta.faces.application.Application)10 FacesException (jakarta.faces.FacesException)5 ApplicationWrapper (jakarta.faces.application.ApplicationWrapper)5 ComponentSystemEventListener (jakarta.faces.event.ComponentSystemEventListener)5 EventInfo (com.sun.faces.application.applicationimpl.events.EventInfo)4 UIViewRoot (jakarta.faces.component.UIViewRoot)4 TestSystemEventListener (com.sun.ts.tests.jsf.api.jakarta_faces.event.common.TestSystemEventListener)2 ComponentSystemEvent (jakarta.faces.event.ComponentSystemEvent)2 SystemEventListenerHolder (jakarta.faces.event.SystemEventListenerHolder)2 ConfigurationException (com.sun.faces.config.ConfigurationException)1 Util.getLocaleFromString (com.sun.faces.util.Util.getLocaleFromString)1