felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pde...@apache.org
Subject svn commit: r1659369 - in /felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples: ./ src/org/apache/felix/dependencymanager/samples/conf/ src/org/apache/felix/dependencymanager/samples/dictionary/annot/ src/org/apach...
Date Thu, 12 Feb 2015 19:14:26 GMT
Author: pderop
Date: Thu Feb 12 19:14:26 2015
New Revision: 1659369

URL: http://svn.apache.org/r1659369
Log:
Added two samples:
org/apache/felix/dependencymanager/samples/dynamicdep/api -> an example of a component
which defines a dependency
dynamically from its init method.

org/apache/felix/dependencymanager/samples/dynamicdep/annot -> the same example, but using
annotations. In this example,
the dependency with name="device" is configured dynamically from the init method (see the
map returned by the init method).

Added:
    felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/dynamicdep.annot.bnd
    felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/dynamicdep.api.bnd
    felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/
    felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/annot/
    felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/annot/DynamicDependency.java
    felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/annot/DynamicDependencyConfigurationAnnot.java
    felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/annot/FileStorage.java
    felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/annot/MapDBStorage.java
    felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/annot/README
    felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/annot/Storage.java
    felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/api/
    felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/api/Activator.java
    felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/api/DynamicDependency.java
    felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/api/DynamicDependencyConfigurationAPI.java
    felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/api/FileStorage.java
    felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/api/MapDBStorage.java
    felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/api/README
    felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/api/Storage.java
Modified:
    felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/bnd.bnd
    felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/conf/Configurator.java
    felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dictionary/annot/SpellChecker.java
    felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dictionary/api/SpellChecker.java

Modified: felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/bnd.bnd
URL: http://svn.apache.org/viewvc/felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/bnd.bnd?rev=1659369&r1=1659368&r2=1659369&view=diff
==============================================================================
--- felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/bnd.bnd
(original)
+++ felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/bnd.bnd
Thu Feb 12 19:14:26 2015
@@ -18,17 +18,17 @@ Bundle-Version: 4.0.0
 	org.apache.felix.gogo.shell;version=0.10.0,\
 	org.apache.felix.dependencymanager.shell;version=latest,\
 	org.apache.felix.dependencymanager.runtime;version=latest,\
-	org.apache.felix.dependencymanager.samples.hello.api;version=latest,\
-	org.apache.felix.dependencymanager.samples.tpool;version=latest,\
-	org.apache.felix.dependencymanager.samples.conf;version=latest,\
 	org.apache.felix.configadmin;version=1.8.0,\
-	org.apache.felix.dependencymanager.samples.device.api;version=latest,\
 	org.apache.felix.eventadmin;version=1.4.3,\
 	biz.aQute.bndlib;version=2.3.0,\
 	org.apache.felix.webconsole;version=4.2.2,\
 	org.apache.felix.http.api;version=2.3.0,\
 	org.apache.felix.http.servlet-api;version=1.0.0,\
-	org.apache.felix.http.jetty;version=2.3.0
+	org.apache.felix.http.jetty;version=2.3.0,\
+	org.apache.felix.dependencymanager.samples.hello.api;version=latest,\
+	org.apache.felix.dependencymanager.samples.tpool;version=latest,\
+	org.apache.felix.dependencymanager.samples.conf;version=latest,\
+	org.apache.felix.dependencymanager.samples.device.api;version=latest
 -runproperties:  \
 	org.apache.felix.dependencymanager.parallel='!org.apache.felix.dependencymanager.samples.tpool,
*',\
 	org.apache.felix.dependencymanager.runtime.log=warn,\

Added: felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/dynamicdep.annot.bnd
URL: http://svn.apache.org/viewvc/felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/dynamicdep.annot.bnd?rev=1659369&view=auto
==============================================================================
--- felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/dynamicdep.annot.bnd
(added)
+++ felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/dynamicdep.annot.bnd
Thu Feb 12 19:14:26 2015
@@ -0,0 +1,2 @@
+Private-Package:  \
+	org.apache.felix.dependencymanager.samples.dynamicdep.annot
\ No newline at end of file

