Search in sources :

Example 1 with EventDispatcher

use of org.eclipse.osgi.framework.eventmgr.EventDispatcher in project rt.equinox.framework by eclipse.

the class EquinoxEventPublisher method flushFrameworkEvents.

void flushFrameworkEvents() {
    EventDispatcher<Object, Object, CountDownLatch> dispatcher = new EventDispatcher<Object, Object, CountDownLatch>() {

        @Override
        public void dispatchEvent(Object eventListener, Object listenerObject, int eventAction, CountDownLatch flushedSignal) {
            // Signal that we have flushed all events
            flushedSignal.countDown();
        }
    };
    ListenerQueue<Object, Object, CountDownLatch> queue = newListenerQueue();
    queue.queueListeners(Collections.<Object, Object>singletonMap(dispatcher, dispatcher).entrySet(), dispatcher);
    // fire event with the flushedSignal latch
    CountDownLatch flushedSignal = new CountDownLatch(1);
    queue.dispatchEventAsynchronous(0, flushedSignal);
    try {
        // Wait for the flush signal; timeout after 30 seconds
        flushedSignal.await(30, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        // ignore but reset the interrupted flag
        Thread.currentThread().interrupt();
    }
}
Also used : EventDispatcher(org.eclipse.osgi.framework.eventmgr.EventDispatcher) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 2 with EventDispatcher

use of org.eclipse.osgi.framework.eventmgr.EventDispatcher in project rt.equinox.framework by eclipse.

the class EquinoxEventPublisher method publishFrameworkEventPrivileged.

public void publishFrameworkEventPrivileged(FrameworkEvent event, FrameworkListener... callerListeners) {
    if (!isEventManagerSet()) {
        return;
    }
    // Build the listener snapshot
    Map<BundleContextImpl, Set<Map.Entry<FrameworkListener, FrameworkListener>>> listenerSnapshot;
    synchronized (allFrameworkListeners) {
        listenerSnapshot = new HashMap<>(allFrameworkListeners.size());
        for (Map.Entry<BundleContextImpl, CopyOnWriteIdentityMap<FrameworkListener, FrameworkListener>> entry : allFrameworkListeners.entrySet()) {
            CopyOnWriteIdentityMap<FrameworkListener, FrameworkListener> listeners = entry.getValue();
            if (!listeners.isEmpty()) {
                listenerSnapshot.put(entry.getKey(), listeners.entrySet());
            }
        }
    }
    // If framework event hook were defined they would be called here
    // deliver the event to the snapshot
    ListenerQueue<FrameworkListener, FrameworkListener, FrameworkEvent> queue = newListenerQueue();
    // add the listeners specified by the caller first
    if (callerListeners != null && callerListeners.length > 0) {
        Map<FrameworkListener, FrameworkListener> listeners = new HashMap<>();
        for (FrameworkListener listener : callerListeners) {
            if (listener != null)
                listeners.put(listener, listener);
        }
        // We use the system bundle context as the dispatcher
        if (listeners.size() > 0) {
            BundleContextImpl systemContext = (BundleContextImpl) container.getStorage().getModuleContainer().getModule(0).getBundle().getBundleContext();
            @SuppressWarnings({ "rawtypes", "unchecked" }) EventDispatcher<FrameworkListener, FrameworkListener, FrameworkEvent> dispatcher = (EventDispatcher) systemContext;
            queue.queueListeners(listeners.entrySet(), dispatcher);
        }
    }
    for (Map.Entry<BundleContextImpl, Set<Map.Entry<FrameworkListener, FrameworkListener>>> entry : listenerSnapshot.entrySet()) {
        @SuppressWarnings({ "rawtypes", "unchecked" }) EventDispatcher<FrameworkListener, FrameworkListener, FrameworkEvent> dispatcher = (EventDispatcher) entry.getKey();
        Set<Map.Entry<FrameworkListener, FrameworkListener>> listeners = entry.getValue();
        queue.queueListeners(listeners, dispatcher);
    }
    queue.dispatchEventAsynchronous(FRAMEWORKEVENT, event);
    // close down the publisher if we got the stopped event
    if ((event.getType() & FRAMEWORK_STOPPED_MASK) != 0) {
        close();
    }
}
Also used : Set(java.util.Set) FrameworkEvent(org.osgi.framework.FrameworkEvent) HashMap(java.util.HashMap) CopyOnWriteIdentityMap(org.eclipse.osgi.framework.eventmgr.CopyOnWriteIdentityMap) EventDispatcher(org.eclipse.osgi.framework.eventmgr.EventDispatcher) FrameworkListener(org.osgi.framework.FrameworkListener) HashMap(java.util.HashMap) Map(java.util.Map) CopyOnWriteIdentityMap(org.eclipse.osgi.framework.eventmgr.CopyOnWriteIdentityMap)

Aggregations

EventDispatcher (org.eclipse.osgi.framework.eventmgr.EventDispatcher)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Set (java.util.Set)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 CopyOnWriteIdentityMap (org.eclipse.osgi.framework.eventmgr.CopyOnWriteIdentityMap)1 FrameworkEvent (org.osgi.framework.FrameworkEvent)1 FrameworkListener (org.osgi.framework.FrameworkListener)1