Search in sources :

Example 1 with EndpointEventListener

use of org.osgi.service.remoteserviceadmin.EndpointEventListener 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 EndpointEventListener

use of org.osgi.service.remoteserviceadmin.EndpointEventListener in project ecf by eclipse.

the class EndpointDescriptionLocator method getMatchingEndpointEventListenerHolders.

/**
 * @param refs service references
 * @param description description
 * @param type type
 * @return EndpointEventListenerHolder[] matching endpoint event listener holders
 * @since 4.1
 */
public EndpointEventListenerHolder[] getMatchingEndpointEventListenerHolders(ServiceReference[] refs, EndpointDescription description, int type) {
    if (refs == null)
        return null;
    List results = new ArrayList();
    for (int i = 0; i < refs.length; i++) {
        EndpointEventListener listener = (EndpointEventListener) context.getService(refs[i]);
        if (listener == null)
            continue;
        List<String> filters = PropertiesUtil.getStringPlusProperty(getMapFromProperties(refs[i]), EndpointEventListener.ENDPOINT_LISTENER_SCOPE);
        // Only proceed if there is a filter present
        if (filters.size() > 0) {
            String matchingFilter = isMatch(description, filters);
            if (matchingFilter != null)
                results.add(new EndpointEventListenerHolder(listener, description, matchingFilter, type));
        }
    }
    return (EndpointEventListenerHolder[]) results.toArray(new EndpointEventListenerHolder[results.size()]);
}
Also used : EndpointEventListener(org.osgi.service.remoteserviceadmin.EndpointEventListener) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

EndpointEventListener (org.osgi.service.remoteserviceadmin.EndpointEventListener)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Dictionary (java.util.Dictionary)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Properties (java.util.Properties)1 CopyOnWriteIdentityMap (org.eclipse.osgi.framework.eventmgr.CopyOnWriteIdentityMap)1 EventDispatcher (org.eclipse.osgi.framework.eventmgr.EventDispatcher)1 EventManager (org.eclipse.osgi.framework.eventmgr.EventManager)1 ListenerQueue (org.eclipse.osgi.framework.eventmgr.ListenerQueue)1 ServiceReference (org.osgi.framework.ServiceReference)1 EndpointDescription (org.osgi.service.remoteserviceadmin.EndpointDescription)1 EndpointEvent (org.osgi.service.remoteserviceadmin.EndpointEvent)1 EndpointListener (org.osgi.service.remoteserviceadmin.EndpointListener)1 BundleTracker (org.osgi.util.tracker.BundleTracker)1 ServiceTracker (org.osgi.util.tracker.ServiceTracker)1 ServiceTrackerCustomizer (org.osgi.util.tracker.ServiceTrackerCustomizer)1