Search in sources :

Example 1 with ListenerQueue

use of org.eclipse.osgi.framework.eventmgr.ListenerQueue in project ecf by eclipse.

the class EndpointDescriptionLocator method start.

public void start() {
    // For service info and endpoint description factories
    // set the service ranking to Integer.MIN_VALUE
    // so that any other registered factories will be preferred
    final Properties properties = new Properties();
    properties.put(Constants.SERVICE_RANKING, new Integer(Integer.MIN_VALUE));
    serviceInfoFactory = new ServiceInfoFactory();
    defaultServiceInfoFactoryRegistration = context.registerService(IServiceInfoFactory.class.getName(), serviceInfoFactory, (Dictionary) properties);
    defaultEndpointDescriptionFactory = new DiscoveredEndpointDescriptionFactory();
    defaultEndpointDescriptionFactoryRegistration = context.registerService(IDiscoveredEndpointDescriptionFactory.class.getName(), defaultEndpointDescriptionFactory, (Dictionary) properties);
    // setup/register default endpointDescriptionReader
    defaultEndpointDescriptionReaderRegistration = context.registerService(IEndpointDescriptionReader.class.getName(), new EndpointDescriptionReader(), (Dictionary) properties);
    // Create thread group, event manager, and eventQueue, and setup to
    // dispatch EndpointListenerEvents
    ThreadGroup eventGroup = new ThreadGroup(// $NON-NLS-1$
    "RSA EndpointDescriptionLocator ThreadGroup");
    eventGroup.setDaemon(true);
    eventManager = new EventManager("RSA EndpointDescriptionLocator Dispatcher", // $NON-NLS-1$
    eventGroup);
    eventQueue = new ListenerQueue(eventManager);
    CopyOnWriteIdentityMap listeners = new CopyOnWriteIdentityMap();
    listeners.put(this, this);
    eventQueue.queueListeners(listeners.entrySet(), new EventDispatcher() {

        public void dispatchEvent(Object eventListener, Object listenerObject, int eventAction, Object eventObject) {
            // $NON-NLS-1$
            final String logMethodName = "dispatchEvent";
            // We now dispatch both EndpointListenerEvents
            if (eventObject instanceof EndpointListenerEvent) {
                final EndpointListenerEvent event = (EndpointListenerEvent) eventObject;
                final EndpointListener endpointListener = event.getEndpointListener();
                final EndpointDescription endpointDescription = event.getEndointDescription();
                final String matchingFilter = event.getMatchingFilter();
                try {
                    boolean discovered = event.isDiscovered();
                    trace("endpointListener.discovered=" + discovered + " ", // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
                    "fwk=" + getFrameworkUUID() + ", endpointListener=" + endpointListener + // $NON-NLS-1$
                    ", endpointDescription=" + endpointDescription + // $NON-NLS-1$
                    ", matchingFilter=" + matchingFilter);
                    if (discovered)
                        endpointListener.endpointAdded(endpointDescription, matchingFilter);
                    else
                        endpointListener.endpointRemoved(endpointDescription, matchingFilter);
                } catch (Exception e) {
                    String message = // $NON-NLS-1$
                    "Exception in EndpointListener listener=" + endpointListener + // $NON-NLS-1$
                    " description=" + endpointDescription + // $NON-NLS-1$
                    " matchingFilter=" + matchingFilter;
                    logError(logMethodName, message, e);
                } catch (LinkageError e) {
                    String message = // $NON-NLS-1$
                    "LinkageError in EndpointListener listener=" + endpointListener + // $NON-NLS-1$
                    " description=" + endpointDescription + // $NON-NLS-1$
                    " matchingFilter=" + matchingFilter;
                    logError(logMethodName, message, e);
                } catch (AssertionError e) {
                    String message = // $NON-NLS-1$
                    "AssertionError in EndpointListener listener=" + endpointListener + // $NON-NLS-1$
                    " description=" + endpointDescription + // $NON-NLS-1$
                    " matchingFilter=" + matchingFilter;
                    logError(logMethodName, message, e);
                }
            // and EndpointEventListenerEvents
            } else if (eventObject instanceof EndpointEventListenerEvent) {
                final EndpointEventListenerEvent event = (EndpointEventListenerEvent) eventObject;
                final EndpointEventListener endpointEventListener = event.getEndpointEventListener();
                final EndpointEvent endpointEvent = event.getEndpointEvent();
                final String matchingFilter = event.getMatchingFilter();
                try {
                    trace("endpointEventListener.discovered=" + getEndpointEventTypeAsString(endpointEvent.getType()) + " ", // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
                    "fwk=" + getFrameworkUUID() + ", endpointEventListener=" + endpointEventListener + // $NON-NLS-1$
                    ", endpointEvent=" + endpointEvent + // $NON-NLS-1$
                    ", matchingFilter=" + matchingFilter);
                    endpointEventListener.endpointChanged(endpointEvent, matchingFilter);
                } catch (Exception e) {
                    String message = // $NON-NLS-1$
                    "Exception in EndpointEventListener listener=" + endpointEventListener + // $NON-NLS-1$
                    " event=" + endpointEvent + // $NON-NLS-1$
                    " matchingFilter=" + matchingFilter;
                    logError(logMethodName, message, e);
                } catch (LinkageError e) {
                    String message = // $NON-NLS-1$
                    "LinkageError in EndpointEventListener listener=" + endpointEventListener + // $NON-NLS-1$
                    " event=" + endpointEvent + // $NON-NLS-1$
                    " matchingFilter=" + matchingFilter;
                    logError(logMethodName, message, e);
                } catch (AssertionError e) {
                    String message = // $NON-NLS-1$
                    "AssertionError in EndpointEventListener listener=" + endpointEventListener + // $NON-NLS-1$
                    " event=" + endpointEvent + // $NON-NLS-1$
                    " matchingFilter=" + matchingFilter;
                    logError(logMethodName, message, e);
                }
            }
        }
    });
    // Register the endpoint listener tracker, so that endpoint listeners
    // that are subsequently added
    // will then be notified of discovered endpoints
    endpointListenerTracker = new ServiceTracker(context, EndpointListener.class.getName(), new ServiceTrackerCustomizer() {

        public Object addingService(ServiceReference reference) {
            if (context == null)
                return null;
            EndpointListener listener = (EndpointListener) context.getService(reference);
            if (listener == null)
                return null;
            Collection<org.osgi.service.remoteserviceadmin.EndpointDescription> allDiscoveredEndpointDescriptions = getEDs();
            for (org.osgi.service.remoteserviceadmin.EndpointDescription ed : allDiscoveredEndpointDescriptions) {
                EndpointDescriptionLocator.EndpointListenerHolder[] endpointListenerHolders = getMatchingEndpointListenerHolders(new ServiceReference[] { reference }, ed);
                if (endpointListenerHolders != null) {
                    for (int i = 0; i < endpointListenerHolders.length; i++) {
                        queueEndpointDescription(endpointListenerHolders[i].getListener(), endpointListenerHolders[i].getDescription(), endpointListenerHolders[i].getMatchingFilter(), true);
                    }
                }
            }
            return listener;
        }

        public void modifiedService(ServiceReference reference, Object service) {
        }

        public void removedService(ServiceReference reference, Object service) {
        }
    });
    endpointListenerTracker.open();
    // Register the endpoint event listener tracker, so that endpoint event
    // listeners
    // that are subsequently added
    // will then be notified of discovered endpoints
    endpointEventListenerTracker = new ServiceTracker(context, EndpointEventListener.class.getName(), new ServiceTrackerCustomizer() {

        public Object addingService(ServiceReference reference) {
            if (context == null)
                return null;
            EndpointEventListener listener = (EndpointEventListener) context.getService(reference);
            if (listener == null)
                return null;
            Collection<org.osgi.service.remoteserviceadmin.EndpointDescription> allDiscoveredEndpointDescriptions = getEDs();
            for (org.osgi.service.remoteserviceadmin.EndpointDescription ed : allDiscoveredEndpointDescriptions) {
                EndpointDescriptionLocator.EndpointEventListenerHolder[] endpointEventListenerHolders = getMatchingEndpointEventListenerHolders(new ServiceReference[] { reference }, ed, EndpointEvent.ADDED);
                if (endpointEventListenerHolders != null) {
                    for (int i = 0; i < endpointEventListenerHolders.length; i++) {
                        queueEndpointDescription(endpointEventListenerHolders[i].getListener(), endpointEventListenerHolders[i].getDescription(), endpointEventListenerHolders[i].getMatchingFilter(), endpointEventListenerHolders[i].getType());
                    }
                }
            }
            return listener;
        }

        public void modifiedService(ServiceReference reference, Object service) {
        }

        public void removedService(ServiceReference reference, Object service) {
        }
    });
    endpointEventListenerTracker.open();
    locatorListeners = new HashMap();
    // Create locator service tracker, so new IDiscoveryLocators can
    // be used to discover endpoint descriptions
    locatorServiceTracker = new ServiceTracker(context, IDiscoveryLocator.class.getName(), new LocatorTrackerCustomizer());
    locatorServiceTracker.open();
    // Create bundle tracker for reading local/xml-file endpoint
    // descriptions
    bundleTrackerCustomizer = new EndpointDescriptionBundleTrackerCustomizer();
    bundleTracker = new BundleTracker(context, Bundle.ACTIVE | Bundle.STARTING, bundleTrackerCustomizer);
    // This may trigger local endpoint description discovery
    bundleTracker.open();
    this.endpointLocatorReg = this.context.registerService(IEndpointDescriptionLocator.class, this, null);
}
Also used : Dictionary(java.util.Dictionary) EndpointListener(org.osgi.service.remoteserviceadmin.EndpointListener) EndpointEventListener(org.osgi.service.remoteserviceadmin.EndpointEventListener) ServiceTracker(org.osgi.util.tracker.ServiceTracker) HashMap(java.util.HashMap) ServiceTrackerCustomizer(org.osgi.util.tracker.ServiceTrackerCustomizer) BundleTracker(org.osgi.util.tracker.BundleTracker) EndpointDescription(org.osgi.service.remoteserviceadmin.EndpointDescription) Properties(java.util.Properties) ListenerQueue(org.eclipse.osgi.framework.eventmgr.ListenerQueue) EndpointDescription(org.osgi.service.remoteserviceadmin.EndpointDescription) EventDispatcher(org.eclipse.osgi.framework.eventmgr.EventDispatcher) EndpointEvent(org.osgi.service.remoteserviceadmin.EndpointEvent) EventManager(org.eclipse.osgi.framework.eventmgr.EventManager) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) ServiceReference(org.osgi.framework.ServiceReference) CopyOnWriteIdentityMap(org.eclipse.osgi.framework.eventmgr.CopyOnWriteIdentityMap)