Added: felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/dynamicdep.api.bnd
URL: http://svn.apache.org/viewvc/felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/dynamicdep.api.bnd?rev=1659369&view=auto
==============================================================================
--- felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/dynamicdep.api.bnd
(added)
+++ felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/dynamicdep.api.bnd
Thu Feb 12 19:14:26 2015
@@ -0,0 +1,3 @@
+Bundle-Activator: org.apache.felix.dependencymanager.samples.dynamicdep.api.Activator
+Private-Package:  \
+	org.apache.felix.dependencymanager.samples.dynamicdep.api

Modified: felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/conf/Configurator.java
URL: http://svn.apache.org/viewvc/felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/conf/Configurator.java?rev=1659369&r1=1659368&r2=1659369&view=diff
==============================================================================
--- felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/conf/Configurator.java
(original)
+++ felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/conf/Configurator.java
Thu Feb 12 19:14:26 2015
@@ -44,8 +44,7 @@ public class Configurator {
             m_serviceConsumerAnnotConf = m_ca.getConfiguration("org.apache.felix.dependencymanager.samples.compositefactory.CompositionManager",
null);
             props = new Hashtable<>();
             props.put("key", "value");
-            m_serviceConsumerAnnotConf.update(props);
-
+            m_serviceConsumerAnnotConf.update(props);            
         }
         catch (IOException e) {
             e.printStackTrace();

Modified: felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dictionary/annot/SpellChecker.java
URL: http://svn.apache.org/viewvc/felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dictionary/annot/SpellChecker.java?rev=1659369&r1=1659368&r2=1659369&view=diff
==============================================================================
--- felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dictionary/annot/SpellChecker.java
(original)
+++ felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dictionary/annot/SpellChecker.java
Thu Feb 12 19:14:26 2015
@@ -45,7 +45,7 @@ public class SpellChecker {
      * We'll use the OSGi log service for logging. If no log service is available, then we'll
use a NullObject.
      */
     @ServiceDependency(required = false)
-    private LogService m_log;
+    private volatile LogService m_log;
 
     /**
      * We'll store all Dictionaries in a concurrent list, in order to avoid method synchronization.

Modified: felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dictionary/api/SpellChecker.java
URL: http://svn.apache.org/viewvc/felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dictionary/api/SpellChecker.java?rev=1659369&r1=1659368&r2=1659369&view=diff
==============================================================================
--- felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dictionary/api/SpellChecker.java
(original)
+++ felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dictionary/api/SpellChecker.java
Thu Feb 12 19:14:26 2015
@@ -35,7 +35,7 @@ public class SpellChecker {
     /**
      * We'll use the OSGi log service for logging. If no log service is available, then we'll
use a NullObject.
      */
-    private LogService m_log;
+    private volatile LogService m_log;
 
     /**
      * We'll store all Dictionaries in a concurrent list, in order to avoid method synchronization.

Added: felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/annot/DynamicDependency.java
URL: http://svn.apache.org/viewvc/felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/annot/DynamicDependency.java?rev=1659369&view=auto
==============================================================================
--- felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/annot/DynamicDependency.java
(added)
+++ felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/annot/DynamicDependency.java
Thu Feb 12 19:14:26 2015
@@ -0,0 +1,75 @@
+package org.apache.felix.dependencymanager.samples.dynamicdep.annot;
+
+import java.util.Dictionary;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.felix.dm.annotation.api.Component;
+import org.apache.felix.dm.annotation.api.ConfigurationDependency;
+import org.apache.felix.dm.annotation.api.Init;
+import org.apache.felix.dm.annotation.api.ServiceDependency;
+import org.apache.felix.dm.annotation.api.Start;
+import org.osgi.service.cm.ConfigurationException;
+import org.osgi.service.event.EventAdmin;
+import org.osgi.service.log.LogService;
+
+import aQute.bnd.annotation.metatype.Configurable;
+
+/**
+ * This Component depends on the following services declared from the Activator:
+ * - LogService
+ * - Configuration with PID="org.apache.felix.dependencymanager.samples.dynamicdep.api.DynamicDependencyConfiguration"
+ * 
+ * We the define a dynamic dependency on a Storage Service from our init method and we configure
the dependency filter and
+ * required from using the injected configuration in our updated method.
+ */
+@Component
+public class DynamicDependency {
+	@ServiceDependency
+	volatile EventAdmin eventAdmin;
+	
+	@ServiceDependency
+	volatile LogService log; 
+		
+	@ServiceDependency(name="storage")
+	volatile Storage storage; // dependency defined dynamically from our init() method
+
+	private String storageType; // type of Storage to depend on (we get that from configadmin)
+	private boolean storageRequired; // is our Storage dependency required or not (we get that
from configadmin)
+
+	/**
+	 * This is the first callback: we are injected with our configuration.
+	 */
+	@ConfigurationDependency(pid="org.apache.felix.dependencymanager.samples.dynamicdep.annot.DynamicDependencyConfigurationAnnot")
+	public void updated(Dictionary<String, Object> properties) throws ConfigurationException
{
+        // We use the bnd "Configurable" helper in order to get an implementation for our
DictionaryConfiguration interface.
+		DynamicDependencyConfigurationAnnot cnf = Configurable.createConfigurable(DynamicDependencyConfigurationAnnot.class,
properties);
+		storageType = cnf.storageType();
+		storageRequired = cnf.storageRequired();
+	}
+
+	/**
+	 * The configuration has been injected and also other required dependencies defined from
the Activator.
+	 * Now, define some dynamic dependencies (here we use the configuration injected from our
updated method in 
+	 * order to configure the filter and required flag for the "Storage" dependency).
+	 */
+	@Init
+	Map<String, String> init() {
+		log.log(LogService.LOG_WARNING, "init: storage type=" + storageType + ", storageRequired="
+ storageRequired);
+		Map<String, String> props = new HashMap<>();
+		props.put("storage.required", Boolean.toString(storageRequired));
+		props.put("storage.filter", "(type=" + storageType + ")");
+		return props;		
+	}
+	
+	/**
+	 * All dependencies injected, including dynamic dependencies defined from init method.
+	 */
+	@Start
+	void start() {
+		log.log(LogService.LOG_WARNING, "start");
+		// Use storage to load/store some key-value pairs ...
+		storage.store("gabu", "zo");
+	}
+	
+}

Added: felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/annot/DynamicDependencyConfigurationAnnot.java
URL: http://svn.apache.org/viewvc/felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/annot/DynamicDependencyConfigurationAnnot.java?rev=1659369&view=auto
==============================================================================
--- felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/annot/DynamicDependencyConfigurationAnnot.java
(added)
+++ felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/annot/DynamicDependencyConfigurationAnnot.java
Thu Feb 12 19:14:26 2015
@@ -0,0 +1,22 @@
+package org.apache.felix.dependencymanager.samples.dynamicdep.annot;
+
+import aQute.bnd.annotation.metatype.Meta.AD;
+import aQute.bnd.annotation.metatype.Meta.OCD;
+
+/**
+ * This interface describes the configuration for our DynamicDependencyComponent component.
We are using the bnd metatype
+ * annotations, allowing to configure our component  from web console.
+ * 
+ * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
+ */
+@OCD(description = "Declare here the configuration for the DynamicDependency component.")
+public interface DynamicDependencyConfigurationAnnot {
+    @AD(description = "Enter the storage type to use", 
+    		deflt = "mapdb", 
+    		optionLabels= {"Map DB Storage implementation", "File Storage implementation"},
+    		optionValues={"mapdb", "file"})
+    String storageType();
+
+    @AD(description = "Specifies here is the storage dependency is required or not (if false,
a null object will be used)", deflt = "true")
+    boolean storageRequired();
+}

Added: felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/annot/FileStorage.java
URL: http://svn.apache.org/viewvc/felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/annot/FileStorage.java?rev=1659369&view=auto
==============================================================================
--- felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/annot/FileStorage.java
(added)
+++ felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/annot/FileStorage.java
Thu Feb 12 19:14:26 2015
@@ -0,0 +1,26 @@
+package org.apache.felix.dependencymanager.samples.dynamicdep.annot;
+
+import java.io.Serializable;
+
+import org.apache.felix.dm.annotation.api.Component;
+import org.apache.felix.dm.annotation.api.Property;
+import org.apache.felix.dm.annotation.api.ServiceDependency;
+import org.osgi.service.log.LogService;
+
+@Component(properties={@Property(name="type", value="file")})
+public class FileStorage implements Storage {
+	@ServiceDependency
+	volatile LogService log; // injected
+
+	@Override
+	public void store(String key, Serializable data) {
+		log.log(LogService.LOG_WARNING, "FileStorage.store(" + key + "," + data + ")");
+	}
+
+	@Override
+	public Serializable get(String key) {
+		// TODO Auto-generated method stub
+		return null;
+	}
+
+}

Added: felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/annot/MapDBStorage.java
URL: http://svn.apache.org/viewvc/felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/annot/MapDBStorage.java?rev=1659369&view=auto
==============================================================================
--- felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/annot/MapDBStorage.java
(added)
+++ felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/annot/MapDBStorage.java
Thu Feb 12 19:14:26 2015
@@ -0,0 +1,25 @@
+package org.apache.felix.dependencymanager.samples.dynamicdep.annot;
+
+import java.io.Serializable;
+
+import org.apache.felix.dm.annotation.api.Component;
+import org.apache.felix.dm.annotation.api.Property;
+import org.apache.felix.dm.annotation.api.ServiceDependency;
+import org.osgi.service.log.LogService;
+
+@Component(properties={@Property(name="type", value="mapdb")})
+public class MapDBStorage implements Storage {
+	@ServiceDependency
+	volatile LogService log; // injected
+
+	@Override
+	public void store(String key, Serializable data) {
+		log.log(LogService.LOG_WARNING, "MapDBStorage.store(" + key + "," + data + ")");
+	}
+
+	@Override
+	public Serializable get(String key) {
+		// TODO Auto-generated method stub
+		return null;
+	}
+}

Added: felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/annot/README
URL: http://svn.apache.org/viewvc/felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/annot/README?rev=1659369&view=auto
==============================================================================
--- felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/annot/README
(added)
+++ felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/annot/README
Thu Feb 12 19:14:26 2015
@@ -0,0 +1,16 @@
+This sample shows how to define a dynamic dependency using annotations.
+
+When you declare a Component:
+
+- the configuration (if any) is first injected (updated callback).
+- then all required dependencies are injected, except "named" dependencies whose required
flag and filter can be configured
+dynamically from the init method.
+- then the init method (annotated with @Init) is invoked; And from there you are then able
return a Map that will be used
+to configure the required flag and the filter of all named dependencies.
+- then the start callback (annotated with @Start) is invoked when all required dependencies
are injected, including named 
+dependencies that have been configured from the init method.
+
+In this sample, the "DynamicDependency" Components configures in its "init" method the dependency
having a "storage" name. 
+the dependency "required" flag and filter string are loaded from a Configuration PID 
+("org.apache.felix.dependencymanager.samples.dynamicdep.annot.DynamicDependencyConfigurationAnnot"),
which is defined using 
+Bnd MetaType Annotations.
\ No newline at end of file

Added: felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/annot/Storage.java
URL: http://svn.apache.org/viewvc/felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/annot/Storage.java?rev=1659369&view=auto
==============================================================================
--- felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/annot/Storage.java
(added)
+++ felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/annot/Storage.java
Thu Feb 12 19:14:26 2015
@@ -0,0 +1,8 @@
+package org.apache.felix.dependencymanager.samples.dynamicdep.annot;
+
+import java.io.Serializable;
+
+public interface Storage {
+	Serializable get(String key);
+	void store(String key, Serializable data);
+}

Added: felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/api/Activator.java
URL: http://svn.apache.org/viewvc/felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/api/Activator.java?rev=1659369&view=auto
==============================================================================
--- felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/api/Activator.java
(added)
+++ felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/api/Activator.java
Thu Feb 12 19:14:26 2015
@@ -0,0 +1,34 @@
+package org.apache.felix.dependencymanager.samples.dynamicdep.api;
+
+import java.util.Properties;
+
+import org.apache.felix.dm.DependencyActivatorBase;
+import org.apache.felix.dm.DependencyManager;
+import org.osgi.framework.BundleContext;
+import org.osgi.service.event.EventAdmin;
+import org.osgi.service.log.LogService;
+
+public class Activator extends DependencyActivatorBase {
+
+	@Override
+	public void init(BundleContext bc, DependencyManager dm)throws Exception {
+		Properties props = new Properties();
+		props.put("type", "mapdb");
+		dm.add(createComponent()
+				.setImplementation(MapDBStorage.class).setInterface(Storage.class.getName(), props)
+				.add(createServiceDependency().setService(LogService.class).setRequired(true)));
+		
+		props = new Properties();
+		props.put("type", "file");
+		dm.add(createComponent()
+				.setImplementation(FileStorage.class).setInterface(Storage.class.getName(), props)
+				.add(createServiceDependency().setService(LogService.class).setRequired(true)));
+
+		dm.add(createComponent()
+				.setImplementation(DynamicDependency.class)
+				.add(createServiceDependency().setService(LogService.class).setRequired(true))
+				.add(createConfigurationDependency().setPid(DynamicDependencyConfigurationAPI.class.getName()))
+				.add(createServiceDependency().setService(EventAdmin.class).setRequired(true)));
+	}
+
+}

Added: felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/api/DynamicDependency.java
URL: http://svn.apache.org/viewvc/felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/api/DynamicDependency.java?rev=1659369&view=auto
==============================================================================
--- felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/api/DynamicDependency.java
(added)
+++ felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/api/DynamicDependency.java
Thu Feb 12 19:14:26 2015
@@ -0,0 +1,60 @@
+package org.apache.felix.dependencymanager.samples.dynamicdep.api;
+
+import java.util.Dictionary;
+
+import org.apache.felix.dm.Component;
+import org.apache.felix.dm.DependencyManager;
+import org.osgi.service.cm.ConfigurationException;
+import org.osgi.service.event.EventAdmin;
+import org.osgi.service.log.LogService;
+
+import aQute.bnd.annotation.metatype.Configurable;
+
+/**
+ * This Component depends on the following services declared from the Activator:
+ * - LogService
+ * - Configuration with PID="org.apache.felix.dependencymanager.samples.dynamicdep.api.DynamicDependencyConfiguration"
+ * 
+ * We the define a dynamic dependency on a Storage Service from our init method and we configure
the dependency filter and
+ * required from using the injected configuration in our updated method.
+ */
+public class DynamicDependency {
+
+	volatile EventAdmin eventAdmin; // injected and defined in Activator
+	volatile LogService log; // injected and defined in Activator
+	volatile Storage storage; // dependency defined dynamically from our init() method
+	private String storageType; // type of Storage to depend on (we get that from configadmin)
+	private boolean storageRequired; // is our Storage dependency required or not (we get that
from configadmin)
+
+	/**
+	 * This is the first callback: we are injected with our configuration.
+	 */
+	public void updated(Dictionary<String, Object> properties) throws ConfigurationException
{
+        // We use the bnd "Configurable" helper in order to get an implementation for our
DictionaryConfiguration interface.
+		DynamicDependencyConfigurationAPI cnf = Configurable.createConfigurable(DynamicDependencyConfigurationAPI.class,
properties);
+		storageType = cnf.storageType();
+		storageRequired = cnf.storageRequired();
+	}
+
+	/**
+	 * The configuration has been injected and also other required dependencies defined from
the Activator.
+	 * Now, define some dynamic dependencies (here we use the configuration injected from our
updated method in 
+	 * order to configure the filter and required flag for the "Storage" dependency).
+	 */
+	public void init(Component c) {
+		log.log(LogService.LOG_WARNING, "init: storage type=" + storageType + ", storageRequired="
+ storageRequired);
+		DependencyManager dm = c.getDependencyManager();
+		// all dynamic dependencies must be declared atomically in the Component.add(...) method,
which accepts varargs.
+		c.add(dm.createServiceDependency().setService(Storage.class, "(type=" + storageType + ")").setRequired(storageRequired));
+	}
+
+	/**
+	 * All dependencies injected, including dynamic dependencies defined from init method.
+	 */
+	void start() {
+		log.log(LogService.LOG_WARNING, "start");
+		// Use storage to load/store some key-value pairs ...
+		storage.store("gabu", "zo");
+	}
+	
+}

