Java Reference
In-Depth Information
}
@Override
public void destroy() {
ctx = null;
}
}
Here you extend
com.google.gwt.user.server.rpc.RemoteServiceServlet
with
your own implementation class. Although the details of
GWT
aren't important for this
example, note that you override the
init()
and
destroy()
methods of
javax.servlet.
GenericServlet
. You grab a reference to the
BundleContext
from an attribute on the
javax.servlet.ServletContext
. Having cached a reference to the
BundleContext
,
you can use it to discover other services in the framework.
EXTENDING THE GWT SAMPLE WEB APPLICATION FOR OSGI
To d e m o n s t r a t e h o w y o u c a n u s e s e r v i c e s i n a
WAB
context, you can make a minor
change to the sample
GWT
application to discover a trivial
StockProvider
service
from the
OSG
i registry using the following interface:
public interface StockProvider {
Map<String, Double> getStocks(String[] symbols);
}
This service returns a
Map
of stock prices for the given symbols. In the
StockPrice-
ServiceImpl.getPrices()
method, you look up a
StockProvider
service and use it
to get stock prices as shown in the next listing.
Listing 15.10 Reading stock prices from the
StockProvider
service
public StockPrice[] getPrices(String[] symbols)
throws DelistedException, ServiceUnavailableException {
StockPrice[] prices = null;
StockProvider provider = (StockProvider) tracker.getService();
if (provider != null) {
prices = readPrices(provider, symbols);
} else {
throw new ServiceUnavailableException();
}
return prices;
}
You see whether a
StockProvider
service is registered using a
ServiceTracker
. If one
is available, you use it to read prices for the specified symbols or throw a checked
exception to indicate that an error message should be displayed to the user.
You've now taken an existing servlet application and deployed it to an
OSG
i envi-
ronment using the
WAB
format. You've also extended this application to discover ser-
vices from the
OSG
i registry. As it stands, this is a trivial example; but you'll see in the
next section how to extend the example further by using the service abstraction to
allow your application to be divided into a multiprocess application. First, we'll briefly
cover one remaining area of interest: how to support standard
WAR
files in
OSG
i.