Search in sources :

Example 16 with EventAdmin

use of org.osgi.service.event.EventAdmin in project sling by apache.

the class DistributedEventSender method runInBackground.

/**
     * Background thread
     */
private void runInBackground() {
    while (this.running) {
        // so let's wait/get the next event from the queue
        String path = null;
        try {
            path = this.queue.take();
        } catch (final InterruptedException e) {
            this.ignoreException(e);
            Thread.currentThread().interrupt();
            this.running = false;
        }
        if (path != null && path.length() > 0 && this.running) {
            ResourceResolver resolver = null;
            try {
                resolver = this.resourceResolverFactory.getServiceResourceResolver(null);
                final Resource eventResource = resolver.getResource(path);
                if (eventResource == null) {
                    this.logger.warn("runInBackground : resource not found at " + path);
                } else if (DistributedEventAdminImpl.RESOURCE_TYPE_EVENT.equals(eventResource.getResourceType())) {
                    final Event e = this.readEvent(eventResource);
                    if (e != null) {
                        // we check event admin as processing is async
                        final EventAdmin localEA = this.eventAdmin;
                        if (localEA != null) {
                            localEA.postEvent(e);
                        } else {
                            this.logger.error("Unable to post event as no event admin is available.");
                        }
                    }
                }
            } catch (final LoginException ex) {
                this.logger.error("Exception during creation of resource resolver.", ex);
            } finally {
                if (resolver != null) {
                    resolver.close();
                }
            }
        }
    }
}
Also used : EventAdmin(org.osgi.service.event.EventAdmin) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) Event(org.osgi.service.event.Event) LoginException(org.apache.sling.api.resource.LoginException)

Example 17 with EventAdmin

use of org.osgi.service.event.EventAdmin in project ddf by codice.

the class SendCommand method sendNotification.

private void sendNotification() throws Exception {
    Long sysTimeMillis = System.currentTimeMillis();
    String id = UUID.randomUUID().toString().replaceAll("-", "");
    String sessionId = "mockSessionId";
    Notification notification = new Notification(id, sessionId, application, title, message, sysTimeMillis, userId);
    notification.put("status", "Started");
    notification.put("bytes", "12345");
    Event event = new Event(Notification.NOTIFICATION_TOPIC_DOWNLOADS, notification);
    // Get OSGi Event Admin service
    EventAdmin eventAdmin;
    @SuppressWarnings("rawtypes") ServiceReference[] serviceReferences = bundleContext.getServiceReferences(SERVICE_PID, null);
    if (serviceReferences == null || serviceReferences.length != 1) {
        LOGGER.debug("Found no service references for {}", SERVICE_PID);
    } else {
        LOGGER.debug("Found " + serviceReferences.length + " service references for " + SERVICE_PID);
        eventAdmin = (EventAdmin) bundleContext.getService(serviceReferences[0]);
        if (eventAdmin != null) {
            eventAdmin.postEvent(event);
        }
    }
}
Also used : EventAdmin(org.osgi.service.event.EventAdmin) Event(org.osgi.service.event.Event) Notification(org.codice.ddf.notifications.Notification) ServiceReference(org.osgi.framework.ServiceReference)

Example 18 with EventAdmin

use of org.osgi.service.event.EventAdmin in project ddf by codice.

the class SendCommand method sendActivity.