Added: felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/api/DynamicDependencyConfigurationAPI.java
URL: http://svn.apache.org/viewvc/felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/api/DynamicDependencyConfigurationAPI.java?rev=1659369&view=auto
==============================================================================
--- felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/api/DynamicDependencyConfigurationAPI.java
(added)
+++ felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/api/DynamicDependencyConfigurationAPI.java
Thu Feb 12 19:14:26 2015
@@ -0,0 +1,22 @@
+package org.apache.felix.dependencymanager.samples.dynamicdep.api;
+
+import aQute.bnd.annotation.metatype.Meta.AD;
+import aQute.bnd.annotation.metatype.Meta.OCD;
+
+/**
+ * This interface describes the configuration for our DynamicDependencyComponent component.
We are using the bnd metatype
+ * annotations, allowing to configure our component  from web console.
+ * 
+ * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
+ */
+@OCD(description = "Declare here the configuration for the DynamicDependency component.")
+public interface DynamicDependencyConfigurationAPI {
+    @AD(description = "Enter the storage type to use", 
+    		deflt = "mapdb", 
+    		optionLabels= {"Map DB Storage implementation", "File Storage implementation"},
+    		optionValues={"mapdb", "file"})
+    String storageType();
+
+    @AD(description = "Specifies here is the storage dependency is required or not (if false,
a null object will be used)", deflt = "true")
+    boolean storageRequired();
+}

