|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
public interface ServiceManager
The ServiceManager simplifies the management of the OSGi service discovery process.
It is intended that this class is used internally in bundles in the following manner; The BundleActivator instantiates a single ServiceManager in the start() method, and pass it to all parts of the bundle that require any external service.
private ServiceManager m_serviceManager;
public void start( BundleContext bundleContext )
throws Exception
{
m_serviceManager = new ServiceManagerImpl( bundleContext );
SomeWorker worker = new SomeWorker( m_serviceManager );
worker.start();
}
The user of the ServiceManager must remember to NOT cache the returned service for a very long time. It should either
be released after each use, or at a maximum of a few seconds. This will ensure that reload of services are properly
removing the old classes and replacing them with the new ones.
public class SomeWorker
{
private ServiceManager m_serviceManager;
public SomeWorker( ServiceManager serviceManager )
{
m_serviceManager = serviceManager;
}
public void someMethod()
{
SomeService service = (SomeService) m_serviceManager.getService( SomeService.class );
service.doSomething();
}
Well behaving bundle activators should also dispose the ServiceManager in the stop() method.
public void stop( BundleContext bundleContext )
throws Exception
{
m_serviceManager.dispose();
}
NOTE! If the ServiceManager is used during the start() method, there is no point of waiting unless a separate
thread is started for the bundle, since the framework starts bundles in sequential order, and therefor it is very
likely that no services will appear while waiting. It is also bad practice for bundles to occupy the thread for
lengthy operations in the start() and stop() methods, and should create their own threads of execution if this is
necessary, such as await the arrival of other services.
| Field Summary | |
|---|---|
static long |
DEFAULT_SERVICE_TIMEOUT
The default service timeout is 10000 milliseconds. |
| Method Summary | |
|---|---|
void |
dispose()
Dispose all the services cached by this ServiceManager. |
java.lang.Object |
getService(java.lang.Class serviceClass)
Returns service of the specified serviceClass. |
java.lang.Object |
getService(java.lang.Class serviceClass,
java.lang.String search,
boolean waitAWhileIfNotAvailable)
Returns service of the specified serviceClass. |
| Field Detail |
|---|
static final long DEFAULT_SERVICE_TIMEOUT
| Method Detail |
|---|
void dispose()
ServiceManager.
java.lang.Object getService(java.lang.Class serviceClass)
serviceClass. Returns null if the service is not
available at the time of invocation. This method is equivalent to:
serviceManager.getService( Class, String, false );
serviceClass - The service class object to be returned. This argument must not be null.
serviceClass.getService(Class, String, boolean)
java.lang.Object getService(java.lang.Class serviceClass,
java.lang.String search,
boolean waitAWhileIfNotAvailable)
serviceClass. Returns null if the service is not
available.
serviceClass - The service class object to be returned. This argument must not be
null.search - The service class name.waitAWhileIfNotAvailable - A boolean indicator whether this service manager should wait until
time-out if the queried service is not available.
serviceClass.
java.lang.IllegalArgumentException - Thrown if the specified serviceClass is null.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||