private void sendActivity() throws Exception {
    String id = UUID.randomUUID().toString().replaceAll("-", "");
    String sessionId = "mockSessionId";
    Map<String, String> operations = new HashMap<>();
    operations.put("cancel", "true");
    ActivityEvent eventProperties = new ActivityEvent(id, sessionId, new Date(), "Activity category", "Activity title", "Activity message", UNKNOWN_PROGRESS, operations, userId, ActivityStatus.RUNNING, 100L);
    Event event = new Event(ActivityEvent.EVENT_TOPIC, eventProperties);
    // Get OSGi Event Admin service
    EventAdmin eventAdmin;
    @SuppressWarnings("rawtypes") ServiceReference[] serviceReferences = bundleContext.getServiceReferences(SERVICE_PID, null);
    if (serviceReferences == null || serviceReferences.length != 1) {
        LOGGER.debug("Found no service references for {}", SERVICE_PID);
    } else {
        LOGGER.debug("Found " + serviceReferences.length + " service references for " + SERVICE_PID);
        eventAdmin = (EventAdmin) bundleContext.getService(serviceReferences[0]);
        if (eventAdmin != null) {
            eventAdmin.postEvent(event);
        }
    }
}
Also used : ActivityEvent(org.codice.ddf.activities.ActivityEvent) HashMap(java.util.HashMap) EventAdmin(org.osgi.service.event.EventAdmin) ActivityEvent(org.codice.ddf.activities.ActivityEvent) Event(org.osgi.service.event.Event) Date(java.util.Date) ServiceReference(org.osgi.framework.ServiceReference)

Example 19 with EventAdmin

use of org.osgi.service.event.EventAdmin in project sling by apache.

the class MockEventAdminTest method testSendEvent_Other3.

@Test
public void testSendEvent_Other3() {
    EventAdmin eventAdmin = context.getService(EventAdmin.class);
    eventAdmin.sendEvent(EVENT_OTHER_3);
    assertEquals(ImmutableList.of(), eventHandler1.getReceivedEvents());
    assertEquals(ImmutableList.of(), eventHandler12.getReceivedEvents());
    assertEquals(ImmutableList.of(), eventHandlerSampleAll.getReceivedEvents());
    assertEquals(ImmutableList.of(EVENT_OTHER_3), eventHandlerAll.getReceivedEvents());
}
Also used : EventAdmin(org.osgi.service.event.EventAdmin) Test(org.junit.Test)

Example 20 with EventAdmin

use of org.osgi.service.event.EventAdmin in project sling by apache.

the class MockEventAdminTest method testSendEvent_Sample2.

@Test
public void testSendEvent_Sample2() {
    EventAdmin eventAdmin = context.getService(EventAdmin.class);
    eventAdmin.sendEvent(EVENT_SAMPLE_2);
    assertEquals(ImmutableList.of(), eventHandler1.getReceivedEvents());
    assertEquals(ImmutableList.of(EVENT_SAMPLE_2), eventHandler12.getReceivedEvents());
    assertEquals(ImmutableList.of(EVENT_SAMPLE_2), eventHandlerSampleAll.getReceivedEvents());
    assertEquals(ImmutableList.of(EVENT_SAMPLE_2), eventHandlerAll.getReceivedEvents());
}
Also used : EventAdmin(org.osgi.service.event.EventAdmin) Test(org.junit.Test)

Aggregations

EventAdmin (org.osgi.service.event.EventAdmin)26 Event (org.osgi.service.event.Event)18 Hashtable (java.util.Hashtable)11 Test (org.junit.Test)7 Adaption (org.apache.sling.adapter.Adaption)4 BundleContext (org.osgi.framework.BundleContext)4 HashMap (java.util.HashMap)3 Fixture (org.apache.sling.resourceresolver.impl.Fixture)3 ServiceReference (org.osgi.framework.ServiceReference)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 FeatureEvent (org.apache.karaf.features.FeatureEvent)2 RepositoryEvent (org.apache.karaf.features.RepositoryEvent)2 AdapterFactoryDescriptor (org.apache.sling.adapter.internal.AdapterFactoryDescriptor)2 AdapterFactoryDescriptorMap (org.apache.sling.adapter.internal.AdapterFactoryDescriptorMap)2 ChangeListener (org.apache.sling.resourceresolver.impl.providers.ResourceProviderTracker.ChangeListener)2 Before (org.junit.Before)2 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)1 Date (java.util.Date)1 Dictionary (java.util.Dictionary)1 EventObject (java.util.EventObject)1