Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 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.policy.xml;
 
 import static javax.xml.stream.XMLStreamConstants.END_ELEMENT;
 import static javax.xml.stream.XMLStreamConstants.START_ELEMENT;
 import static org.apache.tuscany.sca.policy.xml.PolicyConstants.SCA11_NS;
 
 import java.util.List;
 
 
Processor for handling XML models of PolicySet definitions

Version:
$Rev: 992280 $ $Date: 2010-09-03 15:54:04 +0300 (Fri, 03 Sep 2010) $
 
     PolicyConstants {
 
     private PolicyFactory policyFactory;
     private XPathHelper xpathHelper;
 
     // private XPathFactory xpathFactory;
 
     public PolicySetProcessor(ExtensionPointRegistry registryStAXArtifactProcessor<ObjectextensionProcessor) {
         FactoryExtensionPoint modelFactories = registry.getExtensionPoint(FactoryExtensionPoint.class);
         this. = modelFactories.getFactory(PolicyFactory.class);
         this. = extensionProcessor;
         this. = XPathHelper.getInstance(registry);
     }

    
Report a exception.

Parameters:
problems
message
model
 
     private void error(Monitor monitorString messageObject modelException ex) {
         if (monitor != null) {
             Problem problem =
                 monitor.createProblem(this.getClass().getName(),
                                       .,
                                       .,
                                       model,
                                       message,
                                       ex);
             monitor.problem(problem);
         }
    }

    
Report a error.

Parameters:
problems
message
model
    private void error(Monitor monitorString messageObject modelObject... messageParameters) {
        if (monitor != null) {
            Problem problem =
                monitor.createProblem(this.getClass().getName(),
                                      .,
                                      .,
                                      model,
                                      message,
                                      (Object[])messageParameters);
            monitor.problem(problem);
        }
    }
    public PolicySet read(XMLStreamReader readerProcessorContext contextthrows ContributionReadException,
        XMLStreamException {
        PolicySet policySet = null;
        Monitor monitor = context.getMonitor();
        String policySetName = reader.getAttributeValue(null);
        String appliesTo = reader.getAttributeValue(null);
        if (policySetName == null || appliesTo == null) {
            if (policySetName == null)
                error(monitor"PolicySetNameMissing"reader);
            if (appliesTo == null)
                error(monitor"PolicySetAppliesToMissing"reader);
            return policySet;
        }
        policySet = .createPolicySet();
        policySet.setName(new QName(policySetName));
        //TODO: with 1.0 version of specs the applies to xpath is given related to the immediate
        //parent whereas the runtime evaluates the xpath aginst the composite element.  What the runtime
        //is doing is what the future version of the specs could be tending towards.  When that happens
        //this 'if' must be deleted
        if (appliesTo != null && !appliesTo.startsWith("/")) {
            appliesTo = "//" + appliesTo;
        }
        policySet.setAppliesTo(appliesTo);
        if (appliesTo != null) {
            try {
                XPath path = .newXPath();
                NamespaceContext nsContext = .getNamespaceContext(appliesToreader.getNamespaceContext());
                // path.setXPathFunctionResolver(new PolicyXPathFunctionResolver(context));
                XPathExpression expression = .compile(pathnsContextappliesTo);
                policySet.setAppliesToXPathExpression(expression);
            } catch (XPathExpressionException e) {
                ContributionReadException ce = new ContributionReadException(e);
                error(monitor"ContributionReadException"policySetce);
                //throw ce;
            }
        }
        String attachTo = reader.getAttributeValue(null);
        if (attachTo != null) {
            try {
                XPath path = .newXPath();
                NamespaceContext nsContext = .getNamespaceContext(attachToreader.getNamespaceContext());
                path.setXPathFunctionResolver(new PolicyXPathFunctionResolver(nsContext));                
                                           
                attachTo = PolicyXPathFunction.normalize(attachTo,getSCAPrefix(nsContext));
                XPathExpression expression = .compile(pathnsContextattachTo);
                policySet.setAttachTo(attachTo);
                policySet.setAttachToXPathExpression(expression);
            } catch (XPathExpressionException e) {
                ContributionReadException ce = new ContributionReadException(e);
                error(monitor"ContributionReadException"policySetce);
                //throw ce;
            }
        }
        readProvidedIntents(policySetreader);
        int event = reader.getEventType();
        QName name = null;
        reader.next();
        while (reader.hasNext()) {
            event = reader.getEventType();
            switch (event) {
                case : {
                    name = reader.getName();
                    if (.equals(name)) {
                        Intent mappedIntent = .createIntent();
                        String provides = reader.getAttributeValue(null);
                        if (provides != null) {
                            mappedIntent.setName(getQName(reader));
                            if (policySet.getProvidedIntents().contains(mappedIntent)) {
                                readIntentMap(readerpolicySetmappedIntentcontext);
                            } else {
                                error(monitor"IntentNotSpecified"policySetpolicySetName);
                            }
                        } else {
                            error(monitor"IntentMapProvidesMissing"readerpolicySetName);
                        }
                    } else if (.equals(name)) {
                        PolicySet referredPolicySet = .createPolicySet();
                        String referencename = reader.getAttributeValue(null);
                        if (referencename != null) {
                            referredPolicySet.setName(getQName(reader));
                            policySet.getReferencedPolicySets().add(referredPolicySet);
                        } else {
                            error(monitor"PolicySetReferenceNameMissing"readerpolicySetName);
                        }
                    } /*else if ( WS_POLICY_QNAME.equals(name) )  {
                        OMElement policyElement = loadElement(reader);
                        org.apache.neethi.Policy wsPolicy = PolicyEngine.getPolicy(policyElement);
                        policySet.getPolicies().add(wsPolicy);
                      } */else {
                        Object extension = .read(readercontext);
                        if (extension != null) {
                            PolicyExpression exp = .createPolicyExpression();
                            exp.setName(name);
                            exp.setPolicy(extension);
                            // check that all the policies in the policy set are 
                            // expressed in the same language. Compare against the 
                            // first expression we added
                            if ((policySet.getPolicies().size() > 0) && (!policySet.getPolicies().get(0).getName()
                                .equals(name))) {
                                error(monitor"PolicyLanguageMissmatch"readerpolicySet.getName(), policySet
                                    .getPolicies().get(0).getName(), name);
                            } else {
                                policySet.getPolicies().add(exp);
                            }
                        }
                    }
                    break;
                }
            }
            if (event == ) {
                if (.equals(reader.getName())) {
                    break;
                }
            }
            //Read the next element
            if (reader.hasNext()) {
                reader.next();
            }
        }
        return policySet;
    }
    private String getSCAPrefix(NamespaceContext nsContext) {
        Iterator<Stringiter = nsContext.getPrefixes();
        while (iter.hasNext()) {
            String prefix = iter.next();
            if (!prefix.equals(.)) {
                return prefix;
            }
        }
        return "__sca";
    }
	public void readIntentMap(XMLStreamReader readerPolicySet policySetIntent mappedIntentProcessorContext context)
        throws ContributionReadException {
        Monitor monitor = context.getMonitor();
        QName name = reader.getName();
        if (.equals(name)) {
            IntentMap intentMap = .createIntentMap();
            QName intentName = getQName(reader);
            intentMap.setProvidedIntent(mappedIntent);
            if (!policySet.getIntentMaps().contains(intentMap)) {
                policySet.getIntentMaps().add(intentMap);
            } else {
                Monitor.error(context.getMonitor(), this."IntentMapIsNotUnique"policySet
                    .getName().toString(), mappedIntent.getName().getLocalPart());
            }
            String qualifierName = null;
            String qualfiedIntentName = null;
            Intent qualifiedIntent = null;
            Qualifier qualifier = null;
            int event = reader.getEventType();
            try {
                reader.next();
                while (reader.hasNext()) {
                    event = reader.getEventType();
                    switch (event) {
                        case : {
                            name = reader.getName();
                            if (.equals(name)) {
                                qualifierName = getString(reader);
                                if (qualifierName != null) {
                                    qualfiedIntentName =
                                        mappedIntent.getName().getLocalPart() +  + qualifierName;
                                    qualifiedIntent = .createIntent();
                                    qualifiedIntent.setName(new QName(mappedIntent.getName().getNamespaceURI(),
                                                                      qualfiedIntentName));
                                    qualifier = .createQualifier();
                                    qualifier.setIntent(qualifiedIntent);
                                    intentMap.getQualifiers().add(qualifier);
                                } else {
                                    error(monitor"QualifierNameMissing"readerpolicySet.getName());
                                }
                            } else if (.equals(name)) {
                                QName providedIntent = getQName(reader);
                                if (qualifierName.equals(providedIntent.getLocalPart())) {
                                    readIntentMap(readerpolicySetqualifiedIntentcontext);
                                } else {
                                    error(monitor,
                                          "IntentMapDoesNotMatch",
                                          providedIntent,
                                          providedIntent,
                                          qualifierName,
                                          policySet);
                                    //throw new ContributionReadException("Intent provided by IntentMap " + 
                                    //providedIntent + " does not match parent qualifier " + qualifierName +
                                    //" in policyset - " + policySet);
                                }
                            } else {
                                Object extension = .read(readercontext);
                                if (extension != null && qualifier != null) {
                                    PolicyExpression exp = .createPolicyExpression();
                                    exp.setName(name);
                                    exp.setPolicy(extension);
                                    qualifier.getPolicies().add(exp);
                                }
                            }
                            break;
                        }
                    }
                    if (event ==  && .equals(reader.getName())) {
                        break;
                    }
                    //Read the next element
                    if (reader.hasNext()) {
                        reader.next();
                    }
                }
            } catch (XMLStreamException e) {
                ContributionReadException ce = new ContributionReadException(e);
                error(monitor"ContributionReadException"readerce);
                throw ce;
            }
        }
    }
    public void write(PolicySet policySetXMLStreamWriter writerProcessorContext context)
        // Write an <sca:policySet>
        writer.writeStartElement();
        writer.writeNamespace(policySet.getName().getPrefix(), policySet.getName().getNamespaceURI());
        writer.writeAttribute(policySet.getName().getPrefix() +  + policySet.getName().getLocalPart());
        if (policySet.getAppliesTo() != null){
            writer.writeAttribute(policySet.getAppliesTo());
        }
        
        if (policySet.getAttachTo() != null){
            writer.writeAttribute(policySet.getAttachTo());
        }
        writeProvidedIntents(policySetwriter);
        writer.writeEndElement();
    }
    private void readProvidedIntents(PolicySet policySetXMLStreamReader reader) {
        String value = reader.getAttributeValue(null);
        if (value != null) {
            List<IntentprovidedIntents = policySet.getProvidedIntents();
            for (StringTokenizer tokens = new StringTokenizer(value); tokens.hasMoreTokens();) {
                QName qname = getQNameValue(readertokens.nextToken());
                Intent intent = .createIntent();
                intent.setName(qname);
                providedIntents.add(intent);
            }
        }
    }
    private void writeProvidedIntents(PolicySet policySetXMLStreamWriter writerthrows XMLStreamException {
        if (!policySet.getProvidedIntents().isEmpty()) {
            StringBuffer sb = new StringBuffer();
            for (Intent providedIntents : policySet.getProvidedIntents()) {
                sb.append(getQualifiedName(providedIntents.getName(), writer));
                sb.append(" ");
            }
            // Remove the last space
            sb.deleteCharAt(sb.length() - 1);
            writer.writeAttribute(.sb.toString());
        }
    }
    private String getQualifiedName(QName nameXMLStreamWriter writerthrows XMLStreamException {
        String local = name.getLocalPart();
        String prefix = writer.getPrefix(name.getNamespaceURI());
        if (prefix != null && prefix.length() > 0) {
            return prefix + ':' + local;
        } else {
            return local;
        }
    }
    private void resolvePolicies(PolicySet policySetModelResolver resolverProcessorContext context)
        throws ContributionResolveException {
        boolean unresolved = false;
        if (policySet != null) {
            for (Object o : policySet.getPolicies()) {
                .resolve(oresolvercontext);
                /*if ( o instanceof Policy && ((Policy)o).isUnresolved() ) {
                   unresolved = true;
                }*/
            }
            policySet.setUnresolved(unresolved);
        }
    }
    public QName getArtifactType() {
        return ;
    }
    public Class<PolicySetgetModelType() {
        return PolicySet.class;
    }
    private void resolveProvidedIntents(PolicySet policySetModelResolver resolverProcessorContext context)
        throws ContributionResolveException {
        if (policySet != null) {
            //resolve all provided intents
            List<IntentprovidedIntents = new ArrayList<Intent>();
            for (Intent providedIntent : policySet.getProvidedIntents()) {
                if (providedIntent.isUnresolved()) {
                    Intent resolved = resolver.resolveModel(Intent.classprovidedIntentcontext);
                    if (!resolved.isUnresolved() || resolved != providedIntent) {
                        providedIntents.add(resolved);
                    } else {
                        error(context.getMonitor(), "ProvidedIntentNotFound"policySetprovidedIntentpolicySet);
                        return;
                        //throw new ContributionResolveException("Provided Intent - " + providedIntent
                        //+ " not found for PolicySet " + policySet);
                    }
                } else {
                    providedIntents.add(providedIntent);
                }
            }
            policySet.getProvidedIntents().clear();
            policySet.getProvidedIntents().addAll(providedIntents);
        }
    }
    private void resolveIntentsInMappedPolicies(PolicySet policySetModelResolver resolverProcessorContext context)
        throws ContributionResolveException {
        Monitor monitor = context.getMonitor();
        for (IntentMap intentMap : policySet.getIntentMaps()) {
            Intent intent = intentMap.getProvidedIntent();
            if (intent.isUnresolved()) {
                Intent resolved = resolver.resolveModel(Intent.classintentcontext);
                if (!resolved.isUnresolved() || resolved != intent) {
                    intentMap.setProvidedIntent(resolved);
                } else {
                    error(monitor"MappedIntentNotFound"policySetintentpolicySet);
                    return;
                    //throw new ContributionResolveException("Mapped Intent - " + mappedIntent
                    //+ " not found for PolicySet " + policySet);    
                }
            }
            for (Qualifier qualifier : intentMap.getQualifiers()) {
                intent = qualifier.getIntent();
                if (intent.isUnresolved()) {
                    Intent resolved = resolver.resolveModel(Intent.classintentcontext);
                    if (!resolved.isUnresolved() || resolved != intent) {
                        qualifier.setIntent(resolved);
                    } else {
                        error(monitor"MappedIntentNotFound"policySetintentpolicySet);
                        return;
                        //throw new ContributionResolveException("Mapped Intent - " + mappedIntent
                        //+ " not found for PolicySet " + policySet);    
                    }
                }
                for (PolicyExpression exp : qualifier.getPolicies()) {
                    // FIXME: How to resolve the policies?
                }
            }
            // validate that the intent map has a qualifier for each 
            // intent qualifier. The above code has already checked that the
            // qualifiers that are there are resolved
            Intent providedIntent = intentMap.getProvidedIntent();
            if (intentMap.getQualifiers().size() != providedIntent.getQualifiedIntents().size()) {
                String missingQualifiers = "";
                for (Intent loopQualifiedIntent : providedIntent.getQualifiedIntents()) {
                    boolean found = false;
                    for (Qualifier loopQualifier : intentMap.getQualifiers()) {
                        if (loopQualifier.getIntent().getName().equals(loopQualifiedIntent.getName())) {
                            found = true;
                            break;
                        }
                    }
                    if (!found) {
                        missingQualifiers += loopQualifiedIntent.getName().getLocalPart() + " ";
                    }
                }
                if (missingQualifiers.length() > 0) {
                    Monitor.error(context.getMonitor(),
                                  this,
                                  .,
                                  "IntentMapMissingQualifiers",
                                  policySet.getName().toString(),
                                  providedIntent.getName().getLocalPart(),
                                  missingQualifiers);
                }
            }
        }
    }
    private void resolveReferredPolicySets(PolicySet policySetModelResolver resolverProcessorContext context)
        throws ContributionResolveException {
        List<PolicySetreferredPolicySets = new ArrayList<PolicySet>();
        for (PolicySet referredPolicySet : policySet.getReferencedPolicySets()) {
            if (referredPolicySet.isUnresolved()) {
                PolicySet resolved = resolver.resolveModel(PolicySet.classreferredPolicySetcontext);
                if (!resolved.isUnresolved() || resolved != referredPolicySet) {
                    referredPolicySets.add(resolved);
                } else {
                    error(context.getMonitor(), "ReferredPolicySetNotFound"policySetreferredPolicySetpolicySet);
                    return;
                    //throw new ContributionResolveException("Referred PolicySet - " + referredPolicySet
                    //+ "not found for PolicySet - " + policySet);
                }
            } else {
                referredPolicySets.add(referredPolicySet);
            }
        }
        policySet.getReferencedPolicySets().clear();
        policySet.getReferencedPolicySets().addAll(referredPolicySets);
    }
    private void includeReferredPolicySets(PolicySet policySetPolicySet referredPolicySet) {
        for (PolicySet furtherReferredPolicySet : referredPolicySet.getReferencedPolicySets()) {
            includeReferredPolicySets(referredPolicySetfurtherReferredPolicySet);
        }
        policySet.getPolicies().addAll(referredPolicySet.getPolicies());
        policySet.getIntentMaps().addAll(referredPolicySet.getIntentMaps());
    }
    public void resolve(PolicySet policySetModelResolver resolverProcessorContext context)
        throws ContributionResolveException {
        if (policySet != null && policySet.isUnresolved()) {
            resolveProvidedIntents(policySetresolvercontext);
            resolveIntentsInMappedPolicies(policySetresolvercontext);
            resolveReferredPolicySets(policySetresolvercontext);
            for (PolicySet referredPolicySet : policySet.getReferencedPolicySets()) {
                includeReferredPolicySets(policySetreferredPolicySet);
            }
            if (policySet.isUnresolved()) {
                //resolve the policy attachments
                resolvePolicies(policySetresolvercontext);
                /*if ( !policySet.isUnresolved() ) {
                     resolver.addModel(policySet);
                }*/
            }
            policySet.setUnresolved(false);
        }
    }
New to GrepCode? Check out our FAQ X