You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by pd...@apache.org on 2018/10/09 13:34:26 UTC
svn commit: r1843266 -
/felix/site/trunk/content/documentation/subprojects/apache-felix-dependency-manager/reference/component-resource-adapter.mdtext
Author: pderop
Date: Tue Oct 9 13:34:26 2018
New Revision: 1843266
URL: http://svn.apache.org/viewvc?rev=1843266&view=rev
Log:
dm r12 updates
Modified:
felix/site/trunk/content/documentation/subprojects/apache-felix-dependency-manager/reference/component-resource-adapter.mdtext
Modified: felix/site/trunk/content/documentation/subprojects/apache-felix-dependency-manager/reference/component-resource-adapter.mdtext
URL: http://svn.apache.org/viewvc/felix/site/trunk/content/documentation/subprojects/apache-felix-dependency-manager/reference/component-resource-adapter.mdtext?rev=1843266&r1=1843265&r2=1843266&view=diff
==============================================================================
--- felix/site/trunk/content/documentation/subprojects/apache-felix-dependency-manager/reference/component-resource-adapter.mdtext (original)
+++ felix/site/trunk/content/documentation/subprojects/apache-felix-dependency-manager/reference/component-resource-adapter.mdtext Tue Oct 9 13:34:26 2018
@@ -1,14 +1,28 @@
Title: Dependency Manager - Resource Adapter
-Resource adapters work just like adapters, but instead of working with services, they work with resources. Resources, represented as a URL, are an abstraction introduced to provide a generic way of dealing with "blobs" and can be resources inside a bundle, filesystem or some kind of data store.
+Resource adapters work just like adapters, but instead of working with services, they work with resources.
+Resources, represented as a URL, are an abstraction introduced to provide a generic way of dealing with
+"blobs" and can be resources inside a bundle, filesystem or some kind of data store.
-A resource adapter will be applied to any resource that matches the specified filter condition. For each matching resource an adapter will be created based on the adapter implementation class. The adapter will be registered with the specified interface and existing properties from the original resource plus any extra properties you supply here. It will also inherit all dependencies, and if you declare the original service as a member it will be injected.
+A resource adapter will be applied to any resource that matches the specified filter condition.
+For each matching resource an adapter will be created based on the adapter implementation class.
+The adapter will be registered with the specified interface and existing properties from the original
+resource plus any extra properties you supply here. It will also inherit all dependencies,
+and if you declare the original service as a member it will be injected.
-## Usage Example using API:
+To define a resource adapter, you first need to create a [ResourceComponent](http://felix.staging.apache.org/apidocs/dependencymanager/r12/org/apache/felix/dm/ResourceComponent.html)
+component using the DependencyActivatorBase.createResourceComponent() or the DependencyManager.createResourceComponent() method.
+
+The ResourceComponent extends the [Component](http://felix.staging.apache.org/apidocs/dependencymanager/r12/org/apache/felix/dm/Component.html) and provides some additional
+setter methods related to resource adapters.
+
+
+Usage Example:
Here, the "VideoPlayer" service provides a video service on top of any movie
resources, with service properties "host"/"port"/"protocol"/"path" extracted
-from the resource URL:
+from the resource URL. There will be one VideoPlayerImpl instantiated and started for each
+available URL:
:::java
public class VideoPlayerImpl implements VideoPlayer {
@@ -32,3 +46,69 @@ from the resource URL:
}
Notice that resource adapters are only supported with the DM API, not using annotations or using DM lambda.
+
+Now, here is the resource provider, which provides two URLS:
+
+ :::java
+ public class Activator extends DependencyActivatorBase {
+ @Override
+ public void init(BundleContext ctx, DependencyManager dm) throws Exception {
+ // add resource provider
+ URL[] resourceURLs = new URL[] {
+ new URL("file://localhost/path/to/file1.txt"),
+ new URL("file://localhost/path/to/file2.txt")
+ };
+
+ Component resourceProvider = createComponent()
+ .setImplementation(new ResourceProvider(ctx, resourceURLs))
+ .add(dm.createServiceDependency().setService(ResourceHandler.class).setCallbacks("add", "remove"));
+ dm.add(resourceProvider);
+ }
+ }
+
+ class ResourceProvider {
+ final URL[] m_resources;
+ final BundleContext m_context;
+ final Map<ResourceHandler, Filter> m_handlers = new HashMap<>();
+
+ ResourceProvider(BundleContext ctx, URL ... resources) {
+ m_context = ctx;
+ m_resources = resources;
+ }
+
+ public void add(ServiceReference<?> ref, ResourceHandler handler) {
+ String filterString = (String) ref.getProperty("filter");
+ Filter filter = null;
+ if (filterString != null) {
+ try {
+ filter = m_context.createFilter(filterString);
+ }
+ catch (InvalidSyntaxException e) {
+ return;
+ }
+ }
+ for (int i = 0; i < m_resources.length; i++) {
+ if (filter == null || filter.match((Dictionary<String, ?>) ResourceUtil.createProperties(m_resources[i]))) {
+ synchronized (m_handlers) {
+ m_handlers.put(handler, filter);
+ }
+ handler.added(m_resources[i]);
+ }
+ }
+ }
+
+ public void remove(ServiceReference<?> ref, ResourceHandler handler) {
+ Filter filter;
+ synchronized (m_handlers) {
+ filter = (Filter) m_handlers.remove(handler);
+ }
+ if (filter != null) {
+ for (int i = 0; i < m_resources.length; i++) {
+ if (filter == null || filter.match((Dictionary<String, ?>) ResourceUtil.createProperties(m_resources[i]))) {
+ handler.removed(m_resources[i]);
+ }
+ }
+ }
+ }
+ }
+