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:35:59 UTC

svn commit: r1843272 - /felix/site/trunk/content/documentation/subprojects/apache-felix-dependency-manager/reference/dependency-resource.mdtext

Author: pderop
Date: Tue Oct  9 13:35:58 2018
New Revision: 1843272

URL: http://svn.apache.org/viewvc?rev=1843272&view=rev
Log:
dm r12 updates

Modified:
    felix/site/trunk/content/documentation/subprojects/apache-felix-dependency-manager/reference/dependency-resource.mdtext

Modified: felix/site/trunk/content/documentation/subprojects/apache-felix-dependency-manager/reference/dependency-resource.mdtext
URL: http://svn.apache.org/viewvc/felix/site/trunk/content/documentation/subprojects/apache-felix-dependency-manager/reference/dependency-resource.mdtext?rev=1843272&r1=1843271&r2=1843272&view=diff
==============================================================================
--- felix/site/trunk/content/documentation/subprojects/apache-felix-dependency-manager/reference/dependency-resource.mdtext (original)
+++ felix/site/trunk/content/documentation/subprojects/apache-felix-dependency-manager/reference/dependency-resource.mdtext Tue Oct  9 13:35:58 2018
@@ -1,87 +1,92 @@
 Title: Dependency Manager - Resource Dependency
 
-A resource dependency allows you to depend on a resource. A resource is a URL and you can use a filter condition based on protocol, host, port, path and URL.
+A resource dependency allows you to depend on a resource. A resource is a URL and you can use a filter 
+condition based on protocol, host, port, path and URL.
 
-## @ResourceDependency
+To depend on a resource URL, you can use the createResourceDependency method from the [Component interface](http://felix.staging.apache.org/apidocs/dependencymanager/r12/org/apache/felix/dm/Component.html).
 
-Annotates a method of field as a Resource Dependency. A resource dependency allows you to depend on a resource. Resources are an abstraction that is introduced by the dependency manager, represented as a URL. They can be implemented to serve resources embedded in bundles, somewhere on a file system or in an http content repository server, or database.
-A resource is a URL and you can use a filter condition based on protocol, host, port, and path.
+Here is an example where a bundle activation declares a VideoPlayer component which depends on a resource URL.
+The component uses an optional dependency callback, which is invoked each time an URL is registered.
 
-Attributes:
-
-* *added*: Returns the callback method to be invoked when the service is available. This attribute is only meaningful when the annotation is applied on a class field.
-* *changed*: Returns the callback method to be invoked when the service properties have changed.
-* *removed*: Returns the callback method to invoke when the service is lost.
-* *required*: Returns whether the Service dependency is required or not.
-* *filter*: Returns the Service dependency OSGi filter.
-* *propagate*: Specifies if the resource URL properties must be propagated. If set to true, then the URL properties ("protocol"/"host"/"port"/"path") will be propagated to the service properties of the component which is using this dependency.
-* *name*: The name used when dynamically configuring this dependency from the init method. Specifying this attribute allows to dynamically configure the dependency filter and required flag from the Service's init method. All unnamed dependencies will be injected before the init() method; so from the init() method, you can then pick up whatever information needed from already injected (unnamed) dependencies, and configure dynamically your named dependencies, which will then be calculated once the init() method returns. Please refer to [Here]({{ refs.dependencymanager-annotations-lifecycle.path }}).
+    :::java
+    public class Activator extends DependencyActivatorBase {
+        @Override
+        public void init(BundleContext ctx, DependencyManager dm) throws Exception {
+            Component videoPlayer = createComponent()
+            		.setImplementation(VideoPlayer.class)
+            		.add(createResourceDependency().setFilter("(&(path=/path/to/*.txt)(host=localhost))").setCallbacks("play", null));
+            dm.add(videoPlayer);        		  
+        }
+    }
 
-## Usage Examples
+    public class VideoPlayer {
+    	void play(URL url) {
+    		System.out.println("play: " + url);
+    
+    	}
+    }
 
-Here, the "VideoPlayer" component plays any provided MKV video resources
 
+And here is a component which registers some URL resources:
 
     :::java
-    @Component
-    public class VideoPlayer {
-        @ResourceDependency(required=false, filter="(path=/videos/*.mkv)")
-        void playResource(URL video) { ... }
+    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);
+        }
     }
 
-And here is an example of a VideoProvider, which provides some videos using a web URL. Notice that Resource providers need to depend on the DependencyManager API:
-
-    :::java
-    import java.net.MalformedURLException;
-    import java.net.URL;
-    import java.util.HashMap;
-    import java.util.Map;
+    class ResourceProvider {
+    	final URL[] m_resources;
+        final BundleContext m_context;
+        final Map<ResourceHandler, Filter> m_handlers = new HashMap<>();
     
-    import org.apache.felix.dm.ResourceHandler;
-    import org.apache.felix.dm.ResourceUtil;
-    import org.apache.felix.dm.annotation.api.Component;
-    import org.apache.felix.dm.annotation.api.Init;
-    import org.apache.felix.dm.annotation.api.ServiceDependency;
-    import org.osgi.framework.BundleContext;
-    import org.osgi.framework.Filter;
-    import org.osgi.framework.InvalidSyntaxException;
-
-    @Component
-    public class VideoProvider
-    {
-        // Injected by reflection
-        private volatile BundleContext context;
-        // List of known resource handlers
-        private Map<ResourceHandler, Filter> m_handlers = new HashMap<ResourceHandler, Filter>();
-        // List of known video resources
-        private URL[] m_videos;
-
-        @Init
-        void init() throws MalformedURLException
-        {
-           m_videos = new URL[] {
-                   new URL("http://localhost:8080/videos/video1.mkv"),
-                   new URL("http://localhost:8080/videos/video2.mkv"),
-            };
+    	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]);
+                }
+            }
         }
     
-        // Track resource handlers
-        @ServiceDependency(required = false)
-        public void add(Map<String, String> serviceProperties, ResourceHandler handler) throws InvalidSyntaxException
-        {
-            String filterString = serviceProperties.get("filter");
-            filterString = (filterString != null) ? filterString : "(path=*)";
-            Filter filter = context.createFilter(filterString);
-            synchronized (this)
-            {
-                m_handlers.put(handler, filter);
+        public void remove(ServiceReference<?> ref, ResourceHandler handler) {
+            Filter filter;
+            synchronized (m_handlers) {
+                filter = (Filter) m_handlers.remove(handler);
             }
-            for (URL video : m_videos)
-            {
-                if (filter.match(ResourceUtil.createProperties(video)))
-                {
-                    handler.added(video);
+            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]);
+                    }
                 }
             }
         }
-    }