Jump to: navigation, search

EIG:RemoteServiceAdminListener

Revision as of 10:04, 12 April 2011 by Pdeuster.gmail.com (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

The RemoteServiceAdminListener can be used to be notified synchronously of any exports or imports performed by the RemoteServiceAdmin.

In order to use it, the org.osgi.service.remoteserviceadmin.RemoteServiceAdminListener interface has to be implemented and registered as a service using the whiteboard pattern. Assuming there is a class MyRemoteServiceAdminListener that implements the interface, the registration looks like the following:

bundleContext.registerService(RemoteServiceAdminListener.class.getName(), new MyRemoteServiceAdminListener(), null);

The listener interface contains only one method:

public void remoteAdminEvent( RemoteServiceAdminEvent event )

The parameter event contains all the information about imported or exported services.

There are different types of events. A detailed list can be found in section 122.10.11 of the OSGi enterprise specification. What type of event occurred can be checked using the getType() method.

Depending on whether a service was imported or exported the ImportReference or ExportReference can be obtained using the corresponding getter methods.

The following is an example implementation that reacts to a new service being imported into the local framework and stopping the export of a service.

public class MyRemoteServiceAdminListener implements RemoteServiceAdminListener {
 
	@Override
	public void remoteAdminEvent(RemoteServiceAdminEvent event) {
 
		// Print the source bundle for the event
		final Bundle source = event.getSource();
		System.out.println("Received RSA event from "
				+ source.getSymbolicName());
 
		// Handle a few example event types
		switch (event.getType()) {
 
		// A new service was imported into the local framework
		case RemoteServiceAdminEvent.IMPORT_REGISTRATION:
 
			final ImportReference importReference = event.getImportReference();
 
			// Retrieve the ServiceReference and the endpoint it points to
			final ServiceReference importedService = importReference
					.getImportedService();
			final EndpointDescription importedEndpoint = importReference
					.getImportedEndpoint();
 
			System.out.println(importedService + " has been imported from "
					+ importedEndpoint.getFrameworkUUID());
 
			break;
 
		// The export of a service has been removed
		case RemoteServiceAdminEvent.EXPORT_UNREGISTRATION:
 
			final ExportReference exportReference = event.getExportReference();
 
			// Retrieve the ServiceReference
			final ServiceReference exportedService = exportReference
					.getExportedService();
 
			System.out.println(exportedService + " is no longer exported");
 
			break;
 
		default:
			break;
		}
 
	}
 
}