Example 2 with ListenerQueue

use of org.eclipse.osgi.framework.eventmgr.ListenerQueue in project ecf by eclipse.

the class DistributedEventAdmin method localDispatch.

/**
 * Locally dispatch an Event. This method is used to deliver an
 * {@link Event} to matching {@link EventHandler}s that are registered in
 * the local OSGi service registry.
 *
 * @param dispatchedEvent
 *            the Event to dispatch. Will not be <code>null</code>.
 * @param isAsync
 *            <code>true</code> if the dispatch should be done
 *            asynchronously (non-blocking), <code>false</code> if the
 *            dispatch should be done synchronously.
 */
protected void localDispatch(Event dispatchedEvent, boolean isAsync) {
    EventManager currentManager = eventManager;
    if (currentManager == null) {
        return;
    }
    if (dispatchedEvent == null) {
        log.log(LogService.LOG_ERROR, "Null event passed to EventAdmin was ignored.");
    }
    Event event = notifyPreLocalDispatch(dispatchedEvent);
    if (event != null) {
        String eventTopic = event.getTopic();
        try {
            SecurityManager sm = System.getSecurityManager();
            if (sm != null)
                sm.checkPermission(new TopicPermission(eventTopic, TopicPermission.PUBLISH));
        } catch (SecurityException e) {
            logError("Caller bundle does not have TopicPermission to publish topic " + eventTopic, e);
            throw e;
        }
        Set eventHandlerWrappers = eventHandlerTracker.getHandlers(eventTopic);
        SecurityManager sm = System.getSecurityManager();
        Permission perm = (sm == null) ? null : new TopicPermission(eventTopic, TopicPermission.SUBSCRIBE);
        CopyOnWriteIdentityMap listeners = new CopyOnWriteIdentityMap();
        Iterator iter = eventHandlerWrappers.iterator();
        while (iter.hasNext()) {
            EventHandlerWrapper wrapper = (EventHandlerWrapper) iter.next();
            listeners.put(wrapper, perm);
        }
        ListenerQueue listenerQueue = new ListenerQueue(currentManager);
        listenerQueue.queueListeners(listeners.entrySet(), eventHandlerTracker);
        if (isAsync) {
            listenerQueue.dispatchEventAsynchronous(0, event);
        } else {
            listenerQueue.dispatchEventSynchronous(0, event);
        }
        notifyPostLocalDispatch(event);
    }
}
Also used : CopyOnWriteIdentityMap(org.eclipse.osgi.framework.eventmgr.CopyOnWriteIdentityMap) HashSet(java.util.HashSet) Set(java.util.Set) EventManager(org.eclipse.osgi.framework.eventmgr.EventManager) TopicPermission(org.osgi.service.event.TopicPermission) Permission(java.security.Permission) Iterator(java.util.Iterator) ISharedObjectMessageEvent(org.eclipse.ecf.core.sharedobject.events.ISharedObjectMessageEvent) ISharedObjectCreateResponseEvent(org.eclipse.ecf.core.sharedobject.events.ISharedObjectCreateResponseEvent) Event(org.osgi.service.event.Event) ListenerQueue(org.eclipse.osgi.framework.eventmgr.ListenerQueue) EventHandlerWrapper(org.eclipse.ecf.internal.remoteservice.eventadmin.EventHandlerWrapper) TopicPermission(org.osgi.service.event.TopicPermission)

Aggregations

CopyOnWriteIdentityMap (org.eclipse.osgi.framework.eventmgr.CopyOnWriteIdentityMap)2 EventManager (org.eclipse.osgi.framework.eventmgr.EventManager)2 ListenerQueue (org.eclipse.osgi.framework.eventmgr.ListenerQueue)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 Permission (java.security.Permission)1 Dictionary (java.util.Dictionary)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 Properties (java.util.Properties)1 Set (java.util.Set)1 ISharedObjectCreateResponseEvent (org.eclipse.ecf.core.sharedobject.events.ISharedObjectCreateResponseEvent)1 ISharedObjectMessageEvent (org.eclipse.ecf.core.sharedobject.events.ISharedObjectMessageEvent)1 EventHandlerWrapper (org.eclipse.ecf.internal.remoteservice.eventadmin.EventHandlerWrapper)1 EventDispatcher (org.eclipse.osgi.framework.eventmgr.EventDispatcher)1 ServiceReference (org.osgi.framework.ServiceReference)1 Event (org.osgi.service.event.Event)1 TopicPermission (org.osgi.service.event.TopicPermission)1 EndpointDescription (org.osgi.service.remoteserviceadmin.EndpointDescription)1