[ 
https://issues.apache.org/jira/browse/FELIX-3700?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13470642#comment-13470642
 ] 

Pierre De Rop commented on FELIX-3700:
--------------------------------------

I believe I failed to explain my problem clearly ... hope you will give me 
another chance :-)

(I have joined a patch to this issue, it's certainly incomplete, but if you 
could take a look at it)

So, In our applications, we simply have the following components:

1) a component "C", which can be instantiated several times (C1/C2), but all 
instances are using the same configuration, from the same PID "C". For example, 
in the "C" configuration pid, we configure some shared informations, like a 
syslog address, some database addr, etc ...

C is declared like this:

@Component(name="C", factory="CFactory", configuraton-policy=require)"
class C {
   ...
}

2) we also have a "Manager" component which is in charge of instantiating 
C1/C2. We don't instantiate 1001 components. We are actually writing an 
"elastic" http container,  where DS components are dynamically created if the 
load increase. (such components are actually actor components). So, This 
"Manager" component tracks the org.osgi.service.component.ComponentFactory 
instances with filter "(component.factory=CFactory)" in order to instantiate 
C1/C2.

3) finally, we have a "Configurator" component, which loads the configuration 
from a database and registers it into CM (config-admin osgi service), using pid 
"C".

Now the problem is that if the ComponentFactory for C is registered by SCR 
before the configuration for the PID "C" is registered into CM, then  C1/C2 
will be instantiated with an empty configuration. (because the Configuration 
has not yet registered it into CM).

Now,  I could probably rework the application in order to use another strategy, 
but I believe that the component factory for C should not be registered before 
the configuration pid for C is available, because C is declared with 
configuration-policy=require.

Please take a look at the patch
thanks in advance.

                
> DS Factory Components don't support configuration-policy = require
> ------------------------------------------------------------------
>
>                 Key: FELIX-3700
>                 URL: https://issues.apache.org/jira/browse/FELIX-3700
>             Project: Felix
>          Issue Type: Bug
>          Components: Declarative Services (SCR)
>         Environment: linux, jdk1.6
>            Reporter: Pierre De Rop
>
> With scr 1.6.0 and also with scr from trunk, there is the following problem:
> When a factory component (declared with a factory attribute in the Component 
> element), and when the Component is also defined with configuration-policy = 
> require, then the org.osgi.service.component.ComponentFactory associated to 
> the factory component is registered in the OSGi registry even if the 
> configuration for the component is not yet available.
> This is  a problem because when the 
> org.osgi.service.component.ComponentFactory is registered in the registry, 
> then another component using the ComponentFactory may call the newInstance 
> method and then instantiate the component without the required configuration.
> For example, in the following code, Main is injected with the A 
> ComponentFactory and creates immediately one A instance: but at this point, 
> the Configuration for A has not yet been created (see in the Main.start 
> method, where a thread is started in order to create the A configuration 
> after 1 second ...).
> I expect A ComponentFactory to be registered only after the A Configuration 
> is available from config admin, because the A configuration-policy has been 
> set to the "require" value.
> Am I correct or does the spec forbid factory components to use the 
> configuration-policy = require ?
> ->
> @Component(name="A", factory = "A", configurationPolicy = 
> ConfigurationPolicy.require)
> public class A {
>   @Activate
>   void start(Map<?, ?> config) {
>     System.out.println("A.start:" + config.get("foo");
>   }
> }
> @Component
> public class Main {
>   private ConfigurationAdmin _cm;
>   
>   @Reference(type = '*', target = "(component.factory=A)")
>   void bindCF(ComponentFactory cf) { // should only be called once A config 
> admin configuration is avail
>     System.out.println("Main.bindCF");
>     cf.newInstance(null);
>   }
>   
>   @Reference
>   void bindCM(ConfigurationAdmin cm) {
>     _cm = cm;
>   }
>   
>   @Activate
>   void start() {
>     System.out.println("Main.start");
>     
>     new Thread(new Runnable() {
>       public void run() {
>         try {
>           Thread.sleep(1000);
>           System.out.println("Configuring A");          
>           Configuration config = _cm.getConfiguration("A", null);
>           config.update(new Hashtable() {
>             {
>               put("foo", "bar");
>             }
>           });          
>         } catch (Exception ioe) {
>           ioe.printStackTrace();
>         }
>       }
>     }).start();
>   }
> }

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

Reply via email to