Java Reference
In-Depth Information
ServiceRegistration registerService(
String[] clazzes, Object service, Dictionary properties);
ServiceRegistration registerService(
String clazz, Object service, Dictionary properties);
ServiceReference[] getServiceReferences(String clazz, String filter)
throws InvalidSyntaxException;
ServiceReference[] getAllServiceReferences(String clazz, String filter)
throws InvalidSyntaxException;
ServiceReference getServiceReference(String clazz);
Object getService(ServiceReference reference);
boolean ungetService(ServiceReference reference);
...
}
As long as your bundle has a valid context (that is, when it's active), it can use services.
Let's see how easy it is to use a bundle's BundleContext to publish a service.
4.2.1
Publishing a service
Before you can publish a service, you need to describe it so others can find it. In other
words, you need to take details from the implemented contract and record them in
the registry. What details does OSG i need from the contract?
DEFINING A SERVICE
To p u b l i s h a s e r v i c e i n OSG i, you need to provide a single interface name (or an array
of them), the service implementation, and an optional dictionary of metadata (see fig-
ure 4.9). Here's what you can use for a service that provides both stock listings and
stock charts for the London Stock Exchange ( LSE ):
String[] interfaces = new String[] {
StockListing.class.getName(), StockChart.class.getName()};
Dictionary metadata = new Properties();
metadata.setProperty("name", "LSE");
metadata.setProperty("currency", Currency.getInstance("GBP"));
metadata.setProperty("country", "GB");
Class.getName() helps during refactoring. Note that metadata must be in the
Dictionary type and can contain any Java type.
Hi
Lo
Stock
({
}
)
ctx.registerService
4
1/4
2
FOO
NUL
LSE
18
11 1/2
serviceRegistration
id
OSGi
service
registry
Figure 4.9 Publishing a service that provides both stock listings and stock charts
Search WWH ::




Custom Search