In the following example, the "SCR" Component allows to track all bundles containing a specific "Service-Component" OSGi header, in order to load and manage all Declarative Service components specified in the SCR xml documents referenced by the header:
- @Component
+ @Component
public class SCR {
- @BundleDependency(required = false,
- removed = "unloadServiceComponents",
- filter = "(Service-Component=*)"
- stateMask = Bundle.ACTIVE)
+ @BundleDependency(required = false,
+ removed = "unloadServiceComponents",
+ filter = "(Service-Component=*)"
+ stateMask = Bundle.ACTIVE)
void loadServiceComponents(Bundle b) {
- String descriptorPaths = (String) b.getHeaders().get("Service-Component");
- // load all service component specified in the XML descriptorPaths files ...
+ String descriptorPaths = (String) b.getHeaders().get("Service-Component");
+ // load all service component specified in the XML descriptorPaths files ...
}
void unloadServiceComponents(Bundle b) {
- // unload all service component we loaded from our "loadServiceComponents" method.
+ // unload all service component we loaded from our "loadServiceComponents" method.
}
}
@@ -247,64 +246,64 @@ A resource is a URL and you can use a fi
Usage Examples
Here, the "VideoPlayer" component plays any provided MKV video resources
- @Component
+ @Component
public class VideoPlayer {
- @ResourceDependency(required=false, filter="(path=/videos/*.mkv)")
- void playResource(URL video) { ... }
+ @ResourceDependency(required=false, filter="(path=/videos/*.mkv)")
+ void playResource(URL video) { ... }
}
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:
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.util.HashMap;
- import java.util.Map;
-
- 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;
+ import java.net.MalformedURLException;
+ import java.net.URL;
+ import java.util.HashMap;
+ import java.util.Map;
+
+ 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
+ @Component
public class VideoProvider
{
- // Injected by reflection
+ // 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;
+ // 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
+ @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"),
+ m_videos = new URL[] {
+ new URL("http://localhost:8080/videos/video1.mkv"),
+ new URL("http://localhost:8080/videos/video2.mkv"),
};
}
- // Track resource handlers
- @ServiceDependency(required = false)
+ // 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);
+ String filterString = serviceProperties.get("filter");
+ filterString = (filterString != null) ? filterString : "(path=*)";
+ Filter filter = context.createFilter(filterString);
synchronized (this)
{
- m_handlers.put(handler, filter);
+ m_handlers.put(handler, filter);
}
for (URL video : m_videos)
{
- if (filter.match(ResourceUtil.createProperties(video)))
+ if (filter.match(ResourceUtil.createProperties(video)))
{
- handler.added(video);
+ handler.added(video);
}
}
}
Modified: websites/staging/felix/trunk/content/documentation/subprojects/apache-felix-dependency-manager/apache-felix-dependency-manager-using-annotations/apache-felix-dependency-manager-using-annotations-lifecycle.html
==============================================================================
--- websites/staging/felix/trunk/content/documentation/subprojects/apache-felix-dependency-manager/apache-felix-dependency-manager-using-annotations/apache-felix-dependency-manager-using-annotations-lifecycle.html (original)
+++ websites/staging/felix/trunk/content/documentation/subprojects/apache-felix-dependency-manager/apache-felix-dependency-manager-using-annotations/apache-felix-dependency-manager-using-annotations-lifecycle.html Fri Jun 14 14:11:44 2013
@@ -49,8 +49,7 @@
asf
security
sponsorship
-sponsors
-
+sponsors