Java Reference
In-Depth Information
where 1 is the ID of the log service bundle, as reported by the bundles command. You
should see an exception when the log client next calls the service:
Exception in thread "LogService Tester"
java.lang.IllegalStateException: LogService has been deactivated
Your log service proxy has detected that the underlying logger is no longer available
and has thrown an IllegalStateException back to the client. In an ideal world, this
would make the client take action and clean up after itself. If it doesn't, the only leak
is the service proxy. But what does the service proxy look like? The following listing
shows the sample implementation.
Listing 8.7 Delegating service proxy
Proxy.newProxyInstance(
LogService.class.getClassLoader(),
new Class[] { LogService.class },
new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
LogService cachedService = (LogService) loggerMap.get(bundle);
if (cachedService != null) {
return method.invoke(cachedService, args);
}
throw new IllegalStateException("LogService has been deactivated");
}
});
You use JDK reflection to create the proxy, because this approach is less error-prone
than creating a new implementation by hand and delegating each method individually.
The proxy is defined in the same space as the LogService class and provides the same
API . Active logger instances are tracked with an internal shared map. You use reflection
to delegate method calls to active loggers and throw exceptions for inactive loggers.
You could manually create delegating service proxies up front, but doing so would
only make sense for small numbers of services. For large systems, you want a generic
service that accepts a set of interfaces at execution time and returns the appropriate
delegating service proxy. Note also that some OSG i component frameworks, which
we'll discuss in chapters 11 and 12, will automatically create delegating service proxies
for you. There's some overhead involved in both memory and performance, so you
may only want to consider using a delegating service proxy only when you don't trust
client bundles to do the right thing or your service uses so many resources that even a
single leak could be dangerous.
8.5
Summary
We started this chapter with a practical guide to debugging OSG i applications using
the console debugger (jdb) and an advanced IDE (Eclipse). We then moved on to
specific issues you may encounter while working with OSG i, including seven class-
loading problems:
 
Search WWH ::




Custom Search