Search in sources :

Example 1 with RestrictTo

use of org.glassfish.api.event.RestrictTo in project Payara by payara.

the class EventsImpl method send.

@Override
public void send(final Event event, boolean asynchronously) {
    List<EventListener> l = new ArrayList<EventListener>();
    l.addAll(listeners);
    for (final EventListener listener : l) {
        Method m = null;
        try {
            // check if the listener is interested with his event.
            m = listener.getClass().getMethod("event", Event.class);
        } catch (Throwable ex) {
            // We need to catch Throwable, otherwise we can server not to
            // shutdown when the following happens:
            // Assume a bundle which has registered a event listener
            // has been uninstalled without unregistering the listener.
            // listener.getClass() refers to a class of such an uninstalled
            // bundle. If framework has been refreshed, then the
            // classloader can't be used further to load any classes.
            // As a result, an exception like NoClassDefFoundError is thrown
            // from getMethod.
            logger.log(Level.SEVERE, KernelLoggerInfo.exceptionSendEvent, ex);
        }
        if (m != null) {
            RestrictTo fooBar = m.getParameterTypes()[0].getAnnotation(RestrictTo.class);
            if (fooBar != null) {
                EventTypes interested = EventTypes.create(fooBar.value());
                if (!event.is(interested)) {
                    continue;
                }
            }
        }
        if (asynchronously) {
            executor.submit(new Runnable() {

                @Override
                public void run() {
                    try {
                        listener.event(event);
                    } catch (Throwable e) {
                        logger.log(Level.WARNING, KernelLoggerInfo.exceptionDispatchEvent, e);
                    }
                }
            });
        } else {
            try {
                listener.event(event);
            } catch (DeploymentException de) {
                // we re-throw the exception to abort the deployment
                throw de;
            } catch (Throwable e) {
                logger.log(Level.WARNING, KernelLoggerInfo.exceptionDispatchEvent, e);
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) Event(org.glassfish.api.event.EventListener.Event) RestrictTo(org.glassfish.api.event.RestrictTo) EventTypes(org.glassfish.api.event.EventTypes) DeploymentException(org.glassfish.deployment.common.DeploymentException) EventListener(org.glassfish.api.event.EventListener) Method(java.lang.reflect.Method)

Aggregations

Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 EventListener (org.glassfish.api.event.EventListener)1 Event (org.glassfish.api.event.EventListener.Event)1 EventTypes (org.glassfish.api.event.EventTypes)1 RestrictTo (org.glassfish.api.event.RestrictTo)1 DeploymentException (org.glassfish.deployment.common.DeploymentException)1