From 0b0a543b419baf1679c1ad6b737cb69425bd6abc Mon Sep 17 00:00:00 2001 From: bdaniel Date: Tue, 21 Jun 2011 17:31:10 +0000 Subject: Refactor appliesTo builder based on spec clarifications git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1138105 13f79535-47bb-0310-9956-ffa450edef68 --- .../builder/impl/PolicyAppliesToBuilderImpl.java | 133 ++++++++++++--------- 1 file changed, 79 insertions(+), 54 deletions(-) (limited to 'sca-java-2.x/trunk/modules') diff --git a/sca-java-2.x/trunk/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/PolicyAppliesToBuilderImpl.java b/sca-java-2.x/trunk/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/PolicyAppliesToBuilderImpl.java index 64fcc2bc77..158c27e98b 100644 --- a/sca-java-2.x/trunk/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/PolicyAppliesToBuilderImpl.java +++ b/sca-java-2.x/trunk/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/PolicyAppliesToBuilderImpl.java @@ -27,6 +27,7 @@ import java.util.Map; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpression; +import javax.xml.xpath.XPathExpressionException; import org.apache.tuscany.sca.assembly.Component; import org.apache.tuscany.sca.assembly.ComponentReference; @@ -101,86 +102,110 @@ public class PolicyAppliesToBuilderImpl extends PolicyAttachmentBuilderImpl { for (Component component : topComposite.getComponents()) { for (ComponentService componentService : component.getServices()) { - List policySetsToRemove = checkAppliesToSubject(document, appliesToSubjects, topComposite, componentService, componentService.getPolicySets()); - for (Endpoint ep : componentService.getEndpoints()) { - ep.getPolicySets().removeAll(policySetsToRemove); - if (ep.getBinding() instanceof PolicySubject) { - List bindingPolicySetsToRemove = checkAppliesToSubject(document, appliesToSubjects, topComposite, (PolicySubject)ep.getBinding(), ((PolicySubject)ep.getBinding()).getPolicySets()); - ep.getPolicySets().removeAll(bindingPolicySetsToRemove); + + for (Endpoint ep : componentService.getEndpoints()) { + for ( PolicySet ps : new ArrayList(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()) { - List policySetsToRemove = checkAppliesToSubject(document, appliesToSubjects, topComposite, componentReference, componentReference.getPolicySets()); - for (EndpointReference epr : componentReference.getEndpointReferences()) { - epr.getPolicySets().removeAll(policySetsToRemove); - if (epr.getBinding() instanceof PolicySubject) { - List bindingPolicySetsToRemove = checkAppliesToSubject(document, appliesToSubjects, topComposite, (PolicySubject)epr.getBinding(), ((PolicySubject)epr.getBinding()).getPolicySets()); - epr.getPolicySets().removeAll(bindingPolicySetsToRemove); - } + for (ComponentReference componentReference : component.getReferences()) { + for (EndpointReference epr : componentReference.getEndpointReferences()) { + for (PolicySet ps : new ArrayList(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) { - checkAppliesToSubject(document, appliesToSubjects, topComposite, implementation, implementation.getPolicySets()); + for ( PolicySet ps : new ArrayList(implementation.getPolicySets())) { + if (!isApplicableToSubject(document, appliesToSubjects, topComposite, implementation, ps)) + implementation.getPolicySets().remove(ps); + } } + } - - return topComposite; + return topComposite; } /** - * Checks that all the provided policy sets apply to the provided policy subject + * Checks that the provided policy sets applies to the provided policy subject * * @param document * @param appliesToSubjects * @param policySubject - * @param policySets + * @param policySet * @return - * @throws Exception + * @throws XPathExpressionException */ - private List checkAppliesToSubject(Document document, Map> appliesToSubjects, Composite composite, PolicySubject policySubject, List policySets) throws Exception { - List policySetsToRemove = new ArrayList(); - - for (PolicySet policySet : policySets){ - List subjects = appliesToSubjects.get(policySet); + private boolean isApplicableToSubject(Document document, Map> appliesToSubjects, Composite composite, PolicySubject policySubject, PolicySet policySet) throws XPathExpressionException { + + List subjects = appliesToSubjects.get(policySet); - if (subjects == null){ - XPathExpression appliesTo = policySet.getAppliesToXPathExpression(); - if (appliesTo != null) { - NodeList nodes = (NodeList)appliesTo.evaluate(document, XPathConstants.NODESET); + if (subjects == null){ + XPathExpression appliesTo = policySet.getAppliesToXPathExpression(); + if (appliesTo != null) { + NodeList nodes = (NodeList)appliesTo.evaluate(document, XPathConstants.NODESET); - if (nodes.getLength() > 0){ - subjects = new ArrayList(); - appliesToSubjects.put(policySet, subjects); - } + if (nodes.getLength() > 0){ + subjects = new ArrayList(); + appliesToSubjects.put(policySet, subjects); + } - for (int i = 0; i < nodes.getLength(); i++) { - Node node = nodes.item(i); - String index = getStructuralURI(node); - PolicySubject subject = lookup(composite, index); - if ( subject != null ) - subjects.add(subject); - } - } - } - - if (subjects != null){ - if (!subjects.contains(policySubject)){ - policySetsToRemove.add(policySet); - } - } - - // TODO - If no "appliesTo" is provided does the policy set apply to - // everything or nothing? - + for (int i = 0; i < nodes.getLength(); i++) { + Node node = nodes.item(i); + String index = getStructuralURI(node); + PolicySubject subject = lookup(composite, index); + if ( subject != null ) + subjects.add(subject); + } + } } + + if (subjects != null){ + if (!subjects.contains(policySubject)){ + return false; + } else { + return true; + } + } - policySets.removeAll(policySetsToRemove); - return policySetsToRemove; + return false; + } } -- cgit v1.2.3