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/01 21:06:15 UTC

svn commit: r1842565 - /felix/site/trunk/content/documentation/subprojects/apache-felix-dependency-manager/reference/component-factory-configuration-adapter.mdtext

Author: pderop
Date: Mon Oct  1 21:06:15 2018
New Revision: 1842565

URL: http://svn.apache.org/viewvc?rev=1842565&view=rev
Log:
[FELIX-5939] - DM annotations enhancements

Modified:
    felix/site/trunk/content/documentation/subprojects/apache-felix-dependency-manager/reference/component-factory-configuration-adapter.mdtext

Modified: felix/site/trunk/content/documentation/subprojects/apache-felix-dependency-manager/reference/component-factory-configuration-adapter.mdtext
URL: http://svn.apache.org/viewvc/felix/site/trunk/content/documentation/subprojects/apache-felix-dependency-manager/reference/component-factory-configuration-adapter.mdtext?rev=1842565&r1=1842564&r2=1842565&view=diff
==============================================================================
--- felix/site/trunk/content/documentation/subprojects/apache-felix-dependency-manager/reference/component-factory-configuration-adapter.mdtext (original)
+++ felix/site/trunk/content/documentation/subprojects/apache-felix-dependency-manager/reference/component-factory-configuration-adapter.mdtext Mon Oct  1 21:06:15 2018
@@ -2,190 +2,58 @@ Title: Dependency Manager - Factory Conf
 
 A factory configuration adapter service creates an adapter for each matching configuration in Configuration Admin. For each new factory configuration matching the factoryPid, an adapter will be created based on the adapter implementation class. The adapter will be registered with the specified interface and with the specified adapter service properties. Depending on the propagate parameter, every public factory configuration properties (which don't start with ".") will be propagated along with the adapter service properties. It will also inherit all dependencies.
 
-### Usage Examples
+## Example using DM API
 
-    :::java
-    manager.createFactoryConfigurationAdapterService("MyFactoryPid", "update", true)
-           .setInterface(MyService.class.getName(), new Hashtable() {{ put("foo", "bar"); }})
-           .setImplementation(MyServiceImpl.class);         
-
-    class MyServiceImpl implements MyService {
-        void update(Dictionary cnf) {
-           String ip = (String) cnf.get("address");
-           int port = Integer.valueOf(cnf.get("port");
-        }
-        ...
-    }
-
-You an also use "`configuration types`" ([see configuration dependency documentation)](dependency-configuration.html)
-This is the same example as above, using a "MyConfig" configuration type:
+Here is a sample showing a "MyComponent" component, which can be instantiated multiple times using a factory configuration:
 
     :::java
-    manager.createFactoryConfigurationAdapterService("MyFactoryPid", "update", true, MyConfig.class)
-           .setInterface(MyService.class.getName(), new Hashtable() {{ put("foo", "bar"); }})
-           .setImplementation(MyServiceImpl.class);         
-
     public interface MyConfig {
-        String getAddress();
         int getPort();
+        String getAddress();
     }
-
-    class MyServiceImpl implements MyService {
-        void update(Dictionary cnf) {
-           String ip = cnf.getAddress();
-           int port = cnf.getPort();
+ 
+    public class MyComponent implements MyService {
+        void updated(MyConfig cnf) {
+            int port = cnf.getPort();
+            String addr = cnf.getAddress();
+            ...
         }
-        ...
     }
 
-## @FactoryConfigurationAdapterService
-
-Annotates a class that acts as a Factory Configuration Adapter Service. 
-Like with @ConfigurationDependency, you can optionally specify a configuration type and (optioanlly) use bnd metatype annotations
-in order to specify configuration meta data (heading/descriptions/default values, etc ...)
-
-### Annotation attributes:
-
-----
-**`configType`**    
-*Required*: False    
-*Default*: --
-The interface to use as the configuration type, which will be injected instead of the actual configuration dictionary.
-
-----
-**`provides`**    
-*Required*: False    
-*Default*: all directly implemented interfaces.
-The interface(s) to use when registering adapters. By default, directly implemented interfaces will be registered in the OSGi registry. 
-
-----
-**`properties`**    
-*Required*: False    
-*Default*: --
-
-Adapter Service properties. Notice that public factory configuration is also 
-registered in service properties, (only if propagate is true). 
-Public factory configuration properties are those which don't starts with a 
-dot (".").
-
-----
-**`factoryPid`**    
-*Required*: False    
-*Default*: The class name, including the package.
-
-Returns the factory pid whose configurations will instantiate the 
-annotated service class. 
-(By default, the pid is the service class name). 
-
-----
-**`factoryClass`**    
-*Required*: False    
-*Default*: The class name, including the package.
-
-Returns the factory pid from a class name. The full class name will be used as the 
-configuration PID. You can use this method when you use an interface annoted with 
-standard bndtols metatype annotations. (see http://bnd.bndtools.org/chapters/210-metatype.html).
-
-----
-**`updated`**    
-*Required*: False    
-*Default*: "updated"
-
-The Update method to invoke (defaulting to "updated"), when a factory 
-configuration is created or updated 
-
-----
-**`propagate`**    
-*Required*: False    
-*Default*: false
-
-Returns true if the configuration properties must be published 
-along with the service. Any additional service properties specified directly 
-are merged with these. 
-
-----
-**`factoryMethod`**    
-*Required*: False    
-*Default*: --
-
-Sets the static method used to create the adapter instance.
-
-### Usage Examples
-
-Here, a "DictionaryService" service instance is instantiated for each existing 
-factory configuration instance matching the "sample.DictionaryServiceFactory" factory pid:
-
-    :::java
-    @FactoryConfigurationAdapterService(factoryPid="sample.DictionaryServiceFactory")
-    public class DictionaryImpl implements DictionaryService
-    {
-        /**
-          * The key of our config admin dictionary language.
-          */
-        final static String LANG = "lang";
-             
-        /**
-          * The key of our config admin dictionary values.
-          */
-        final static String WORDS = "words";
-             
-        /**
-          * We store all configured words in a thread-safe data structure, because ConfigAdmin
-          * may invoke our updated method at any time.
-          */
-        private CopyOnWriteArrayList<String> m_words = new CopyOnWriteArrayList<String>();
-             
-        /**
-          * Our Dictionary language.
-          */
-        private String m_lang;
-         
-        protected void updated(Dictionary<String, ?> config) {
-            m_lang = (String) config.get(LANG);
-            m_words.clear();
-            String[] words = (String[]) config.get(WORDS);
-            for (String word : words) {
-                m_words.add(word);
-            }
-         }   
-             // ...
+    public class Activator extends DependencyActivatorBase {
+        &Override
+        public void init(BundleContext context, DependencyManager dm) throws Exception {
+            Component factoryComponent = createFactoryComponent()
+                .setFactoryPid("my.factory.pid")
+                .setInterface(MySevice.class.getName(), null)
+                .setImplementation(MyComponent.class)
+                .setConfigType(MyConfig.class);
+            dm.add(factoryComponent);
+        }
     }
-
-Here is the same example as above, but using a configuration type as well as meta types (the DM annotations metatype attributes are deprecated and 
-it's better to use standard bnd metatype annotations):
-
-First, we declare our factory configuration metadata using standard bndtools metatype annotations (see http://bnd.bndtools.org/chapters/210-metatype.html):
+ 
+## Example using DM Lambda API
 
     :::java
-    package sample;
-    import java.util.List;
-    import aQute.bnd.annotation.metatype.Meta.AD;
-    import aQute.bnd.annotation.metatype.Meta.OCD;
-
-    @OCD(factory = true, description = "Declare here some Dictionary instances.")
-    public interface DictionaryConfiguration {
-       @AD(description = "Describes the dictionary language.", deflt = "en")
-       String lang();
-
-       @AD(description = "Declare here the list of words supported by this dictionary.")
-       List words();
+    public class Activator extends DependencyManagerActivator {
+        public void init(BundleContext ctx, DependencyManager dm) throws Exception { 
+            factoryPidAdapter(adapter -> adapter
+                .factoryPid("my.factory.pid")
+                .provides(MySevice.class)
+                .impl(DictionaryImpl.class)
+                .update(MyConfig.class, MyComponent::updated)                
+       }
     }
-     
-And here is the DictionaryService:
 
-    :::java
-    import java.util.List;
-    import aQute.bnd.annotation.metatype.Configurable;
+## Example using DM Annotations
 
-    @FactoryConfigurationAdapterService(configType=DictionaryConfiguration.class)  
-    public class DictionaryImpl implements DictionaryService {
-        protected void updated(DictionaryConfiguration config) {   
-            m_lang = config.lang();
-            m_words.clear();
-            for (String word : conf.words()) {
-                m_words.add(word);
-            }
+    :::java
+    @Component(factoryPid = "my.factory.pid")
+    public class MyComponent implements MyService {
+        void updated(MyConfig cnf) {
+            int port = cnf.getPort();
+            String addr = cnf.getAddress();
+            ...
         }
-        ...
     }