public class ServiceHelper extends AbstractContextual
Constructor and Description |
---|
ServiceHelper(Context context)
Creates a new service helper for discovering and instantiating services.
|
ServiceHelper(Context context,
Collection<Class<? extends Service>> serviceClasses)
Creates a new service helper for discovering and instantiating services.
|
ServiceHelper(Context context,
Collection<Class<? extends Service>> serviceClasses,
boolean strict)
Creates a new service helper for discovering and instantiating services.
|
Modifier and Type | Method and Description |
---|---|
<S extends Service> |
createExactService(Class<S> c)
Instantiates a service of the given class, registering it in the index.
|
<S extends Service> |
loadService(Class<S> c)
Obtains a service compatible with the given class, instantiating it (and
registering it in the index) if necessary.
|
void |
loadServices()
Ensures all candidate service classes are registered in the index, locating
and instantiating compatible services as needed.
|
context, getContext
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
setContext
public ServiceHelper(Context context)
context
- The application context for which services should be
instantiated.public ServiceHelper(Context context, Collection<Class<? extends Service>> serviceClasses)
context
- The application context to which services should be added.serviceClasses
- The service classes to instantiate.public ServiceHelper(Context context, Collection<Class<? extends Service>> serviceClasses, boolean strict)
context
- The application context to which services should be added.serviceClasses
- The service classes to instantiate.strict
- Whether service loading will fail fast when there is an error
instantiating a required service.public void loadServices()
This is a NxM operation, where N is the number of service classes attached
to this ServiceHelper, and M is the number of discovered plugins. Multiple
implementations of a given service subclass can be loaded. For example, if
FooService, a subclass of Service
(whether abstract, concrete, or
an interface), appears on the service list, all subclasses of FooService in
the plugin class pool will be loaded.
NB: In typical use, a Context
will only return the highest priority
implementation for a loaded service. To retrieve the lower priority
service(s), they must be requested directly (or through a superclass not
shared with the higher priority implementation). Thus, as these lower
priority services will go unused in many cases, it is critical that service
loading (initialization) is as lightweight as possible.
IllegalArgumentException
- if one of the requested services is
required (i.e., not marked Optional
) but cannot be
filled.public <S extends Service> S loadService(Class<S> c)
IllegalArgumentException
- if no suitable service can be created and
the class is required (i.e., not marked Optional
)Copyright © 2015–2022 SciJava. All rights reserved.