private Composite checkAppliesTo()

in modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/PolicyAppliesToBuilderImpl.java [165:241]


    private Composite checkAppliesTo(Document document, Map<PolicySet, List<PolicySubject>> appliesToSubjects, Composite topComposite, BuilderContext context) throws Exception {
 
    	for ( Component component : topComposite.getComponents() ) {
    		if ( component.getImplementation() instanceof Composite ) {
    			Composite nested = (Composite) component.getImplementation();
    			checkAppliesTo(saveAsDOM(nested),new HashMap<PolicySet, List<PolicySubject>>(), nested, context );
    		}
    	}

    	for (Component component : topComposite.getComponents()) {

    		for (ComponentService componentService : component.getServices()) {
    	
    			for (Endpoint ep : componentService.getEndpoints()) {  
    				for ( PolicySet ps : new ArrayList<PolicySet>(ep.getPolicySets()) ) {
    					// Check if this PolicySet applies to the binding, component service, interface, component, or composite. If not,
    					// remove it from the list of policy sets for this endpoint. 
    					if ( ep.getBinding() instanceof PolicySubject ) {
    						if (isApplicableToSubject(document, appliesToSubjects, topComposite, (PolicySubject)ep.getBinding(), ps))
    							continue;
    					}
    					
    					if (isApplicableToSubject(document, appliesToSubjects, topComposite, componentService, ps))
    						continue;
    					else if ( (componentService.getInterfaceContract() != null ) && (isApplicableToSubject(document, appliesToSubjects, topComposite, componentService.getInterfaceContract().getInterface(), ps)))
    						continue;
    					else if ( isApplicableToSubject(document, appliesToSubjects, topComposite, component, ps))
    						continue;
    					else if ( isApplicableToSubject(document, appliesToSubjects, topComposite, topComposite, ps))
    						continue;
    					else
    						ep.getPolicySets().remove(ps);
    				}
    			}
    		}

    		for (ComponentReference componentReference : component.getReferences()) {    			
    			for (EndpointReference epr : componentReference.getEndpointReferences()) {
    			    // don't process the EPR yet if it's not resolved
                    if (epr.getStatus() == EndpointReference.Status.WIRED_TARGET_NOT_FOUND ||
                        epr.getBinding() == null){ 
                        continue;
                    }
                    
    				for (PolicySet ps : new ArrayList<PolicySet>(epr.getPolicySets()) ) {
                        // Check if this PolicySet applies to the binding, component reference, component, or composite. If not,
                        // remove it from the list of policy sets for this endpoint. 
    					if ( epr.getBinding() instanceof PolicySubject ) {
    						if (isApplicableToSubject(document, appliesToSubjects, topComposite, (PolicySubject)epr.getBinding(), ps))
    							continue;
    					}
    					if (isApplicableToSubject(document, appliesToSubjects, topComposite, componentReference, ps))
    						continue;
    					else if ( (componentReference.getInterfaceContract() != null) && (isApplicableToSubject(document, appliesToSubjects, topComposite, componentReference.getInterfaceContract().getInterface(), ps)))
    						continue;
    					else if ( isApplicableToSubject(document, appliesToSubjects, topComposite, component, ps))
    						continue;
    					else if ( isApplicableToSubject(document, appliesToSubjects, topComposite, topComposite, ps))
    						continue;
    					else
    						epr.getPolicySets().remove(ps);    			
    				}
    			}
    		}

    		Implementation implementation = component.getImplementation();
    		if (implementation != null && 
    				implementation instanceof PolicySubject) {
    			for ( PolicySet ps : new ArrayList<PolicySet>(implementation.getPolicySets())) {    		
    				if (!isApplicableToSubject(document, appliesToSubjects, topComposite, implementation, ps))
    					implementation.getPolicySets().remove(ps);
    			}
    		}
    		
    	}
    	return topComposite;
    }