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

Tuomas Kiviaho commented on FELIX-4050:
---------------------------------------

Thanks for the tips and quick response,

I've worked around this case already, but in any case here's a formal example 
(which is combination of both use cases) that will break as explained above. 

public class S {

    @Inject
    org.apache.felix.dm.Component _c;

    S2 _s2;

    private Dictionary config; 
    
    @ConfigurationDependency(pid="MyPid")
    void configure(Dictionary config) { this.config = config; }

    @ServiceDependency(name="S#")
    S3 s3;

    @Init
    Map init() {
        _c.add((_c.getDependencyManager().createServiceDependency()
                .setService(S2.class)
                .setRequired(true)
                .setAutoConfig("_s2")
                .setInstanceBound(true));
        return new HashMap() {{
                put("S3.filter", m_config.get("filter"));
                put("S3.required", m_config.get("required"));
        }};
    }

}

Here the a state change calculation will occur at _c.add(...) which will 
transition in turn transition the state prematurely from TRACKING_OPTIONAL to 
BOUND because name dependency because named dependencies are added behind the 
scenes after init() method completes. This only happens when all required 
dependencies are available at the time of state change calculation. 

I could also be mess up similarly the state change calculation of the first use 
case just by separating the one _c.add(...) call to two separate calls.
                
> Named dependencies are not injected if new dependencies are added at init 
> phase.
> --------------------------------------------------------------------------------
>
>                 Key: FELIX-4050
>                 URL: https://issues.apache.org/jira/browse/FELIX-4050
>             Project: Felix
>          Issue Type: Bug
>          Components: Dependency Manager
>    Affects Versions: dependencymanager-3.1.0
>            Reporter: Tuomas Kiviaho
>
> Spec says that "In the Init method, you are yet allowed to add some 
> additional dependencies (but using the API)."
> I guess this means that I am allowed to call Component.add(). This leads to 
> state transition for instance when new service dependency is added since it's 
> started right away because component is already instantiated at this point. 
> Component.dependencyAvailable() is called if service tracker finds a match 
> right away and this in turn starts the state change calculation.
> Problem is that State uses components current instantiated status to 
> determinate whether it is bound or not and not the status what the component 
> was given to the activateService() method. State change calculation 
> transitions to bound state prematurely because component is now instantiated. 
> All required dependencies are available, because component is still unaware 
> of forthcoming named dependencies at this point.
> I suggest that some sort of placeholder dependencies are used which the named 
> dependencies will replace when they are created/configured. This also 
> approach also preserves the order in which dependencies were actually added 
> to the component. In the future there could be a new is/SetActive property to 
> DependencyActivation which could be used to turn on/off an already added 
> dependencies. Then named dependencies could be used as such instead of 
> placeholders and user could even configure them directly.

--
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