private void copyPoliciesToComponent()

in modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/CompositeProcessor.java [1100:1205]


    private void copyPoliciesToComponent(Component component, 
                                         Implementation implementation, 
                                         ModelResolver resolver,
                                         boolean clearImplSettings) throws ContributionResolveException {
        if (implementation instanceof PolicySetAttachPoint) {
            // Add implementation policies into component, since implementation instances are 
            // reused and it's likely that this implementation instance will not hold after its resolution.
            // On the first call to this method (clearImplSettings=true), we are moving policies from the
            // implementation XML element up to the component.  In this case if there are mutually exclusive
            // policies we must clear the component policy so that the implementation policy "wins".
            // On the second call to this method (clearImplSettings=false), we are moving policies from the
            // componentType implementation up to the component.  In this case if there are mutually
            // exclusive policies it is an error.  This error will be detected later in the PolicyComputer.
            if (clearImplSettings) {
                for (Intent intent : ((PolicySetAttachPoint)implementation).getRequiredIntents()) {
                    for (Intent excluded : intent.getExcludedIntents()) {
                        if (component.getRequiredIntents().contains(excluded)) {
                            component.getRequiredIntents().remove(excluded);
                        }
                        for (Iterator i = component.getPolicySets().iterator(); i.hasNext(); ) {
                            PolicySet cmpPolicySet = (PolicySet) i.next();
                            if (cmpPolicySet.getProvidedIntents().contains(excluded)) {
                                i.remove();
                            }
                        }
                    }
                }
                for (PolicySet policySet : ((PolicySetAttachPoint)implementation).getPolicySets()) {
                    for (Intent intent : policySet.getProvidedIntents()) {
                        for (Intent excluded : intent.getExcludedIntents()) {
                            if (component.getRequiredIntents().contains(excluded)) {
                                component.getRequiredIntents().remove(excluded);
                            }
                            for (Iterator i = component.getPolicySets().iterator(); i.hasNext(); ) {
                                PolicySet cmpPolicySet = (PolicySet) i.next();
                                if (cmpPolicySet.getProvidedIntents().contains(excluded)) {
                                    i.remove();
                                }
                            }
                        }
                    }
                }
            }
            component.getRequiredIntents().addAll(((PolicySetAttachPoint)implementation).getRequiredIntents());
            component.getPolicySets().addAll(((PolicySetAttachPoint)implementation).getPolicySets());
            component.getApplicablePolicySets().addAll(((PolicySetAttachPoint)implementation).getApplicablePolicySets());
            
            if ( implementation instanceof OperationsConfigurator ) {
                boolean notFound;
                List<ConfiguredOperation> opsFromImplementation = new ArrayList<ConfiguredOperation>();
                List<ConfiguredOperation> implConfOperations = 
                    new ArrayList<ConfiguredOperation>(((OperationsConfigurator)implementation).getConfiguredOperations());
                for ( ConfiguredOperation implConfOp : implConfOperations ) {
                    notFound = true;
                    for ( ConfiguredOperation compConfOp : ((OperationsConfigurator)component).getConfiguredOperations() ) {
                        if ( implConfOp.getName().equals(compConfOp.getName()) ) {
                            notFound = false;

                            if (clearImplSettings) {
                                for (Intent intent : implConfOp.getRequiredIntents()) {
                                    for (Intent excluded : intent.getExcludedIntents()) {
                                        if (compConfOp.getRequiredIntents().contains(excluded)) {
                                            compConfOp.getRequiredIntents().remove(excluded);
                                        }
                                    }
                                }
                                for (PolicySet policySet : implConfOp.getPolicySets()) {
                                    for (Intent intent : policySet.getProvidedIntents()) {
                                        for (Intent excluded : intent.getExcludedIntents()) {
                                            if (compConfOp.getRequiredIntents().contains(excluded)) {
                                                compConfOp.getRequiredIntents().remove(excluded);
                                            }
                                            for (Iterator i = compConfOp.getPolicySets().iterator(); i.hasNext(); ) {
                                                PolicySet cmpPolicySet = (PolicySet) i.next();
                                                if (cmpPolicySet.getProvidedIntents().contains(excluded)) {
                                                    i.remove();
                                                }
                                            }
                                        }
                                    }
                                }
                            }

                            addInheritedIntents(implConfOp.getRequiredIntents(), compConfOp.getRequiredIntents());
                            addInheritedPolicySets(implConfOp.getPolicySets(), compConfOp.getPolicySets());
                            addInheritedPolicySets(implConfOp.getApplicablePolicySets(), compConfOp.getApplicablePolicySets());
                        }
                    }
                    
                    if ( notFound ) {
                        opsFromImplementation.add(implConfOp);
                    }
                    
                    if ( clearImplSettings ) {
                        ((OperationsConfigurator)implementation).getConfiguredOperations().remove(implConfOp);
                    }
                }
                ((OperationsConfigurator)component).getConfiguredOperations().addAll(opsFromImplementation);
            }
            
            if ( clearImplSettings ) {
                ((PolicySetAttachPoint)implementation).getRequiredIntents().clear();
                ((PolicySetAttachPoint)implementation).getPolicySets().clear();
            }
        }
    }