Search in sources :

Example 21 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 22 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)

Example 23 with EventAdmin

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

the class MockEventAdminTest method testPostEvents.

@Test(timeout = 2000)
public void testPostEvents() {
    EventAdmin eventAdmin = context.getService(EventAdmin.class);
    eventAdmin.postEvent(EVENT_SAMPLE_2);
    eventAdmin.postEvent(EVENT_OTHER_3);
    // wait until result is as expected (with timeout)
    boolean expectedResult = false;
    while (!expectedResult) {
        expectedResult = ObjectUtils.equals(ImmutableList.of(), eventHandler1.getReceivedEvents()) && ObjectUtils.equals(ImmutableList.of(EVENT_SAMPLE_2), eventHandler12.getReceivedEvents()) && ObjectUtils.equals(ImmutableList.of(EVENT_SAMPLE_2), eventHandlerSampleAll.getReceivedEvents()) && ObjectUtils.equals(ImmutableList.of(EVENT_SAMPLE_2, EVENT_OTHER_3), eventHandlerAll.getReceivedEvents());
    }
}
Also used : EventAdmin(org.osgi.service.event.EventAdmin) Test(org.junit.Test)

Example 24 with EventAdmin

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

the class MockAdapterManagerImpl method unregisterAdapterFactory.

/**
     * Check that the package containing the class is exported or is a java.*
     * class.
     * 
     * @param packageAdmin the PackageAdmin service
     * @param clazz the class name
     * @return true if the package is exported
     */
// DISABLED IN THIS COPY OF CLASS
/*
    static boolean checkPackage(PackageAdmin packageAdmin, String clazz) {
        final String packageName = getPackageName(clazz); 
        if (packageName.startsWith("java.")) {
            return true;
        }
        return packageAdmin.getExportedPackage(packageName) != null;
    }
    */
/**
     * Unregisters the {@link AdapterFactory} referred to by the service
     * <code>reference</code> from the registry.
     */
private void unregisterAdapterFactory(final ServiceReference reference) {
    synchronized (this.boundAdapterFactories) {
        boundAdapterFactories.remove(reference);
    }
    final String[] adaptables = PropertiesUtil.toStringArray(reference.getProperty(ADAPTABLE_CLASSES));
    final String[] adapters = PropertiesUtil.toStringArray(reference.getProperty(ADAPTER_CLASSES));
    if (adaptables == null || adaptables.length == 0 || adapters == null || adapters.length == 0) {
        return;
    }
    boolean factoriesModified = false;
    AdapterFactoryDescriptorMap adfMap = null;
    AdapterFactoryDescriptor removedDescriptor = null;
    for (final String adaptable : adaptables) {
        synchronized (this.descriptors) {
            adfMap = this.descriptors.get(adaptable);
        }
        if (adfMap != null) {
            synchronized (adfMap) {
                AdapterFactoryDescriptor factoryDesc = adfMap.remove(reference);
                if (factoryDesc != null) {
                    factoriesModified = true;
                    // Since the code paths above does not fully guarantee it though, let's keep this check in place
                    if (removedDescriptor != null && removedDescriptor != factoryDesc) {
                        log.error("When unregistering reference {} got duplicate service descriptors {} and {}. Unregistration of {} services may be incomplete.", new Object[] { reference, removedDescriptor, factoryDesc, Adaption.class.getName() });
                    }
                    removedDescriptor = factoryDesc;
                }
            }
        }
    }
    // removed
    if (factoriesModified) {
        this.factoryCache.clear();
    }
    // unregister adaption
    if (removedDescriptor != null) {
        removedDescriptor.getAdaption().unregister();
        if (log.isDebugEnabled()) {
            log.debug("Unregistered service {} with {} : {} and {} : {}", new Object[] { Adaption.class.getName(), SlingConstants.PROPERTY_ADAPTABLE_CLASSES, Arrays.toString(adaptables), SlingConstants.PROPERTY_ADAPTER_CLASSES, Arrays.toString(adapters) });
        }
    }
    // send event
    final EventAdmin localEA = this.eventAdmin;
    if (localEA != null) {
        final Dictionary<String, Object> props = new Hashtable<String, Object>();
        props.put(SlingConstants.PROPERTY_ADAPTABLE_CLASSES, adaptables);
        props.put(SlingConstants.PROPERTY_ADAPTER_CLASSES, adapters);
        localEA.postEvent(new Event(SlingConstants.TOPIC_ADAPTER_FACTORY_REMOVED, props));
    }
}
Also used : AdapterFactoryDescriptorMap(org.apache.sling.adapter.internal.AdapterFactoryDescriptorMap) Adaption(org.apache.sling.adapter.Adaption) EventAdmin(org.osgi.service.event.EventAdmin) AdapterFactoryDescriptor(org.apache.sling.adapter.internal.AdapterFactoryDescriptor) Hashtable(java.util.Hashtable) Event(org.osgi.service.event.Event)

Example 25 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)

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