Search in sources :

Example 1 with EventTypes

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

the class EventsTest method asynchronousEvents.

@Test
@Ignore
public void asynchronousEvents() {
    List<EventTypes> asyncEvents = asynchonousEvents();
    Iterator<EventTypes> itr = asyncEvents.iterator();
    while (itr.hasNext()) {
        EventTypes et = itr.next();
        for (EventListener.Event evt : allEvents) {
            if (evt.is(et)) {
                itr.remove();
            }
        }
    }
    for (EventTypes et : asyncEvents) {
        System.out.println("Asynchronous event " + et.type() + " was not received");
    }
    Assert.assertTrue(asyncEvents.size() == 0);
}
Also used : EventTypes(org.glassfish.api.event.EventTypes) EventListener(org.glassfish.api.event.EventListener) ConfigApiTest(org.glassfish.tests.utils.ConfigApiTest)

Example 2 with EventTypes

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

the class EventsTest method deployUndeployTest.

@Test
public void deployUndeployTest() throws Exception {
    final List<EventTypes> myTestEvents = getSingletonModuleSuccessfullDeploymentEvents();
    Events events = habitat.getService(Events.class);
    EventListener listener = new EventListener() {

        public void event(Event event) {
            if (myTestEvents.contains(event.type())) {
                myTestEvents.remove(event.type());
            }
        }
    };
    events.register(listener);
    Deployment deployment = habitat.getService(Deployment.class);
    DeployCommandParameters params = new DeployCommandParameters(application);
    params.name = "fakeApplication";
    params.target = "server";
    ActionReport report = habitat.getService(ActionReport.class, "hk2-agent");
    ExtendedDeploymentContext dc = deployment.getBuilder(Logger.getAnonymousLogger(), params, report).source(application).build();
    deployment.deploy(dc);
    events.unregister(listener);
    for (EventTypes et : myTestEvents) {
        System.out.println("An expected event of type " + et.type() + " was not received");
    }
    try {
        final List<EventTypes> myTestEvents2 = getSingletonModuleSuccessfullUndeploymentEvents();
        EventListener listener2 = new EventListener() {

            public void event(Event event) {
                if (myTestEvents2.contains(event.type())) {
                    myTestEvents2.remove(event.type());
                }
            }
        };
        events.register(listener2);
        UndeployCommandParameters params2 = new UndeployCommandParameters("fakeApplication");
        params2.target = "server";
        ActionReport report2 = habitat.getService(ActionReport.class, "hk2-agent");
        ExtendedDeploymentContext dc2 = deployment.getBuilder(Logger.getAnonymousLogger(), params2, report2).source(application).build();
        deployment.undeploy("fakeApplication", dc2);
        events.unregister(listener2);
        for (EventTypes et : myTestEvents2) {
            System.out.println("An expected event of type " + et.type() + " was not received");
        }
    } catch (Throwable t) {
        t.printStackTrace();
    }
}
Also used : DeployCommandParameters(org.glassfish.api.deployment.DeployCommandParameters) UndeployCommandParameters(org.glassfish.api.deployment.UndeployCommandParameters) Events(org.glassfish.api.event.Events) Deployment(org.glassfish.internal.deployment.Deployment) EventTypes(org.glassfish.api.event.EventTypes) EventListener(org.glassfish.api.event.EventListener) ActionReport(org.glassfish.api.ActionReport) ExtendedDeploymentContext(org.glassfish.internal.deployment.ExtendedDeploymentContext) ConfigApiTest(org.glassfish.tests.utils.ConfigApiTest)

Example 3 with EventTypes

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

Example 4 with EventTypes

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

the class ImmutableTest method test1.

@Test
public void test1() {
    EventTypes evt1 = EventTypes.create("foo");
    EventTypes evt2 = EventTypes.create("foo");
    EventTypes evt3 = EventTypes.create("foo34");
    Assert.assertNotSame(evt1, evt3);
    Assert.assertEquals(evt1, evt2);
    Assert.assertTrue(evt1 == evt2);
    Assert.assertFalse(evt1 == evt3);
}
Also used : EventTypes(org.glassfish.api.event.EventTypes) Test(org.junit.Test)

Aggregations

EventTypes (org.glassfish.api.event.EventTypes)4 EventListener (org.glassfish.api.event.EventListener)3 ConfigApiTest (org.glassfish.tests.utils.ConfigApiTest)2 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)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 DeploymentException (org.glassfish.deployment.common.DeploymentException)1 Deployment (org.glassfish.internal.deployment.Deployment)1 ExtendedDeploymentContext (org.glassfish.internal.deployment.ExtendedDeploymentContext)1 Test (org.junit.Test)1