diff options
author | slaws <slaws@13f79535-47bb-0310-9956-ffa450edef68> | 2010-03-18 21:10:52 +0000 |
---|---|---|
committer | slaws <slaws@13f79535-47bb-0310-9956-ffa450edef68> | 2010-03-18 21:10:52 +0000 |
commit | 80b3ade6f34fb14be296bf04dcc878a6e435dec7 (patch) | |
tree | 7c5d785cf247d67d5f9e3c14c793b1f86261bc0f /sca-java-2.x/trunk/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/PolicyAppliesToBuilderImpl.java | |
parent | 4f7979eeab0d91cf03123b79884a66e342fe846e (diff) |
Add a builder to calculate whether policy sets apply to the element to which they are attached.
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@924993 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to '')
-rw-r--r-- | sca-java-2.x/trunk/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/PolicyAppliesToBuilderImpl.java | 171 |
1 files changed, 171 insertions, 0 deletions
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 new file mode 100644 index 0000000000..2417940b1c --- /dev/null +++ b/sca-java-2.x/trunk/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/PolicyAppliesToBuilderImpl.java @@ -0,0 +1,171 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.builder.impl; + + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathExpression; + +import org.apache.tuscany.sca.assembly.Component; +import org.apache.tuscany.sca.assembly.ComponentReference; +import org.apache.tuscany.sca.assembly.ComponentService; +import org.apache.tuscany.sca.assembly.Composite; +import org.apache.tuscany.sca.assembly.Endpoint; +import org.apache.tuscany.sca.assembly.EndpointReference; +import org.apache.tuscany.sca.assembly.Implementation; +import org.apache.tuscany.sca.assembly.builder.BuilderContext; +import org.apache.tuscany.sca.assembly.builder.CompositeBuilderException; + +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.policy.PolicySet; +import org.apache.tuscany.sca.policy.PolicySubject; +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +/** + * A builder that checks that policy sets apply to the elements to which they are attached. + * Any that don't are removed. It first creates a DOM model for the composite so that the xpath + * expression can be evaluated. For each element that holds a policy set is calculates the + * appliesTo nodes and checks that the current element is in the set. If not the policySet is + * removed from the element + * + * @version $Rev$ $Date$ + */ +public class PolicyAppliesToBuilderImpl extends PolicyAttachmentBuilderImpl { + + public PolicyAppliesToBuilderImpl(ExtensionPointRegistry registry) { + super(registry); + } + + public String getID() { + return "org.apache.tuscany.sca.policy.builder.PolicyAppliesToBuilder"; + } + + public Composite build(Composite composite, BuilderContext context) + throws CompositeBuilderException { + try { + // create a DOM for the Domain Composite Infoset + Document document = saveAsDOM(composite); + + // create a cache of evaluated node against each policy set so we don't + // have to keep evaluating policy sets that appear in multiple places + Map<PolicySet, List<PolicySubject>> appliesToSubjects = new HashMap<PolicySet, List<PolicySubject>>(); + + // for all implementations, endpoint and endpoint references check that + // the policy sets validly apply + return checkAppliesTo(document, appliesToSubjects, composite, context); + + } catch (Exception e) { + throw new CompositeBuilderException(e); + } + } + + private Composite checkAppliesTo(Document document, Map<PolicySet, List<PolicySubject>> appliesToSubjects, Composite composite, BuilderContext context) throws Exception { + // look at policies recursively + for (Component component : composite.getComponents()) { + Implementation implementation = component.getImplementation(); + if (implementation instanceof Composite) { + checkAppliesTo(document, appliesToSubjects, (Composite)implementation, context); + } + } + + for (Component component : composite.getComponents()) { + + for (ComponentService componentService : component.getServices()) { + for (Endpoint ep : componentService.getEndpoints()) { + if (ep.getBinding() instanceof PolicySubject) { + checkAppliesToSubject(document, appliesToSubjects, composite, (PolicySubject)ep.getBinding(), ep.getPolicySets()); + } + } + } + + for (ComponentReference componentReference : component.getReferences()) { + for (EndpointReference epr : componentReference.getEndpointReferences()) { + if (epr.getBinding() instanceof PolicySubject) { + checkAppliesToSubject(document, appliesToSubjects, composite, (PolicySubject)epr.getBinding(), epr.getPolicySets()); + } + } + } + + Implementation implementation = component.getImplementation(); + if (implementation != null && + implementation instanceof PolicySubject) { + checkAppliesToSubject(document, appliesToSubjects, composite, implementation, implementation.getPolicySets()); + } + } + + return composite; + } + + /** + * Checks that all the provided policy sets apply to the provided policy subject + * + * @param document + * @param appliesToSubjects + * @param policySubject + * @param policySets + * @return + * @throws Exception + */ + private void checkAppliesToSubject(Document document, Map<PolicySet, List<PolicySubject>> appliesToSubjects, Composite composite, PolicySubject policySubject, List<PolicySet> policySets) throws Exception { + List<PolicySet> policySetsToRemove = new ArrayList<PolicySet>(); + + for (PolicySet policySet : policySets){ + List<PolicySubject> subjects = appliesToSubjects.get(policySet); + + 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<PolicySubject>(); + 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); + 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? + + } + + policySets.removeAll(policySetsToRemove); + } +} |