A service is typically exported and registered with the OSGi framework using one of the following methods:
void addExportedService(String, Object, Dictionary)
void addExportedServices(String[], Object, Dictionary)
The methods differ in that the first method is used to register the
service under a single service name, whereas the second method allows the
service to be registered under multiple service names. In both cases the
second parameter is the service object to be registered, and the third
parameter is a Dictionary of properties to be registered with
the service. If no properties are to be registered, simply pass
null.
The method addExportedService(String, Object, Dictionary) is
most commonly used, for example:
protected void activate() {
Object service = new HotdogVendor();
Dictionary properties = new Hashtable(11);
properties.put("spicy", new Integer(7)); //$NON-NLS-1$
addExportedService(VendorService.SERVICE_NAME, service, properties);
}
To register a single service under multiple service names, use the method
addExportedServices(String[], Object, Dictionary), for
example:
protected void activate() {
Object service = new Shimmer();
String[] names = new String[] {
DessertToppingService.SERVICE_NAME,
FloorWaxService.SERVICE_NAME
};
Dictionary properties = new Hashtable(11);
properties.put("viscosity", "medium"); //$NON-NLS-1$ //$NON-NLS-2$
addExportedServices(names, service, properties);
}
When registering multiple services
in this way, the same set of properties are used to register the service
under each of the service names. This means that changing the properties
later will affect the service for each of its registered service names.
To register a unique set of properties for each service name, register
the service individually for each service name, for example:
protected void activate() {
Object service = new Shimmer();
// DessertToppingService
Dictionary dessertToppingProperties = new Hashtable(11);
dessertToppingProperties.put("flavor", "banana"); //$NON-NLS-1$ //$NON-NLS-2$
addExportedService(DessertToppingService.SERVICE_NAME, service, dessertToppingProperties);
// FloorWaxService
Dictionary floorWaxProperties = new Hashtable(11);
floorWaxProperties.put("viscosity", "medium"); //$NON-NLS-1$ //$NON-NLS-2$
addExportedService(FloorWaxService.SERVICE_NAME, service, floorWaxProperties);
}
Copyright © 2001, 2007 IBM Corporation and others. All Rights Reserved.