Added: felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/api/FileStorage.java
URL: http://svn.apache.org/viewvc/felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/api/FileStorage.java?rev=1659369&view=auto
==============================================================================
--- felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/api/FileStorage.java
(added)
+++ felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/api/FileStorage.java
Thu Feb 12 19:14:26 2015
@@ -0,0 +1,21 @@
+package org.apache.felix.dependencymanager.samples.dynamicdep.api;
+
+import java.io.Serializable;
+
+import org.osgi.service.log.LogService;
+
+public class FileStorage implements Storage {
+	volatile LogService log; // injected
+
+	@Override
+	public void store(String key, Serializable data) {
+		log.log(LogService.LOG_WARNING, "FileStorage.store(" + key + "," + data + ")");
+	}
+
+	@Override
+	public Serializable get(String key) {
+		// TODO Auto-generated method stub
+		return null;
+	}
+
+}

Added: felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/api/MapDBStorage.java
URL: http://svn.apache.org/viewvc/felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/api/MapDBStorage.java?rev=1659369&view=auto
==============================================================================
--- felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/api/MapDBStorage.java
(added)
+++ felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/api/MapDBStorage.java
Thu Feb 12 19:14:26 2015
@@ -0,0 +1,20 @@
+package org.apache.felix.dependencymanager.samples.dynamicdep.api;
+
+import java.io.Serializable;
+
+import org.osgi.service.log.LogService;
+
+public class MapDBStorage implements Storage {
+	volatile LogService log; // injected
+
+	@Override
+	public void store(String key, Serializable data) {
+		log.log(LogService.LOG_WARNING, "MapDBStorage.store(" + key + "," + data + ")");
+	}
+
+	@Override
+	public Serializable get(String key) {
+		// TODO Auto-generated method stub
+		return null;
+	}
+}

