org.scijava
Class Context

java.lang.Object
  extended by org.scijava.Context
All Implemented Interfaces:
Disposable

public class Context
extends Object
implements Disposable

Top-level SciJava application context, which initializes and maintains a list of services.

Author:
Curtis Rueden
See Also:
Service

Constructor Summary
Context()
          Creates a new SciJava application context with all available services.
Context(boolean empty)
          Creates a new SciJava application context.
Context(Class... serviceClasses)
          Creates a new SciJava application context with the specified services (and any required service dependencies).
Context(Collection<Class<? extends Service>> serviceClasses)
          Creates a new SciJava application context with the specified services (and any required service dependencies).
Context(Collection<Class<? extends Service>> serviceClasses, PluginIndex pluginIndex)
          Creates a new SciJava application context with the specified services (and any required service dependencies).
Context(PluginIndex pluginIndex)
          Creates a new SciJava application with the specified PluginIndex.
 
Method Summary
 void dispose()
          Performs any needed cleanup of the object's services, in preparation for the object being retired (e.g., to make garbage collection possible).
 PluginIndex getPluginIndex()
           
<S extends Service>
S
getService(Class<S> c)
          Gets the service of the given class, or null if there is no matching service.
 Service getService(String className)
          Gets the service of the given class name (useful for scripts).
 ServiceIndex getServiceIndex()
           
 void inject(Object o)
          Injects the application context into the given object.
<S extends Service>
S
service(Class<S> c)
          Gets the service of the given class.
 Service service(String className)
          Gets the service of the given class name (useful for scripts).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Context

public Context()
Creates a new SciJava application context with all available services.


Context

public Context(boolean empty)
Creates a new SciJava application context.

Parameters:
empty - If true, the context will be empty; otherwise, it will be initialized with all available services.

Context

public Context(Class... serviceClasses)
Creates a new SciJava application context with the specified services (and any required service dependencies).

Developer's note: This constructor's argument is raw (i.e., Class... instead of Class<? extends Service>...) because otherwise, downstream invocations (e.g., new Context(DisplayService.class)) yield the potentially confusing warning:

Type safety: A generic array of Class is created for a varargs parameter

To avoid this, we have opted to use raw types and suppress the relevant warnings here instead.

Parameters:
serviceClasses - A list of types that implement the Service interface (e.g., DisplayService.class).
Throws:
ClassCastException - If any of the given arguments do not implement the Service interface.

Context

public Context(Collection<Class<? extends Service>> serviceClasses)
Creates a new SciJava application context with the specified services (and any required service dependencies).

Parameters:
serviceClasses - A collection of types that implement the Service interface (e.g., DisplayService.class).

Context

public Context(PluginIndex pluginIndex)
Creates a new SciJava application with the specified PluginIndex. This allows a base set of available plugins to be defined, and is useful when plugins that would not be returned by the PluginIndex's PluginFinder are desired.

NB: the PluginIndex.discover() method may still be called, adding additional plugins to this index. The mechanism of discovery should be configured exclusively through the attached PluginFinder.

Parameters:
pluginIndex - The plugin index to use when discovering and indexing plugins. If you wish to completely control how services are discovered (i.e., use your own PluginFinder implementation), then you can pass a custom PluginIndex here.

Context

public Context(Collection<Class<? extends Service>> serviceClasses,
               PluginIndex pluginIndex)
Creates a new SciJava application context with the specified services (and any required service dependencies). Service dependency candidates are selected from those discovered by the given PluginIndex's associated PluginFinder.

NB: Context creation is an important step of a SciJava application's lifecycle. Particularly in environments where more than one implementation exists for various services, careful consideration should be exercised regarding what classes and plugins are provided to the Context, and what needs to occur during the initialization of these services (especially those of lower priority). See ServiceHelper.loadServices() for more information.

Parameters:
serviceClasses - A collection of types that implement the Service interface (e.g., DisplayService.class).
pluginIndex - The plugin index to use when discovering and indexing plugins. If you wish to completely control how services are discovered (i.e., use your own PluginFinder implementation), then you can pass a custom PluginIndex here.
Method Detail

getServiceIndex

public ServiceIndex getServiceIndex()

getPluginIndex

public PluginIndex getPluginIndex()

service

public <S extends Service> S service(Class<S> c)
Gets the service of the given class.

Throws:
NoSuchServiceException - if the context does not have the requested service.

service

public Service service(String className)
Gets the service of the given class name (useful for scripts).

Throws:
IllegalArgumentException - if the class does not exist, or is not a service class.
NoSuchServiceException - if the context does not have the requested service.

getService

public <S extends Service> S getService(Class<S> c)
Gets the service of the given class, or null if there is no matching service.


getService

public Service getService(String className)
Gets the service of the given class name (useful for scripts).


inject

public void inject(Object o)
Injects the application context into the given object. This does three distinct things:

Parameters:
o - The object to which the context should be assigned.
Throws:
IllegalStateException - If the object already has a context.
IllegalArgumentException - If the object has a required Service parameter (see Parameter.required()) which is not available from this context.

dispose

public void dispose()
Description copied from interface: Disposable
Performs any needed cleanup of the object's services, in preparation for the object being retired (e.g., to make garbage collection possible).

Specified by:
dispose in interface Disposable


Copyright © 2009–2014 SciJava. All rights reserved.