use of org.osgi.service.remoteserviceadmin.EndpointDescription in project ecf by eclipse.
the class AbstractRemoteServiceRegisterTest method testRegisterAndUpdateProperties.
public void testRegisterAndUpdateProperties() throws Exception {
remoteServiceAdminEvents.clear();
// register RemoteServiceAdminListener
ServiceRegistration listenerReg = getContext().registerService(RemoteServiceAdminListener.class.getName(), createRemoteServiceAdminListener(), null);
// Create server container
this.server = ContainerFactory.getDefault().createContainer(getServerContainerTypeName(), new Object[] { createServerID() });
// Create props
Properties props = getServiceProperties();
// Put "1" for "testonekey"
props.put("testonekey", "1");
// Actually register with default service (IConcatService) as a remote service
registration = registerDefaultService(props);
ExportReference exportRef = null;
Thread.sleep(15000);
for (RemoteServiceAdminEvent e : remoteServiceAdminEvents) if (e.getType() == RemoteServiceAdminEvent.EXPORT_REGISTRATION)
exportRef = e.getExportReference();
assertTrue(exportRef != null);
// remoteServiceAdminEvents should have the Export registration
assertTrue(remoteServiceAdminEvents.size() > 0);
// Get ExportReference
EndpointDescription oldED = exportRef.getExportedEndpoint();
assertNotNull(oldED);
Map<String, ?> oldEDProperties = oldED.getProperties();
assertNotNull(oldEDProperties);
assertTrue("1".equals(oldEDProperties.get("testonekey")));
assertTrue(oldEDProperties.get("testtwokey") == null);
// Change testonekey value to "two" and set a new property "testtwokey" to "2"
props.put("testonekey", "two");
props.put("testtwokey", "2");
// Set/update the properties on the registration
this.registration.setProperties(props);
// Now get new EndpointDescription and test that new properties have been changed in EndpointDescription
EndpointDescription updatedED = exportRef.getExportedEndpoint();
assertNotNull(updatedED);
Map<String, ?> updatedEDProperties = updatedED.getProperties();
assertNotNull(updatedEDProperties);
assertTrue("two".equals(updatedEDProperties.get("testonekey")));
assertTrue("2".equals(updatedEDProperties.get("testtwokey")));
Thread.sleep(15000);
listenerReg.unregister();
}
use of org.osgi.service.remoteserviceadmin.EndpointDescription 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