use of org.osgi.service.remoteserviceadmin.EndpointListener in project ecf by eclipse.
the class EndpointDescriptionLocator method getMatchingEndpointListenerHolders.
public EndpointListenerHolder[] getMatchingEndpointListenerHolders(ServiceReference[] refs, EndpointDescription description) {
if (refs == null)
return null;
List results = new ArrayList();
for (int i = 0; i < refs.length; i++) {
EndpointListener listener = (EndpointListener) context.getService(refs[i]);
if (listener == null)
continue;
List<String> filters = PropertiesUtil.getStringPlusProperty(getMapFromProperties(refs[i]), EndpointListener.ENDPOINT_LISTENER_SCOPE);
if (filters.size() > 0) {
String matchingFilter = isMatch(description, filters);
if (matchingFilter != null)
results.add(new EndpointListenerHolder(listener, description, matchingFilter));
}
}
return (EndpointListenerHolder[]) results.toArray(new EndpointListenerHolder[results.size()]);
}
use of org.osgi.service.remoteserviceadmin.EndpointListener 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);
}
Aggregations