-
Notifications
You must be signed in to change notification settings - Fork 6
Services
ECR bundles may export services to be used by other bundles. Usually each service may be linked to one or more an extension points.
Note that exported services are registered as OSGi services.
To declare services, you need to create an XML component where you specify the services the component exports.
XML Components may contain both extension points, extensions and service declarations.
Here is an example on how to declare two services:
<?xml version="1.0"?>
<component name="org.eclipse.ecr.core.schema.TypeService"
version="1.0.0">
<implementation class="org.eclipse.ecr.core.schema.TypeService" />
<service>
<provide
interface="org.eclipse.ecr.core.schema.SchemaManager"/>
<provide
interface="org.eclipse.ecr.core.schema.TypeProvider"/>
</service>
</component>
The TypeService
component above provides two services: org.eclipse.ecr.core.schema.SchemaManager
and org.eclipse.ecr.core.schema.TypeProvider
.
You can provide as many services as you want by adding more provide
elements.
Components that are providing services acts as service factories - so the component implementation must implement a method that to provide / create services. This is the getAdapter
method from the org.eclipse.ecr.runtime.model.Component
interface.
Here is an example of implementing a component that provides services:
public class TypeService extends DefaultComponent {
@Override
public void activate(ComponentContext context) {
typeManager = new SchemaManagerImpl();
typeProvider = new TypeProviderImpl();
}
@Override
public void deactivate(ComponentContext context) {
typeManager.clear();
typeManager = null;
typeProvider = null;
}
...
@Override
public <T> T getAdapter(Class<T> adapter) {
if (SchemaManager.class.isAssignableFrom(adapter)) {
return (T) schemaManager;
} else if (TypeProvider.class.isAssignableFrom(adapter)) {
return (T) typeProvider;
}
return null;
}
...
}
In this example we provide 2 service objects a SchemaManager
and a TypeProvider
service (which are singletons - so they will be instantiated only once when component is activated).
The activate
and deactivate
methods are called by the component registry after the component was registered and before it is unregistered - thus, they are called only once in the life time of a component instance.
You can of course choose to create the service instance each time the getAdapter
method is called.
So the declaration in the XML file is only saying that that component is providing the 2 services. When the service is looked up at runtime - the component getAdapter
method will be invoked to get an instance of the service.
Of course as any other XML component declaration the component file must be referenced in the MANIFEST.MF
as we saw in Components and Extension Points sections.
All ECR services are available through the getService
or getLocalService
methods in the org.eclipse.ecr.runtime.api.Framework
class. The existence of the getLocalService
method is historical; the only difference on the 2 methods is that getService
throws an exception if the service is not found, while getLocalService
returns null
.
Example:
...
SchemaManager schemaMgr = Framework.getService(SchemaManager.class);
...
Note: since services are also registered as OSGi services, you can also look them up using the OSGi service API.
Next: Go to the Configuration section to learn how to configure components and services in ECR.