Added: felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/api/README
URL: http://svn.apache.org/viewvc/felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/api/README?rev=1659369&view=auto
==============================================================================
--- felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/api/README
(added)
+++ felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/api/README
Thu Feb 12 19:14:26 2015
@@ -0,0 +1,13 @@
+This sample shows how to define a dynamic dependency from a Component's init method.
+
+When you declare a Component:
+
+- the configuration (if any) is first injected (updated callback).
+- then all required dependencies are injected.
+- then the init(Component c) method is invoked; And from there you are then able to add dynamic
dependencies using any previously
+injected services (either configuration injected in update method, or other injected services
declared from the Activator).
+- then the start callback is invoked when all required dependencies declared from the init
method are injected.
+
+In this sample, the "DynamicDependency" Components defines in its "init" method a dynamic
dependency on a Storage service. 
+But it first loads the "storage type" and "storage required" dependency informations from
a Configuration PID 
+("org.apache.felix.dependencymanager.samples.dynamicdep.api.DynamicDependencyConfigurationAPI"),
which is defined using Bnd MetaType Annotations.
\ No newline at end of file

Added: felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/api/Storage.java
URL: http://svn.apache.org/viewvc/felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/api/Storage.java?rev=1659369&view=auto
==============================================================================
--- felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/api/Storage.java
(added)
+++ felix/sandbox/pderop/dependencymanager-samples/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/dynamicdep/api/Storage.java
Thu Feb 12 19:14:26 2015
@@ -0,0 +1,8 @@
+package org.apache.felix.dependencymanager.samples.dynamicdep.api;
+
+import java.io.Serializable;
+
+public interface Storage {
+	Serializable get(String key);
+	void store(String key, Serializable data);
+}



Mime
View raw message