|
Re: Using bundles with Wars [message #1063985 is a reply to message #1063886] |
Mon, 17 June 2013 06:57 |
|
You could implement a BundleActivator to get access to a bundle context and get a reference to an OSGi service with it (or better use a ServiceTracker. Don't forget to declare the activator in the manifest file.
public class Activator implements BundleActivator {
private ServiceTracker<IStateServiceClient, IStateServiceClient> stateServiceTracker;
private static Activator instance;
/**
* Returns the shared instance
*
* @return the shared instance
*/
public static Activator getInstance() {
return instance;
}
@Override
public void start(BundleContext context) throws Exception {
instance = this;
stateServiceTracker = new ServiceTracker<IStateServiceClient, IStateServiceClient>(context,
IStateServiceClient.class.getName(), null);
stateServiceTracker.open();
}
@Override
public void stop(BundleContext context) throws Exception {
stateServiceTracker.close();
instance = null;
}
public IStateServiceClient getStateService() {
return stateServiceTracker.getService();
}
}
Manifest addition:
Bundle-Activator: my.package.Activator
In your web classes you can get access to the service like this:
Activator.getInstance().getStateService();
Remember to check for a null return value. If there is no active service registered in the OSGi environment atm then null will be returned.
Hope this helps
Mihael
|
|
|
|
Powered by
FUDForum. Page generated in 0.03724 seconds