Search in sources :

Example 1 with ServletContextAttributeEvent

use of jakarta.servlet.ServletContextAttributeEvent in project tomcat by apache.

the class ApplicationContext method removeAttribute.

@Override
public void removeAttribute(String name) {
    Object value = null;
    // Check for read only attribute
    if (readOnlyAttributes.containsKey(name)) {
        return;
    }
    value = attributes.remove(name);
    if (value == null) {
        return;
    }
    // Notify interested application event listeners
    Object[] listeners = context.getApplicationEventListeners();
    if ((listeners == null) || (listeners.length == 0)) {
        return;
    }
    ServletContextAttributeEvent event = new ServletContextAttributeEvent(context.getServletContext(), name, value);
    for (Object obj : listeners) {
        if (!(obj instanceof ServletContextAttributeListener)) {
            continue;
        }
        ServletContextAttributeListener listener = (ServletContextAttributeListener) obj;
        try {
            context.fireContainerEvent("beforeContextAttributeRemoved", listener);
            listener.attributeRemoved(event);
            context.fireContainerEvent("afterContextAttributeRemoved", listener);
        } catch (Throwable t) {
            ExceptionUtils.handleThrowable(t);
            context.fireContainerEvent("afterContextAttributeRemoved", listener);
            // FIXME - should we do anything besides log these?
            log(sm.getString("applicationContext.attributeEvent"), t);
        }
    }
}
Also used : ServletContextAttributeListener(jakarta.servlet.ServletContextAttributeListener) ServletContextAttributeEvent(jakarta.servlet.ServletContextAttributeEvent)

Example 2 with ServletContextAttributeEvent

use of jakarta.servlet.ServletContextAttributeEvent in project tomcat by apache.

the class ApplicationContext method setAttribute.

@Override
public void setAttribute(String name, Object value) {
    // Name cannot be null
    if (name == null) {
        throw new NullPointerException(sm.getString("applicationContext.setAttribute.namenull"));
    }
    // Null value is the same as removeAttribute()
    if (value == null) {
        removeAttribute(name);
        return;
    }
    // Check for read only attribute
    if (readOnlyAttributes.containsKey(name)) {
        return;
    }
    Object oldValue = attributes.put(name, value);
    boolean replaced = oldValue != null;
    // Notify interested application event listeners
    Object[] listeners = context.getApplicationEventListeners();
    if ((listeners == null) || (listeners.length == 0)) {
        return;
    }
    ServletContextAttributeEvent event = null;
    if (replaced) {
        event = new ServletContextAttributeEvent(context.getServletContext(), name, oldValue);
    } else {
        event = new ServletContextAttributeEvent(context.getServletContext(), name, value);
    }
    for (Object obj : listeners) {
        if (!(obj instanceof ServletContextAttributeListener)) {
            continue;
        }
        ServletContextAttributeListener listener = (ServletContextAttributeListener) obj;
        try {
            if (replaced) {
                context.fireContainerEvent("beforeContextAttributeReplaced", listener);
                listener.attributeReplaced(event);
                context.fireContainerEvent("afterContextAttributeReplaced", listener);
            } else {
                context.fireContainerEvent("beforeContextAttributeAdded", listener);
                listener.attributeAdded(event);
                context.fireContainerEvent("afterContextAttributeAdded", listener);
            }
        } catch (Throwable t) {
            ExceptionUtils.handleThrowable(t);
            if (replaced) {
                context.fireContainerEvent("afterContextAttributeReplaced", listener);
            } else {
                context.fireContainerEvent("afterContextAttributeAdded", listener);
            }
            // FIXME - should we do anything besides log these?
            log(sm.getString("applicationContext.attributeEvent"), t);
        }
    }
}
Also used : ServletContextAttributeListener(jakarta.servlet.ServletContextAttributeListener) ServletContextAttributeEvent(jakarta.servlet.ServletContextAttributeEvent)

Aggregations

ServletContextAttributeEvent (jakarta.servlet.ServletContextAttributeEvent)2 ServletContextAttributeListener (jakarta.servlet.ServletContextAttributeListener)2