Search in sources :

Example 6 with EventListener

use of org.glassfish.api.event.EventListener 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

EventListener (org.glassfish.api.event.EventListener)6 EventTypes (org.glassfish.api.event.EventTypes)2 Method (java.lang.reflect.Method)1 MalformedURLException (java.net.MalformedURLException)1 PrivilegedActionException (java.security.PrivilegedActionException)1 PrivilegedExceptionAction (java.security.PrivilegedExceptionAction)1 ArrayList (java.util.ArrayList)1 Context (javax.naming.Context)1 NamingException (javax.naming.NamingException)1 ActionReport (org.glassfish.api.ActionReport)1 DeployCommandParameters (org.glassfish.api.deployment.DeployCommandParameters)1 UndeployCommandParameters (org.glassfish.api.deployment.UndeployCommandParameters)1 Event (org.glassfish.api.event.EventListener.Event)1 Events (org.glassfish.api.event.Events)1 RestrictTo (org.glassfish.api.event.RestrictTo)1 NamingObjectProxy (org.glassfish.api.naming.NamingObjectProxy)1 DeploymentException (org.glassfish.deployment.common.DeploymentException)1 ActiveDescriptor (org.glassfish.hk2.api.ActiveDescriptor)1 DelegatingClassLoader (org.glassfish.internal.api.DelegatingClassLoader)1 Deployment (org.glassfish.internal.deployment.Deployment)1