From 1390d26e0674ee489a93d0734fd60c9bac2757c5 Mon Sep 17 00:00:00 2001 From: jsdelfino Date: Sun, 17 Aug 2008 06:33:56 +0000 Subject: Some refactoring, moved the factory implementation to the impl package. Moved the XML processors to the XML package. Removed unused exception class. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@686585 13f79535-47bb-0310-9956-ffa450edef68 --- .../implementation/bpel/DefaultBPELFactory.java | 44 -- .../bpel/impl/BPELDocumentProcessor.java | 518 -------------------- .../implementation/bpel/impl/BPELFactoryImpl.java | 45 ++ .../bpel/impl/BPELImplementationImpl.java | 8 +- .../bpel/impl/BPELImplementationProcessor.java | 522 --------------------- .../bpel/impl/BPELProcessDefinitionImpl.java | 2 +- .../bpel/impl/BPELProcessException.java | 42 -- .../bpel/xml/BPELDocumentProcessor.java | 515 ++++++++++++++++++++ .../bpel/xml/BPELImplementationProcessor.java | 520 ++++++++++++++++++++ ...ca.contribution.processor.StAXArtifactProcessor | 2 +- ...sca.contribution.processor.URLArtifactProcessor | 2 +- ...che.tuscany.sca.implementation.bpel.BPELFactory | 2 +- 12 files changed, 1085 insertions(+), 1137 deletions(-) delete mode 100644 java/sca/modules/implementation-bpel/src/main/java/org/apache/tuscany/sca/implementation/bpel/DefaultBPELFactory.java delete mode 100644 java/sca/modules/implementation-bpel/src/main/java/org/apache/tuscany/sca/implementation/bpel/impl/BPELDocumentProcessor.java create mode 100644 java/sca/modules/implementation-bpel/src/main/java/org/apache/tuscany/sca/implementation/bpel/impl/BPELFactoryImpl.java delete mode 100644 java/sca/modules/implementation-bpel/src/main/java/org/apache/tuscany/sca/implementation/bpel/impl/BPELImplementationProcessor.java delete mode 100644 java/sca/modules/implementation-bpel/src/main/java/org/apache/tuscany/sca/implementation/bpel/impl/BPELProcessException.java create mode 100644 java/sca/modules/implementation-bpel/src/main/java/org/apache/tuscany/sca/implementation/bpel/xml/BPELDocumentProcessor.java create mode 100644 java/sca/modules/implementation-bpel/src/main/java/org/apache/tuscany/sca/implementation/bpel/xml/BPELImplementationProcessor.java (limited to 'java') diff --git a/java/sca/modules/implementation-bpel/src/main/java/org/apache/tuscany/sca/implementation/bpel/DefaultBPELFactory.java b/java/sca/modules/implementation-bpel/src/main/java/org/apache/tuscany/sca/implementation/bpel/DefaultBPELFactory.java deleted file mode 100644 index f5d5d98062..0000000000 --- a/java/sca/modules/implementation-bpel/src/main/java/org/apache/tuscany/sca/implementation/bpel/DefaultBPELFactory.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * 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.implementation.bpel; - -import org.apache.tuscany.sca.contribution.ModelFactoryExtensionPoint; -import org.apache.tuscany.sca.implementation.bpel.impl.BPELImplementationImpl; -import org.apache.tuscany.sca.implementation.bpel.impl.BPELProcessDefinitionImpl; - -/** - * A default factory for the BPEL implementation model. - * - * @version $Rev$ $Date$ - */ -public class DefaultBPELFactory implements BPELFactory { - - public DefaultBPELFactory(ModelFactoryExtensionPoint modelFactories) { - } - - public BPELImplementation createBPELImplementation() { - return new BPELImplementationImpl(); - } - - public BPELProcessDefinition createBPELProcessDefinition() { - return new BPELProcessDefinitionImpl(); - } - -} diff --git a/java/sca/modules/implementation-bpel/src/main/java/org/apache/tuscany/sca/implementation/bpel/impl/BPELDocumentProcessor.java b/java/sca/modules/implementation-bpel/src/main/java/org/apache/tuscany/sca/implementation/bpel/impl/BPELDocumentProcessor.java deleted file mode 100644 index caf9e2371d..0000000000 --- a/java/sca/modules/implementation-bpel/src/main/java/org/apache/tuscany/sca/implementation/bpel/impl/BPELDocumentProcessor.java +++ /dev/null @@ -1,518 +0,0 @@ -/* - * 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.implementation.bpel.impl; - -import static javax.xml.stream.XMLStreamConstants.END_ELEMENT; -import static javax.xml.stream.XMLStreamConstants.START_ELEMENT; - -import java.io.InputStream; -import java.net.URI; -import java.net.URL; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; -import java.util.List; - -import javax.wsdl.Definition; -import javax.wsdl.PortType; -import javax.wsdl.extensions.ExtensibilityElement; -import javax.xml.namespace.QName; -import javax.xml.stream.XMLInputFactory; -import javax.xml.stream.XMLStreamReader; - -import org.apache.tuscany.sca.assembly.builder.impl.ProblemImpl; -import org.apache.tuscany.sca.contribution.ModelFactoryExtensionPoint; -import org.apache.tuscany.sca.contribution.processor.BaseStAXArtifactProcessor; -import org.apache.tuscany.sca.contribution.processor.URLArtifactProcessor; -import org.apache.tuscany.sca.contribution.resolver.ModelResolver; -import org.apache.tuscany.sca.contribution.service.ContributionReadException; -import org.apache.tuscany.sca.contribution.service.ContributionResolveException; -import org.apache.tuscany.sca.implementation.bpel.BPELFactory; -import org.apache.tuscany.sca.implementation.bpel.BPELProcessDefinition; -import org.apache.tuscany.sca.implementation.bpel.xml.BPELImportElement; -import org.apache.tuscany.sca.implementation.bpel.xml.BPELPartnerLinkElement; -import org.apache.tuscany.sca.implementation.bpel.xml.BPELPartnerLinkTypeElement; -import org.apache.tuscany.sca.interfacedef.InvalidInterfaceException; -import org.apache.tuscany.sca.interfacedef.wsdl.WSDLDefinition; -import org.apache.tuscany.sca.interfacedef.wsdl.WSDLFactory; -import org.apache.tuscany.sca.interfacedef.wsdl.WSDLInterface; -import org.apache.tuscany.sca.interfacedef.wsdl.WSDLObject; -import org.apache.tuscany.sca.interfacedef.wsdl.xml.BPELPartnerLinkTypeExt; -import org.apache.tuscany.sca.monitor.Monitor; -import org.apache.tuscany.sca.monitor.Problem; -import org.apache.tuscany.sca.monitor.Problem.Severity; - -/** - * BPEL document processor responsible for reading a BPEL file and producing necessary model info about it - * - * TODO: The namespaces for WS-BPEL include 2 versions - only the earlier BPEL 1.1 versions are - * supported at present - the BPEL 2.0 namespaces also need support. This will require inspection - * of both BPEL process files and of WSDL files for their BPEL namespaces - * @version $Rev$ $Date$ - */ -public class BPELDocumentProcessor extends BaseStAXArtifactProcessor implements URLArtifactProcessor { - public final static QName BPEL_PROCESS_DEFINITION = new QName("http://schemas.xmlsoap.org/ws/2004/03/business-process/", "process"); - public final static QName BPEL_EXECUTABLE_DEFINITION = new QName("http://docs.oasis-open.org/wsbpel/2.0/process/executable", "process"); - private static final String SCA_BPEL_NS = "http://docs.oasis-open.org/ns/opencsa/sca-bpel/200801"; - private static final String BPEL_NS = "http://schemas.xmlsoap.org/ws/2004/03/business-process/"; - private static final String BPEL_PLINK_NS = "http://schemas.xmlsoap.org/ws/2004/03/partner-link/"; - private static final QName PROCESS_ELEMENT = new QName(BPEL_NS, "process"); - private static final QName PARTNERLINK_ELEMENT = new QName(BPEL_NS, "partnerLink"); - private static final QName ONEVENT_ELEMENT = new QName(BPEL_NS, "onEvent"); - private static final QName RECEIVE_ELEMENT = new QName(BPEL_NS, "receive"); - private static final QName ONMESSAGE_ELEMENT = new QName(BPEL_NS, "onMessage"); - private static final QName INVOKE_ELEMENT = new QName(BPEL_NS, "invoke"); - private static final QName IMPORT_ELEMENT = new QName(BPEL_NS, "import"); - private static final String LINKTYPE_NAME = "partnerLinkType"; - private static final QName LINKTYPE_ELEMENT = new QName(BPEL_PLINK_NS, LINKTYPE_NAME); - public final static String NAME_ELEMENT = "name"; - - private final static XMLInputFactory inputFactory = XMLInputFactory.newInstance(); - - private final BPELFactory factory; - private WSDLFactory WSDLfactory; - private Monitor monitor; - - public BPELDocumentProcessor(ModelFactoryExtensionPoint modelFactories, Monitor monitor) { - this.factory = modelFactories.getFactory(BPELFactory.class); - this.WSDLfactory = modelFactories.getFactory(WSDLFactory.class); - this.monitor = monitor; - } - - public String getArtifactType() { - return "*.bpel"; - } - - public Class getModelType() { - return BPELProcessDefinition.class; - } - - public BPELProcessDefinition read(URL contributionURL, URI artifactURI, URL artifactURL) throws ContributionReadException { - BPELProcessDefinition processDefinition = null; - try { - // for now we are just using process name - // and relying on componentType file for service definition - // so it's OK to set resolved for now - processDefinition = readProcessDefinition(artifactURL); - processDefinition.setURI(artifactURI); - processDefinition.setUnresolved(false); - } catch (Exception e) { - ContributionReadException ce = new ContributionReadException(e); - error("ContributionReadException", artifactURL, ce); - } - - return processDefinition; - } - - public void resolve(BPELProcessDefinition model, ModelResolver resolver) throws ContributionResolveException { - // FIXME - serious resolving needs to happen here - - // Step 1 is to resolve the WSDL files referenced from this BPEL process - // - one complexity here is that the WSDL definitions hold BPEL extension elements for - // the partnerLinkType declarations - and these must be used in later steps - // - // Step 2 is to take all the partnerLink definitions and establish the PortType being - // used, by tracing through the related partnerLinkType declarations - the PortType is - // effectively a definition of the interface used by the partnerLink. - // - another consideration here is that each partnerLink can involve 2 interfaces, one - // for the forward calls to the process, the other for calls from the process - depending - // on whether the partnerLink is a reference or a service, one of these interfaces is a - // callback interface. - - List theImports = model.getImports(); - for (BPELImportElement theImport : theImports) { - - // Deal with WSDL imports - if (theImport.getImportType().equals("http://schemas.xmlsoap.org/wsdl/")) { - String WSDLLocation = theImport.getLocation(); - String WSDLNamespace = theImport.getNamespace(); - - // Resolve the WSDL definition - WSDLDefinition proxy = WSDLfactory.createWSDLDefinition(); - proxy.setUnresolved(true); - proxy.setNamespace(WSDLNamespace); - if (WSDLLocation != null) { - proxy.setLocation(URI.create(WSDLLocation)); - } - WSDLDefinition resolved = resolver.resolveModel(WSDLDefinition.class, proxy); - if (resolved != null && !resolved.isUnresolved()) { - theImport.setWSDLDefinition(resolved); - } else { - error("CannotResolveWSDLReference", resolver, WSDLLocation, WSDLNamespace); - return; - } // end if - } // end if - } // end for - - // Fetch the sets of partner links, port types and interfaces - List thePLinkTypes = getPartnerLinkTypes( theImports ); - Collection theInterfaces = (Collection)new ArrayList(); - Collection thePortTypes = getAllPortTypes( theImports, theInterfaces, resolver ); - - // Store the Port Types and the Interfaces for later calculation of the component type... - model.getPortTypes().addAll(thePortTypes); - model.getInterfaces().addAll(theInterfaces); - - // Now, for each partnerLink in the BPEL process, find the related partnerLinkType - // element - List thePartnerLinks = model.getPartnerLinks(); - for (BPELPartnerLinkElement thePartnerLink : thePartnerLinks) { - QName partnerLinkType = thePartnerLink.getPartnerLinkType(); - BPELPartnerLinkTypeElement pLinkType = findPartnerLinkType(partnerLinkType, thePLinkTypes); - if (pLinkType == null) { - error("PartnerLinkNoMatchingType", thePartnerLink, thePartnerLink.getName()); - } else - thePartnerLink.setPartnerLinkType(pLinkType); - } // end for - - } // end resolve - - /** - * Retrieve all the Partner Link types defined in the imported WSDL files - * - * @param theImports - */ - private List getPartnerLinkTypes( List theImports) throws ContributionResolveException { - - List thePLinks = new ArrayList(); - - // We must find the partner link type elements from amongst the imported - // WSDLs - for (BPELImportElement theImport : theImports) { - WSDLDefinition theWSDL = theImport.getWSDLDefinition(); - Definition WSDLDefinition = theWSDL.getDefinition(); - - // The BPEL partnerLinkType elements are extension elements within - // the WSDL - List extensibilityElements = WSDLDefinition.getExtensibilityElements(); - - for (ExtensibilityElement theElement : extensibilityElements) { - QName elementType = theElement.getElementType(); - if (elementType.equals(LINKTYPE_ELEMENT)) { - BPELPartnerLinkTypeExt pLinkExt = (BPELPartnerLinkTypeExt)theElement; - // Fetch the name of the partnerLinkType - String name = pLinkExt.getName(); - QName qName = new QName(WSDLDefinition.getTargetNamespace(), name); - BPELPartnerLinkTypeElement pLinkElement = new BPELPartnerLinkTypeElement(qName); - - // The partnerLinkType must have one and may have 2 role - // child elements - int count = 0; - for (int i = 0; i < 2; i++) { - if (pLinkExt.getRoleName(i) == null) - continue; - PortType pType = WSDLDefinition.getPortType(pLinkExt.getRolePortType(i)); - if (count == 0) { - pLinkElement.setRole1(pLinkExt.getRoleName(i), pLinkExt.getRolePortType(i), pType); - count++; - } else if (count == 1) { - pLinkElement.setRole2(pLinkExt.getRoleName(i), pLinkExt.getRolePortType(i), pType); - count++; - } else { - break; - } // end if - } // end for - - if (count == 0) { - error("PartnerLinkTypeNoRoles", theElement, pLinkElement.getName()); - throw new ContributionResolveException("partnerLinkType " + pLinkElement.getName() + " has no Roles defined"); - } else - thePLinks.add(pLinkElement); - } // end if - - } // end for - } // end for - return thePLinks; - } // end getPartnerLinkTypes - - /** - * Returns all the portTypes referenced by the process. - * - * @param theImports - * @param theInterfaces - * @param resolver - * @return - * @throws ContributionResolveException - */ - private Collection getAllPortTypes(List theImports, - Collection theInterfaces, ModelResolver resolver) throws ContributionResolveException { - - Collection thePortTypes = (Collection)new ArrayList(); - for (BPELImportElement theImport : theImports) { - WSDLDefinition theWSDL = theImport.getWSDLDefinition(); - Definition wsdlDefinition = theWSDL.getDefinition(); - - Collection portTypes = (Collection)wsdlDefinition.getPortTypes().values(); - thePortTypes.addAll(portTypes); - - // Create WSDLInterface elements for each PortType found - for (PortType portType : portTypes) { - WSDLObject wsdlPortType = theWSDL.getWSDLObject(PortType.class, portType.getQName()); - WSDLInterface wsdlInterface; - if (wsdlPortType != null) { - // Introspect the WSDL portType and add the resulting - // WSDLInterface to the resolver - try { - theWSDL.setDefinition(wsdlPortType.getDefinition()); - wsdlInterface = WSDLfactory.createWSDLInterface(wsdlPortType.getElement(), theWSDL, resolver); - wsdlInterface.setWsdlDefinition(theWSDL); - } catch (InvalidInterfaceException e) { - ContributionResolveException ce = new ContributionResolveException(e); - error("ContributionResolveException", resolver, ce); - throw ce; - } // end try - resolver.addModel(wsdlInterface); - theInterfaces.add(wsdlInterface); - } // end if - } // end for - - // ----------------------- - - } // end for - - return thePortTypes; - } // end getAllPortTypes - - /** - * Finds a partnerLinkType definition within the WSDLs imported by the BPEL - * process. - * - * @param partnerLinkTypeName - the name of the partnerLinkType - * @param theImports a list of the WSDL import declarations - * @return a BPELPartnerLinkTypeElement for the partnerLinkType or null if it cannot be - * found - */ - private BPELPartnerLinkTypeElement findPartnerLinkType( QName partnerLinkTypeName, - List thePLinkTypes) { - // We must find the partner link type element from amongst the imported WSDLs - for ( BPELPartnerLinkTypeElement thePLinkType : thePLinkTypes ){ - if( thePLinkType.getName().equals(partnerLinkTypeName) ) return thePLinkType; - } // end for - return null; - } // end findPartnerLinkType - - - /** - * Read a process definition. - * - * @param doc - * @return - * @throws Exception - */ - private BPELProcessDefinition readProcessDefinition(URL doc) throws Exception { - BPELProcessDefinition processDefinition = factory.createBPELProcessDefinition(); - processDefinition.setUnresolved(true); - processDefinition.setLocation(doc); - - InputStream is = doc.openStream(); - XMLStreamReader reader = null; - try { - reader = inputFactory.createXMLStreamReader(is); - - /* - * The principle here is to look for partnerLink elements, which - * form either services or references. A partnerLink can be EITHER - - * the algorithm for deciding is: 1) Explicit marking with - * sca:reference or sca:service attribute 2) "first use" of the - * partnerLink by specific BPEL activity elements: , - * or elements imply a service - * implies a reference - */ - - // TODO - need to handle elements as kind of "nested" processes - // - and scopes introduce the possibility of partnerLinks with the - // same name at different levels of scope.... (yuk!!) - boolean completed = false; - while (!completed) { - switch (reader.next()) { - case START_ELEMENT: - QName qname = reader.getName(); - if (BPEL_PROCESS_DEFINITION.equals(qname) || BPEL_EXECUTABLE_DEFINITION.equals(qname)) { - QName processName = new QName(getString(reader, org.apache.tuscany.sca.assembly.xml.Constants.TARGET_NAMESPACE), getString(reader, NAME_ELEMENT)); - processDefinition.setName(processName); - } else if (PARTNERLINK_ELEMENT.equals(qname)) { - processDefinition.getPartnerLinks().add(processPartnerLinkElement(reader)); - } else if (ONEVENT_ELEMENT.equals(qname) || RECEIVE_ELEMENT.equals(qname) || ONMESSAGE_ELEMENT.equals(qname)) { - processPartnerLinkAsService(reader.getAttributeValue(null, "partnerLink"), processDefinition.getPartnerLinks()); - } else if (INVOKE_ELEMENT.equals(qname)) { - processPartnerLinkAsReference(reader.getAttributeValue(null, "partnerLink"), processDefinition.getPartnerLinks()); - } else if (IMPORT_ELEMENT.equals(qname)) { - processDefinition.getImports().add(processImportElement(reader)); - } // end if - break; - case END_ELEMENT: - if (PROCESS_ELEMENT.equals(reader.getName())) { - completed = true; - break; - } // end if - } // end switch - } // end while - } finally { - if (reader != null) - reader.close(); - is.close(); - } // end try - - return processDefinition; - } // end readProcessDefinition - - /** - * Processes a partnerLink element from the BPEL process and creates a - * BPELPartnerLink object - * - * @param reader - */ - private BPELPartnerLinkElement processPartnerLinkElement(XMLStreamReader reader) throws ContributionReadException { - BPELPartnerLinkElement partnerLink = new BPELPartnerLinkElement( - reader.getAttributeValue(null, "name"), - getQNameValue(reader, reader.getAttributeValue(null, "partnerLinkType")), - reader.getAttributeValue(null, "myRole"), - reader.getAttributeValue(null, "partnerRole")); - - // See if there are any SCA extension attributes - String scaService = reader.getAttributeValue(SCA_BPEL_NS, "service"); - String scaReference = reader.getAttributeValue(SCA_BPEL_NS, "reference"); - if ((scaService != null) && (scaReference != null)) { - // It is incorrect to set both service & reference attributes - error("PartnerLinkHasBothAttr", partnerLink, reader.getAttributeValue(null, "name")); - throw new ContributionReadException("BPEL PartnerLink " + reader.getAttributeValue(null, "name") + " has both sca:reference and sca:service attributes set"); - } - - // Set the SCA type and the related name, if present - if (scaService != null) - partnerLink.setAsService(scaService); - else if (scaReference != null) - partnerLink.setAsReference(scaReference); - return partnerLink; - - } // end processPartnerLinkElement - - /** - * Processes an element from the BPEL process and creates a - * BPELImportElement object - * - * @param reader - */ - private BPELImportElement processImportElement(XMLStreamReader reader) { - return (new BPELImportElement(reader.getAttributeValue(null, "location"), - reader.getAttributeValue(null, "importType"), - reader.getAttributeValue(null, "namespace"))); - - } // end processImportElement - - /** - * Mark a named partnerLink as a Service, unless it is already marked as a - * Reference - * - * @param partnerLinkName - * @param partnerLinks - */ - private void processPartnerLinkAsService(String partnerLinkName, List partnerLinks) { - BPELPartnerLinkElement partnerLink = findPartnerLinkByName(partnerLinks, partnerLinkName); - if (partnerLink == null) { - warning("ReferencePartnerLinkNotInList", partnerLinkName, partnerLinkName); - } else { - // Set the type of the partnerLink to "service" if not already - // set... - if (!partnerLink.isSCATyped()) - partnerLink.setAsService(partnerLinkName); - } // endif - } // end processPartnerLinkAsReference - - /** - * Mark a named partnerLink as a Reference, unless it is already marked as a - * Service - * - * @param partnerLinkName - * @param partnerLinks - */ - private void processPartnerLinkAsReference(String partnerLinkName, List partnerLinks) { - BPELPartnerLinkElement partnerLink = findPartnerLinkByName(partnerLinks, partnerLinkName); - if (partnerLink == null) { - warning("ReferencePartnerLinkNotInList", partnerLinkName, partnerLinkName); - } else { - // Set the type of the partnerLink to "service" if not already - // set... - if (!partnerLink.isSCATyped()) - partnerLink.setAsReference(partnerLinkName); - } // endif - } // end processPartnerLinkAsReference - - /** - * Finds a PartnerLink by name from a List of PartnerLinks returns null if - * there is no partnerLink with a matching name - returns the PartnerLink - * with a matching name - * - * @param partnerLinks - * @param partnerLinkName - */ - private BPELPartnerLinkElement findPartnerLinkByName(List partnerLinks, String partnerLinkName) { - // Scan the list looking for a partner link with the supplied name - Iterator it = partnerLinks.iterator(); - while (it.hasNext()) { - BPELPartnerLinkElement thePartnerLink = it.next(); - if (thePartnerLink.getName().equals(partnerLinkName)) - return thePartnerLink; - } - return null; - } // end method findPartnerLinkByName - - /** - * Report a warning. - * - * @param problems - * @param message - * @param model - */ - private void warning(String message, Object model, Object... messageParameters) { - if (monitor != null) { - Problem problem = new ProblemImpl(this.getClass().getName(), "impl-bpel-validation-messages", Severity.WARNING, model, message, (Object[])messageParameters); - monitor.problem(problem); - } - } - - /** - * Report a error. - * - * @param problems - * @param message - * @param model - */ - private void error(String message, Object model, Object... messageParameters) { - if (monitor != null) { - Problem problem = new ProblemImpl(this.getClass().getName(), "impl-bpel-validation-messages", Severity.ERROR, model, message, (Object[])messageParameters); - monitor.problem(problem); - } - } - - /** - * Report a exception. - * - * @param problems - * @param message - * @param model - */ - private void error(String message, Object model, Exception ex) { - if (monitor != null) { - Problem problem = new ProblemImpl(this.getClass().getName(), "impl-bpel-validation-messages", Severity.ERROR, model, message, ex); - monitor.problem(problem); - } - } -} diff --git a/java/sca/modules/implementation-bpel/src/main/java/org/apache/tuscany/sca/implementation/bpel/impl/BPELFactoryImpl.java b/java/sca/modules/implementation-bpel/src/main/java/org/apache/tuscany/sca/implementation/bpel/impl/BPELFactoryImpl.java new file mode 100644 index 0000000000..ab53964fb3 --- /dev/null +++ b/java/sca/modules/implementation-bpel/src/main/java/org/apache/tuscany/sca/implementation/bpel/impl/BPELFactoryImpl.java @@ -0,0 +1,45 @@ +/* + * 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.implementation.bpel.impl; + +import org.apache.tuscany.sca.contribution.ModelFactoryExtensionPoint; +import org.apache.tuscany.sca.implementation.bpel.BPELFactory; +import org.apache.tuscany.sca.implementation.bpel.BPELImplementation; +import org.apache.tuscany.sca.implementation.bpel.BPELProcessDefinition; + +/** + * A factory for the BPEL implementation model. + * + * @version $Rev$ $Date$ + */ +public class BPELFactoryImpl implements BPELFactory { + + public BPELFactoryImpl(ModelFactoryExtensionPoint modelFactories) { + } + + public BPELImplementation createBPELImplementation() { + return new BPELImplementationImpl(); + } + + public BPELProcessDefinition createBPELProcessDefinition() { + return new BPELProcessDefinitionImpl(); + } + +} diff --git a/java/sca/modules/implementation-bpel/src/main/java/org/apache/tuscany/sca/implementation/bpel/impl/BPELImplementationImpl.java b/java/sca/modules/implementation-bpel/src/main/java/org/apache/tuscany/sca/implementation/bpel/impl/BPELImplementationImpl.java index d4b9e872de..52af7503a3 100644 --- a/java/sca/modules/implementation-bpel/src/main/java/org/apache/tuscany/sca/implementation/bpel/impl/BPELImplementationImpl.java +++ b/java/sca/modules/implementation-bpel/src/main/java/org/apache/tuscany/sca/implementation/bpel/impl/BPELImplementationImpl.java @@ -37,18 +37,12 @@ import org.apache.tuscany.sca.implementation.bpel.BPELProcessDefinition; * * @version $Rev$ $Date$ */ -public class BPELImplementationImpl extends ImplementationImpl implements BPELImplementation { +class BPELImplementationImpl extends ImplementationImpl implements BPELImplementation { private QName processName; private BPELProcessDefinition processDefinition; private ComponentType componentType; - /** - * Constructs a new BPEL implementation. - */ - public BPELImplementationImpl() { - } - public QName getProcess() { return processName; } diff --git a/java/sca/modules/implementation-bpel/src/main/java/org/apache/tuscany/sca/implementation/bpel/impl/BPELImplementationProcessor.java b/java/sca/modules/implementation-bpel/src/main/java/org/apache/tuscany/sca/implementation/bpel/impl/BPELImplementationProcessor.java deleted file mode 100644 index 3cabbd4a72..0000000000 --- a/java/sca/modules/implementation-bpel/src/main/java/org/apache/tuscany/sca/implementation/bpel/impl/BPELImplementationProcessor.java +++ /dev/null @@ -1,522 +0,0 @@ -/* - * 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.implementation.bpel.impl; - -import static javax.xml.stream.XMLStreamConstants.END_ELEMENT; - -import java.util.Collection; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import javax.wsdl.PortType; -import javax.xml.namespace.QName; -import javax.xml.stream.XMLStreamException; -import javax.xml.stream.XMLStreamReader; -import javax.xml.stream.XMLStreamWriter; - -import org.apache.tuscany.sca.assembly.AssemblyFactory; -import org.apache.tuscany.sca.assembly.ComponentType; -import org.apache.tuscany.sca.assembly.Multiplicity; -import org.apache.tuscany.sca.assembly.Property; -import org.apache.tuscany.sca.assembly.Reference; -import org.apache.tuscany.sca.assembly.Service; -import org.apache.tuscany.sca.assembly.builder.impl.ProblemImpl; -import org.apache.tuscany.sca.assembly.xml.Constants; -import org.apache.tuscany.sca.contribution.ModelFactoryExtensionPoint; -import org.apache.tuscany.sca.contribution.processor.BaseStAXArtifactProcessor; -import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; -import org.apache.tuscany.sca.contribution.resolver.ModelResolver; -import org.apache.tuscany.sca.contribution.service.ContributionReadException; -import org.apache.tuscany.sca.contribution.service.ContributionResolveException; -import org.apache.tuscany.sca.contribution.service.ContributionWriteException; -import org.apache.tuscany.sca.databinding.xml.DOMDataBinding; -import org.apache.tuscany.sca.implementation.bpel.BPELFactory; -import org.apache.tuscany.sca.implementation.bpel.BPELImplementation; -import org.apache.tuscany.sca.implementation.bpel.BPELProcessDefinition; -import org.apache.tuscany.sca.implementation.bpel.DefaultBPELFactory; -import org.apache.tuscany.sca.implementation.bpel.xml.BPELPartnerLinkElement; -import org.apache.tuscany.sca.interfacedef.wsdl.WSDLFactory; -import org.apache.tuscany.sca.interfacedef.wsdl.WSDLInterface; -import org.apache.tuscany.sca.interfacedef.wsdl.WSDLInterfaceContract; -import org.apache.tuscany.sca.monitor.Monitor; -import org.apache.tuscany.sca.monitor.Problem; -import org.apache.tuscany.sca.monitor.Problem.Severity; - -/** - * Implements a StAX artifact processor for BPEL implementations. - * - * The artifact processor is responsible for processing - * elements in SCA assembly XML composite files and populating the BPEL - * implementation model, resolving its references to other artifacts in the SCA - * contribution, and optionally write the model back to SCA assembly XML. - * - * @version $Rev$ $Date$ - */ -public class BPELImplementationProcessor extends BaseStAXArtifactProcessor implements StAXArtifactProcessor { - private static final String PROCESS = "process"; - private static final String IMPLEMENTATION_BPEL = "implementation.bpel"; - private static final QName IMPLEMENTATION_BPEL_QNAME = new QName(Constants.SCA10_NS, IMPLEMENTATION_BPEL); - - private AssemblyFactory assemblyFactory; - private BPELFactory bpelFactory; - private WSDLFactory wsdlFactory; - private Monitor monitor; - - public BPELImplementationProcessor(ModelFactoryExtensionPoint modelFactories, Monitor monitor) { - this.assemblyFactory = modelFactories.getFactory(AssemblyFactory.class); - this.wsdlFactory = modelFactories.getFactory(WSDLFactory.class); - this.bpelFactory = new DefaultBPELFactory(modelFactories); - this.monitor = monitor; - } - - public QName getArtifactType() { - // Returns the QName of the XML element processed by this processor - return IMPLEMENTATION_BPEL_QNAME; - } - - public Class getModelType() { - // Returns the type of model processed by this processor - return BPELImplementation.class; - } - - public BPELImplementation read(XMLStreamReader reader) throws ContributionReadException, XMLStreamException { - assert IMPLEMENTATION_BPEL_QNAME.equals(reader.getName()); - - // Read an element - BPELImplementation implementation = null; - - // Read the process attribute. - QName process = getAttributeValueNS(reader, PROCESS); - if (process == null) { - return implementation; - } - - // Create and initialize the BPEL implementation model - implementation = bpelFactory.createBPELImplementation(); - implementation.setProcess(process); - implementation.setUnresolved(true); - - // Skip to end element - while (reader.hasNext()) { - if (reader.next() == END_ELEMENT && IMPLEMENTATION_BPEL_QNAME.equals(reader.getName())) { - break; - } - } - - return implementation; - } - - public void resolve(BPELImplementation implementation, ModelResolver resolver) throws ContributionResolveException { - - if( implementation != null && implementation.isUnresolved()) - { - BPELProcessDefinition processDefinition = resolveBPELProcessDefinition(implementation, resolver); - if(processDefinition.isUnresolved()) { - error("BPELProcessNotFound", implementation, processDefinition.getName()); - } else { - implementation.setProcessDefinition(processDefinition); - - // Get the component type from the process definition - generateComponentType( implementation ); - - //resolve component type - mergeComponentType(resolver, implementation); - - //set current implementation resolved - implementation.setUnresolved(false); - } - } - - } // end resolve - - public void write( BPELImplementation bpelImplementation, - XMLStreamWriter writer ) throws ContributionWriteException, XMLStreamException { - //FIXME Deal with policy processing... - // Write - // policyProcessor.writePolicyPrefixes(bpelImplementation, writer); - writer.writeStartElement(Constants.SCA10_NS, IMPLEMENTATION_BPEL); - // policyProcessor.writePolicyAttributes(bpelImplementation, writer); - - if (bpelImplementation.getProcess() != null) { - writer.writeAttribute(PROCESS, bpelImplementation.getProcess().toString() ); - } - - writer.writeEndElement(); - - } // end write - - private BPELProcessDefinition resolveBPELProcessDefinition(BPELImplementation impl, ModelResolver resolver) throws ContributionResolveException { - QName processName = impl.getProcess(); - BPELProcessDefinition processDefinition = this.bpelFactory.createBPELProcessDefinition(); - processDefinition.setName(processName); - processDefinition.setUnresolved(true); - - return resolver.resolveModel(BPELProcessDefinition.class, processDefinition); - } // end resolveBPELProcessDefinition - - /** - * Calculates the component type of the supplied implementation and attaches it to the - * implementation. - * - * @param impl - * @throws ContributionResolveException - */ - private void generateComponentType(BPELImplementation impl) throws ContributionResolveException { - - // Create a ComponentType and mark it unresolved - ComponentType componentType = assemblyFactory.createComponentType(); - componentType.setUnresolved(true); - impl.setComponentType(componentType); - - // Each partner link in the process represents either a service or a - // reference - // - or both, in the sense of involving a callback - BPELProcessDefinition theProcess = impl.getProcessDefinition(); - List partnerLinks = theProcess.getPartnerLinks(); - - for (BPELPartnerLinkElement pLink : partnerLinks) { - - // check that the partner link has been designated as service or - // reference in SCA terms - if (pLink.isSCATyped()) { - String SCAName = pLink.getSCAName(); - if (pLink.querySCAType().equals("reference")) { - componentType.getReferences().add(generateReference(SCAName, pLink.getMyRolePortType(), pLink.getPartnerRolePortType(), theProcess.getInterfaces())); - } else { - componentType.getServices().add(generateService(SCAName, pLink.getMyRolePortType(), pLink.getPartnerRolePortType(), theProcess.getInterfaces())); - } // end if - } // end if - } // end for - - } // end getComponentType - - /** - * Create an SCA reference for a partnerLink - * @param name - name of the reference - * @param myRolePT - partner link type of myRole - * @param partnerRolePT - partner link type of partnerRole - * @param theInterfaces - list of WSDL interfaces associated with the BPEL process - * @return - */ - private Reference generateReference( String name, PortType myRolePT, - PortType partnerRolePT, Collection theInterfaces) throws ContributionResolveException { - - Reference reference = assemblyFactory.createReference(); - WSDLInterfaceContract interfaceContract = wsdlFactory.createWSDLInterfaceContract(); - reference.setInterfaceContract(interfaceContract); - - // Establish whether there is just a call interface or a call + callback - // interface - PortType callPT = null; - PortType callbackPT = null; - if (myRolePT != null) { - callPT = myRolePT; - // If the 2 port types are not the same one, there is a callback... - if (partnerRolePT != null) { - if (!myRolePT.getQName().equals(partnerRolePT.getQName())) { - callbackPT = partnerRolePT; - } // end if - } // end if - } else if (partnerRolePT != null) { - callPT = partnerRolePT; - } // end if - - // No interfaces mean an error - if (callPT == null && callbackPT == null) { - error("MyRolePartnerRoleNull", theInterfaces); - } // end if - - // Set the name of the reference to the supplied name and the - // multiplicity of the reference - // to 1..1 - // TODO: support other multiplicities - reference.setName(name); - reference.setMultiplicity(Multiplicity.ONE_ONE); - - if (callPT != null) { - // Set the call interface and, if present, the callback interface - WSDLInterface callInterface = null; - for (WSDLInterface anInterface : theInterfaces) { - if (anInterface.getPortType().getQName().equals(callPT.getQName())) - callInterface = anInterface; - } // end for - if (callInterface == null) { - error("NoInterfaceForPortType", theInterfaces, callPT.getQName().toString()); - } else - reference.getInterfaceContract().setInterface(callInterface); - } - - // There is a callback if the partner role is not null and if the - // partner role port type - // is not the same as the port type for my role - if (callbackPT != null) { - WSDLInterface callbackInterface = null; - for (WSDLInterface anInterface : theInterfaces) { - if (anInterface.getPortType().getQName().equals(callbackPT.getQName())) - callbackInterface = anInterface; - } // end for - if (callbackInterface == null) { - error("NoInterfaceForPortType", theInterfaces, callbackPT.getQName().toString()); - } else - reference.getInterfaceContract().setCallbackInterface(callbackInterface); - } // end if - - return reference; - } // end generateReference - - /** - * Create an SCA service for a partnerLink - * @param name - name of the reference - * @param myRolePT - partner link type of myRole - * @param partnerRolePT - partner link type of partnerRole - * @param theInterfaces - list of WSDL interfaces associated with the BPEL process - * @return - */ - private Service generateService( String name, PortType myRolePT, - PortType partnerRolePT, Collection theInterfaces ) - throws ContributionResolveException { - Service service = assemblyFactory.createService(); - WSDLInterfaceContract interfaceContract = wsdlFactory.createWSDLInterfaceContract(); - service.setInterfaceContract(interfaceContract); - - // Set the name of the service to the supplied name - service.setName(name); - - // Establish whether there is just a call interface or a call + callback - // interface - PortType callPT = null; - PortType callbackPT = null; - if (myRolePT != null) { - callPT = myRolePT; - // If the 2 port types are not the same one, there is a callback... - if (partnerRolePT != null) { - if (!myRolePT.getQName().equals(partnerRolePT.getQName())) { - callbackPT = partnerRolePT; - } // end if - } // end if - } else if (partnerRolePT != null) { - callPT = partnerRolePT; - } // end if - - // No interfaces mean an error - if (callPT == null && callbackPT == null) { - error("MyRolePartnerRoleNull", theInterfaces); - } // end if - - if (callPT != null) { - // Set the call interface and, if present, the callback interface - WSDLInterface callInterface = null; - for (WSDLInterface anInterface : theInterfaces) { - if (anInterface.getPortType().getQName().equals(callPT.getQName())) - callInterface = anInterface; - } // end for - if (callInterface == null) { - error("NoInterfaceForPortType", theInterfaces, callPT.getQName().toString()); - } else - service.getInterfaceContract().setInterface(callInterface); - } // end if - - // There is a callback if the partner role is not null and if the - // partner role port type - // is not the same as the port type for my role - if (callbackPT != null) { - WSDLInterface callbackInterface = null; - for (WSDLInterface anInterface : theInterfaces) { - if (anInterface.getPortType().getQName().equals(callbackPT.getQName())) - callbackInterface = anInterface; - } // end for - if (callbackInterface == null) { - error("NoInterfaceForPortType", theInterfaces, callbackPT.getQName().toString()); - } else - service.getInterfaceContract().setCallbackInterface(callbackInterface); - } // end if - - return service; - } // end generateService - - /** - * Merge the componentType from introspection and from external file - * - * Note the setting of the DataBinding for both Services and References to DOM, since this is - * the data format expected by the ODE BPEL implementation code. - * - * @param resolver - * @param impl - */ - private void mergeComponentType(ModelResolver resolver, BPELImplementation impl) { - - // Load the component type from a component type file, if any - ComponentType componentType = getComponentType(resolver, impl); - if (componentType != null && !componentType.isUnresolved()) { - - // References... - Map refMap = new HashMap(); - for (Reference reference : componentType.getReferences()) { - reference.getInterfaceContract().getInterface().resetDataBinding(DOMDataBinding.NAME); - refMap.put(reference.getName(), reference); - } // end for - - // For the present, overwrite anything arising from the component - // type sidefile if - // equivalent services are defined in the implementation. - // TODO - a more careful merge must be done, using the - // implementation introspection data - // as the master but adding any additional and non-conflicting - // information from the - // sidefile - for (Reference ref : impl.getReferences()) { - ref.getInterfaceContract().getInterface().resetDataBinding(DOMDataBinding.NAME); - refMap.put(ref.getName(), ref); - } // end for - - impl.getReferences().clear(); - impl.getReferences().addAll(refMap.values()); - - // Services..... - Map serviceMap = new HashMap(); - for (Service service : componentType.getServices()) { - service.getInterfaceContract().getInterface().resetDataBinding(DOMDataBinding.NAME); - serviceMap.put(service.getName(), service); - } // end for - - // For the present, overwrite anything arising from the component - // type sidefile if - // equivalent services are defined in the implementation. - // TODO - a more careful merge must be done, using the - // implementation introspection data - // as the master but adding any additional and non-conflicting - // information from the - // sidefile - for (Service svc : impl.getServices()) { - svc.getInterfaceContract().getInterface().resetDataBinding(DOMDataBinding.NAME); - serviceMap.put(svc.getName(), svc); - } // end for - - impl.getServices().clear(); - impl.getServices().addAll(serviceMap.values()); - - // Properties - Map propMap = new HashMap(); - for (Property property : componentType.getProperties()) { - propMap.put(property.getName(), property); - } // end for - - // A simple overwrite of any equivalent properties from the - // component type sidefile - for (Property prop : impl.getProperties()) { - propMap.put(prop.getName(), prop); - } - } - } - - - /** - * Find the componentType side file based on the BPEL implementation artifact - * @param resolver - * @param impl - * @return - */ - private ComponentType getComponentType(ModelResolver resolver, BPELImplementation impl) { - String bpelProcessURI = impl.getProcessDefinition().getURI().toString(); - - // Get the component type definition contained in the componentType file, if any - String componentTypeURI = bpelProcessURI.replace(".bpel", ".componentType"); - ComponentType componentType = assemblyFactory.createComponentType(); - componentType.setUnresolved(true); - componentType.setURI(componentTypeURI); - componentType = resolver.resolveModel(ComponentType.class, componentType); - if (!componentType.isUnresolved()) { - return componentType; - } - return null; - } // end getComponentType - - /** - * Returns a QName from its string representation in a named attribute of an XML element - * supplied in an XMLStreamReader - * - * QName attributes of an XML element (such as BPEL process) is presented in one of - * two alternative formats: - * 1) In the form of a local name with a prefix, with the prefix referencing a namespace - * URI declaration elsewhere in the composite (typically on the composite element) - * - * ie: nms:SomeName - * xmlns:nms="http://example.com/somenamespace" - * - * 2) In the XML Namespaces recommendation format (see http://jclark.com/xml/xmlns.htm ) - * where the namespace URI and the local name are encoded into a single string, with the - * namespace URI enclosed between a pair of braces {...} - * - * ie: {http://example.com/somenamespace}SomeName - */ - private QName getAttributeValueNS(XMLStreamReader reader, String attribute) { - String fullValue = reader.getAttributeValue(null, attribute); - if (fullValue == null) { - error("AttributeProcessMissing", reader); - return null; - } - - // Deal with the attribute in the XML Namespaces recommendation format - // - trim off any leading/trailing spaces and check that the first - // character is '{' - if (fullValue.trim().charAt(0) == '{') { - try { - // Attempt conversion to a QName object - QName theProcess = QName.valueOf(fullValue); - return theProcess; - } catch (IllegalArgumentException e) { - // This exception happens if the attribute begins with '{' but - // doesn't conform - // to the XML Namespaces recommendation format - error("AttributeWithoutNamespace", reader, attribute, fullValue); - return null; - } - } // endif - - // Deal with the attribute in the local name + prefix format - if (fullValue.indexOf(":") < 0) { - error("AttributeWithoutPrefix", reader, attribute, fullValue); - return null; - } - String prefix = fullValue.substring(0, fullValue.indexOf(":")); - String name = fullValue.substring(fullValue.indexOf(":") + 1); - String nsUri = reader.getNamespaceContext().getNamespaceURI(prefix); - if (nsUri == null) { - error("AttributeUnrecognizedNamespace", reader, attribute, fullValue); - return null; - } - return new QName(nsUri, name, prefix); - } - - /** - * Report a error. - * - * @param problems - * @param message - * @param model - */ - private void error(String message, Object model, Object... messageParameters) { - if (monitor != null) { - Problem problem = new ProblemImpl(this.getClass().getName(), "impl-bpel-validation-messages", Severity.ERROR, model, message, (Object[])messageParameters); - monitor.problem(problem); - } - } - -} diff --git a/java/sca/modules/implementation-bpel/src/main/java/org/apache/tuscany/sca/implementation/bpel/impl/BPELProcessDefinitionImpl.java b/java/sca/modules/implementation-bpel/src/main/java/org/apache/tuscany/sca/implementation/bpel/impl/BPELProcessDefinitionImpl.java index 1372b94eee..0f087e3727 100644 --- a/java/sca/modules/implementation-bpel/src/main/java/org/apache/tuscany/sca/implementation/bpel/impl/BPELProcessDefinitionImpl.java +++ b/java/sca/modules/implementation-bpel/src/main/java/org/apache/tuscany/sca/implementation/bpel/impl/BPELProcessDefinitionImpl.java @@ -37,7 +37,7 @@ import org.apache.tuscany.sca.interfacedef.wsdl.WSDLInterface; * * @version $Rev$ $Date$ */ -public class BPELProcessDefinitionImpl implements BPELProcessDefinition { +class BPELProcessDefinitionImpl implements BPELProcessDefinition { private QName name; private URI uri; private URL location; diff --git a/java/sca/modules/implementation-bpel/src/main/java/org/apache/tuscany/sca/implementation/bpel/impl/BPELProcessException.java b/java/sca/modules/implementation-bpel/src/main/java/org/apache/tuscany/sca/implementation/bpel/impl/BPELProcessException.java deleted file mode 100644 index 9b9f62a0be..0000000000 --- a/java/sca/modules/implementation-bpel/src/main/java/org/apache/tuscany/sca/implementation/bpel/impl/BPELProcessException.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * 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.implementation.bpel.impl; - -/** - * Thrown when a process can't be compiled properly or when its descriptors - * are invalid. - * - * @version $Rev$ $Date$ - */ -public class BPELProcessException extends RuntimeException { - private static final long serialVersionUID = 1047893235216756186L; - - public BPELProcessException(String message) { - super(message); - } - - public BPELProcessException(String message, Throwable cause) { - super(message, cause); - } - - public BPELProcessException(Throwable cause) { - super(cause); - } -} diff --git a/java/sca/modules/implementation-bpel/src/main/java/org/apache/tuscany/sca/implementation/bpel/xml/BPELDocumentProcessor.java b/java/sca/modules/implementation-bpel/src/main/java/org/apache/tuscany/sca/implementation/bpel/xml/BPELDocumentProcessor.java new file mode 100644 index 0000000000..6953c7ae93 --- /dev/null +++ b/java/sca/modules/implementation-bpel/src/main/java/org/apache/tuscany/sca/implementation/bpel/xml/BPELDocumentProcessor.java @@ -0,0 +1,515 @@ +/* + * 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.implementation.bpel.xml; + +import static javax.xml.stream.XMLStreamConstants.END_ELEMENT; +import static javax.xml.stream.XMLStreamConstants.START_ELEMENT; + +import java.io.InputStream; +import java.net.URI; +import java.net.URL; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; + +import javax.wsdl.Definition; +import javax.wsdl.PortType; +import javax.wsdl.extensions.ExtensibilityElement; +import javax.xml.namespace.QName; +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLStreamReader; + +import org.apache.tuscany.sca.assembly.builder.impl.ProblemImpl; +import org.apache.tuscany.sca.contribution.ModelFactoryExtensionPoint; +import org.apache.tuscany.sca.contribution.processor.BaseStAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.URLArtifactProcessor; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.contribution.service.ContributionReadException; +import org.apache.tuscany.sca.contribution.service.ContributionResolveException; +import org.apache.tuscany.sca.implementation.bpel.BPELFactory; +import org.apache.tuscany.sca.implementation.bpel.BPELProcessDefinition; +import org.apache.tuscany.sca.interfacedef.InvalidInterfaceException; +import org.apache.tuscany.sca.interfacedef.wsdl.WSDLDefinition; +import org.apache.tuscany.sca.interfacedef.wsdl.WSDLFactory; +import org.apache.tuscany.sca.interfacedef.wsdl.WSDLInterface; +import org.apache.tuscany.sca.interfacedef.wsdl.WSDLObject; +import org.apache.tuscany.sca.interfacedef.wsdl.xml.BPELPartnerLinkTypeExt; +import org.apache.tuscany.sca.monitor.Monitor; +import org.apache.tuscany.sca.monitor.Problem; +import org.apache.tuscany.sca.monitor.Problem.Severity; + +/** + * BPEL document processor responsible for reading a BPEL file and producing necessary model info about it + * + * TODO: The namespaces for WS-BPEL include 2 versions - only the earlier BPEL 1.1 versions are + * supported at present - the BPEL 2.0 namespaces also need support. This will require inspection + * of both BPEL process files and of WSDL files for their BPEL namespaces + * @version $Rev$ $Date$ + */ +public class BPELDocumentProcessor extends BaseStAXArtifactProcessor implements URLArtifactProcessor { + public final static QName BPEL_PROCESS_DEFINITION = new QName("http://schemas.xmlsoap.org/ws/2004/03/business-process/", "process"); + public final static QName BPEL_EXECUTABLE_DEFINITION = new QName("http://docs.oasis-open.org/wsbpel/2.0/process/executable", "process"); + private static final String SCA_BPEL_NS = "http://docs.oasis-open.org/ns/opencsa/sca-bpel/200801"; + private static final String BPEL_NS = "http://schemas.xmlsoap.org/ws/2004/03/business-process/"; + private static final String BPEL_PLINK_NS = "http://schemas.xmlsoap.org/ws/2004/03/partner-link/"; + private static final QName PROCESS_ELEMENT = new QName(BPEL_NS, "process"); + private static final QName PARTNERLINK_ELEMENT = new QName(BPEL_NS, "partnerLink"); + private static final QName ONEVENT_ELEMENT = new QName(BPEL_NS, "onEvent"); + private static final QName RECEIVE_ELEMENT = new QName(BPEL_NS, "receive"); + private static final QName ONMESSAGE_ELEMENT = new QName(BPEL_NS, "onMessage"); + private static final QName INVOKE_ELEMENT = new QName(BPEL_NS, "invoke"); + private static final QName IMPORT_ELEMENT = new QName(BPEL_NS, "import"); + private static final String LINKTYPE_NAME = "partnerLinkType"; + private static final QName LINKTYPE_ELEMENT = new QName(BPEL_PLINK_NS, LINKTYPE_NAME); + public final static String NAME_ELEMENT = "name"; + + private final static XMLInputFactory inputFactory = XMLInputFactory.newInstance(); + + private final BPELFactory factory; + private WSDLFactory WSDLfactory; + private Monitor monitor; + + public BPELDocumentProcessor(ModelFactoryExtensionPoint modelFactories, Monitor monitor) { + this.factory = modelFactories.getFactory(BPELFactory.class); + this.WSDLfactory = modelFactories.getFactory(WSDLFactory.class); + this.monitor = monitor; + } + + public String getArtifactType() { + return "*.bpel"; + } + + public Class getModelType() { + return BPELProcessDefinition.class; + } + + public BPELProcessDefinition read(URL contributionURL, URI artifactURI, URL artifactURL) throws ContributionReadException { + BPELProcessDefinition processDefinition = null; + try { + // for now we are just using process name + // and relying on componentType file for service definition + // so it's OK to set resolved for now + processDefinition = readProcessDefinition(artifactURL); + processDefinition.setURI(artifactURI); + processDefinition.setUnresolved(false); + } catch (Exception e) { + ContributionReadException ce = new ContributionReadException(e); + error("ContributionReadException", artifactURL, ce); + } + + return processDefinition; + } + + public void resolve(BPELProcessDefinition model, ModelResolver resolver) throws ContributionResolveException { + // FIXME - serious resolving needs to happen here + + // Step 1 is to resolve the WSDL files referenced from this BPEL process + // - one complexity here is that the WSDL definitions hold BPEL extension elements for + // the partnerLinkType declarations - and these must be used in later steps + // + // Step 2 is to take all the partnerLink definitions and establish the PortType being + // used, by tracing through the related partnerLinkType declarations - the PortType is + // effectively a definition of the interface used by the partnerLink. + // - another consideration here is that each partnerLink can involve 2 interfaces, one + // for the forward calls to the process, the other for calls from the process - depending + // on whether the partnerLink is a reference or a service, one of these interfaces is a + // callback interface. + + List theImports = model.getImports(); + for (BPELImportElement theImport : theImports) { + + // Deal with WSDL imports + if (theImport.getImportType().equals("http://schemas.xmlsoap.org/wsdl/")) { + String WSDLLocation = theImport.getLocation(); + String WSDLNamespace = theImport.getNamespace(); + + // Resolve the WSDL definition + WSDLDefinition proxy = WSDLfactory.createWSDLDefinition(); + proxy.setUnresolved(true); + proxy.setNamespace(WSDLNamespace); + if (WSDLLocation != null) { + proxy.setLocation(URI.create(WSDLLocation)); + } + WSDLDefinition resolved = resolver.resolveModel(WSDLDefinition.class, proxy); + if (resolved != null && !resolved.isUnresolved()) { + theImport.setWSDLDefinition(resolved); + } else { + error("CannotResolveWSDLReference", resolver, WSDLLocation, WSDLNamespace); + return; + } // end if + } // end if + } // end for + + // Fetch the sets of partner links, port types and interfaces + List thePLinkTypes = getPartnerLinkTypes( theImports ); + Collection theInterfaces = (Collection)new ArrayList(); + Collection thePortTypes = getAllPortTypes( theImports, theInterfaces, resolver ); + + // Store the Port Types and the Interfaces for later calculation of the component type... + model.getPortTypes().addAll(thePortTypes); + model.getInterfaces().addAll(theInterfaces); + + // Now, for each partnerLink in the BPEL process, find the related partnerLinkType + // element + List thePartnerLinks = model.getPartnerLinks(); + for (BPELPartnerLinkElement thePartnerLink : thePartnerLinks) { + QName partnerLinkType = thePartnerLink.getPartnerLinkType(); + BPELPartnerLinkTypeElement pLinkType = findPartnerLinkType(partnerLinkType, thePLinkTypes); + if (pLinkType == null) { + error("PartnerLinkNoMatchingType", thePartnerLink, thePartnerLink.getName()); + } else + thePartnerLink.setPartnerLinkType(pLinkType); + } // end for + + } // end resolve + + /** + * Retrieve all the Partner Link types defined in the imported WSDL files + * + * @param theImports + */ + private List getPartnerLinkTypes( List theImports) throws ContributionResolveException { + + List thePLinks = new ArrayList(); + + // We must find the partner link type elements from amongst the imported + // WSDLs + for (BPELImportElement theImport : theImports) { + WSDLDefinition theWSDL = theImport.getWSDLDefinition(); + Definition WSDLDefinition = theWSDL.getDefinition(); + + // The BPEL partnerLinkType elements are extension elements within + // the WSDL + List extensibilityElements = WSDLDefinition.getExtensibilityElements(); + + for (ExtensibilityElement theElement : extensibilityElements) { + QName elementType = theElement.getElementType(); + if (elementType.equals(LINKTYPE_ELEMENT)) { + BPELPartnerLinkTypeExt pLinkExt = (BPELPartnerLinkTypeExt)theElement; + // Fetch the name of the partnerLinkType + String name = pLinkExt.getName(); + QName qName = new QName(WSDLDefinition.getTargetNamespace(), name); + BPELPartnerLinkTypeElement pLinkElement = new BPELPartnerLinkTypeElement(qName); + + // The partnerLinkType must have one and may have 2 role + // child elements + int count = 0; + for (int i = 0; i < 2; i++) { + if (pLinkExt.getRoleName(i) == null) + continue; + PortType pType = WSDLDefinition.getPortType(pLinkExt.getRolePortType(i)); + if (count == 0) { + pLinkElement.setRole1(pLinkExt.getRoleName(i), pLinkExt.getRolePortType(i), pType); + count++; + } else if (count == 1) { + pLinkElement.setRole2(pLinkExt.getRoleName(i), pLinkExt.getRolePortType(i), pType); + count++; + } else { + break; + } // end if + } // end for + + if (count == 0) { + error("PartnerLinkTypeNoRoles", theElement, pLinkElement.getName()); + throw new ContributionResolveException("partnerLinkType " + pLinkElement.getName() + " has no Roles defined"); + } else + thePLinks.add(pLinkElement); + } // end if + + } // end for + } // end for + return thePLinks; + } // end getPartnerLinkTypes + + /** + * Returns all the portTypes referenced by the process. + * + * @param theImports + * @param theInterfaces + * @param resolver + * @return + * @throws ContributionResolveException + */ + private Collection getAllPortTypes(List theImports, + Collection theInterfaces, ModelResolver resolver) throws ContributionResolveException { + + Collection thePortTypes = (Collection)new ArrayList(); + for (BPELImportElement theImport : theImports) { + WSDLDefinition theWSDL = theImport.getWSDLDefinition(); + Definition wsdlDefinition = theWSDL.getDefinition(); + + Collection portTypes = (Collection)wsdlDefinition.getPortTypes().values(); + thePortTypes.addAll(portTypes); + + // Create WSDLInterface elements for each PortType found + for (PortType portType : portTypes) { + WSDLObject wsdlPortType = theWSDL.getWSDLObject(PortType.class, portType.getQName()); + WSDLInterface wsdlInterface; + if (wsdlPortType != null) { + // Introspect the WSDL portType and add the resulting + // WSDLInterface to the resolver + try { + theWSDL.setDefinition(wsdlPortType.getDefinition()); + wsdlInterface = WSDLfactory.createWSDLInterface(wsdlPortType.getElement(), theWSDL, resolver); + wsdlInterface.setWsdlDefinition(theWSDL); + } catch (InvalidInterfaceException e) { + ContributionResolveException ce = new ContributionResolveException(e); + error("ContributionResolveException", resolver, ce); + throw ce; + } // end try + resolver.addModel(wsdlInterface); + theInterfaces.add(wsdlInterface); + } // end if + } // end for + + // ----------------------- + + } // end for + + return thePortTypes; + } // end getAllPortTypes + + /** + * Finds a partnerLinkType definition within the WSDLs imported by the BPEL + * process. + * + * @param partnerLinkTypeName - the name of the partnerLinkType + * @param theImports a list of the WSDL import declarations + * @return a BPELPartnerLinkTypeElement for the partnerLinkType or null if it cannot be + * found + */ + private BPELPartnerLinkTypeElement findPartnerLinkType( QName partnerLinkTypeName, + List thePLinkTypes) { + // We must find the partner link type element from amongst the imported WSDLs + for ( BPELPartnerLinkTypeElement thePLinkType : thePLinkTypes ){ + if( thePLinkType.getName().equals(partnerLinkTypeName) ) return thePLinkType; + } // end for + return null; + } // end findPartnerLinkType + + + /** + * Read a process definition. + * + * @param doc + * @return + * @throws Exception + */ + private BPELProcessDefinition readProcessDefinition(URL doc) throws Exception { + BPELProcessDefinition processDefinition = factory.createBPELProcessDefinition(); + processDefinition.setUnresolved(true); + processDefinition.setLocation(doc); + + InputStream is = doc.openStream(); + XMLStreamReader reader = null; + try { + reader = inputFactory.createXMLStreamReader(is); + + /* + * The principle here is to look for partnerLink elements, which + * form either services or references. A partnerLink can be EITHER - + * the algorithm for deciding is: 1) Explicit marking with + * sca:reference or sca:service attribute 2) "first use" of the + * partnerLink by specific BPEL activity elements: , + * or elements imply a service + * implies a reference + */ + + // TODO - need to handle elements as kind of "nested" processes + // - and scopes introduce the possibility of partnerLinks with the + // same name at different levels of scope.... (yuk!!) + boolean completed = false; + while (!completed) { + switch (reader.next()) { + case START_ELEMENT: + QName qname = reader.getName(); + if (BPEL_PROCESS_DEFINITION.equals(qname) || BPEL_EXECUTABLE_DEFINITION.equals(qname)) { + QName processName = new QName(getString(reader, org.apache.tuscany.sca.assembly.xml.Constants.TARGET_NAMESPACE), getString(reader, NAME_ELEMENT)); + processDefinition.setName(processName); + } else if (PARTNERLINK_ELEMENT.equals(qname)) { + processDefinition.getPartnerLinks().add(processPartnerLinkElement(reader)); + } else if (ONEVENT_ELEMENT.equals(qname) || RECEIVE_ELEMENT.equals(qname) || ONMESSAGE_ELEMENT.equals(qname)) { + processPartnerLinkAsService(reader.getAttributeValue(null, "partnerLink"), processDefinition.getPartnerLinks()); + } else if (INVOKE_ELEMENT.equals(qname)) { + processPartnerLinkAsReference(reader.getAttributeValue(null, "partnerLink"), processDefinition.getPartnerLinks()); + } else if (IMPORT_ELEMENT.equals(qname)) { + processDefinition.getImports().add(processImportElement(reader)); + } // end if + break; + case END_ELEMENT: + if (PROCESS_ELEMENT.equals(reader.getName())) { + completed = true; + break; + } // end if + } // end switch + } // end while + } finally { + if (reader != null) + reader.close(); + is.close(); + } // end try + + return processDefinition; + } // end readProcessDefinition + + /** + * Processes a partnerLink element from the BPEL process and creates a + * BPELPartnerLink object + * + * @param reader + */ + private BPELPartnerLinkElement processPartnerLinkElement(XMLStreamReader reader) throws ContributionReadException { + BPELPartnerLinkElement partnerLink = new BPELPartnerLinkElement( + reader.getAttributeValue(null, "name"), + getQNameValue(reader, reader.getAttributeValue(null, "partnerLinkType")), + reader.getAttributeValue(null, "myRole"), + reader.getAttributeValue(null, "partnerRole")); + + // See if there are any SCA extension attributes + String scaService = reader.getAttributeValue(SCA_BPEL_NS, "service"); + String scaReference = reader.getAttributeValue(SCA_BPEL_NS, "reference"); + if ((scaService != null) && (scaReference != null)) { + // It is incorrect to set both service & reference attributes + error("PartnerLinkHasBothAttr", partnerLink, reader.getAttributeValue(null, "name")); + throw new ContributionReadException("BPEL PartnerLink " + reader.getAttributeValue(null, "name") + " has both sca:reference and sca:service attributes set"); + } + + // Set the SCA type and the related name, if present + if (scaService != null) + partnerLink.setAsService(scaService); + else if (scaReference != null) + partnerLink.setAsReference(scaReference); + return partnerLink; + + } // end processPartnerLinkElement + + /** + * Processes an element from the BPEL process and creates a + * BPELImportElement object + * + * @param reader + */ + private BPELImportElement processImportElement(XMLStreamReader reader) { + return (new BPELImportElement(reader.getAttributeValue(null, "location"), + reader.getAttributeValue(null, "importType"), + reader.getAttributeValue(null, "namespace"))); + + } // end processImportElement + + /** + * Mark a named partnerLink as a Service, unless it is already marked as a + * Reference + * + * @param partnerLinkName + * @param partnerLinks + */ + private void processPartnerLinkAsService(String partnerLinkName, List partnerLinks) { + BPELPartnerLinkElement partnerLink = findPartnerLinkByName(partnerLinks, partnerLinkName); + if (partnerLink == null) { + warning("ReferencePartnerLinkNotInList", partnerLinkName, partnerLinkName); + } else { + // Set the type of the partnerLink to "service" if not already + // set... + if (!partnerLink.isSCATyped()) + partnerLink.setAsService(partnerLinkName); + } // endif + } // end processPartnerLinkAsReference + + /** + * Mark a named partnerLink as a Reference, unless it is already marked as a + * Service + * + * @param partnerLinkName + * @param partnerLinks + */ + private void processPartnerLinkAsReference(String partnerLinkName, List partnerLinks) { + BPELPartnerLinkElement partnerLink = findPartnerLinkByName(partnerLinks, partnerLinkName); + if (partnerLink == null) { + warning("ReferencePartnerLinkNotInList", partnerLinkName, partnerLinkName); + } else { + // Set the type of the partnerLink to "service" if not already + // set... + if (!partnerLink.isSCATyped()) + partnerLink.setAsReference(partnerLinkName); + } // endif + } // end processPartnerLinkAsReference + + /** + * Finds a PartnerLink by name from a List of PartnerLinks returns null if + * there is no partnerLink with a matching name - returns the PartnerLink + * with a matching name + * + * @param partnerLinks + * @param partnerLinkName + */ + private BPELPartnerLinkElement findPartnerLinkByName(List partnerLinks, String partnerLinkName) { + // Scan the list looking for a partner link with the supplied name + Iterator it = partnerLinks.iterator(); + while (it.hasNext()) { + BPELPartnerLinkElement thePartnerLink = it.next(); + if (thePartnerLink.getName().equals(partnerLinkName)) + return thePartnerLink; + } + return null; + } // end method findPartnerLinkByName + + /** + * Report a warning. + * + * @param problems + * @param message + * @param model + */ + private void warning(String message, Object model, Object... messageParameters) { + if (monitor != null) { + Problem problem = new ProblemImpl(this.getClass().getName(), "impl-bpel-validation-messages", Severity.WARNING, model, message, (Object[])messageParameters); + monitor.problem(problem); + } + } + + /** + * Report a error. + * + * @param problems + * @param message + * @param model + */ + private void error(String message, Object model, Object... messageParameters) { + if (monitor != null) { + Problem problem = new ProblemImpl(this.getClass().getName(), "impl-bpel-validation-messages", Severity.ERROR, model, message, (Object[])messageParameters); + monitor.problem(problem); + } + } + + /** + * Report a exception. + * + * @param problems + * @param message + * @param model + */ + private void error(String message, Object model, Exception ex) { + if (monitor != null) { + Problem problem = new ProblemImpl(this.getClass().getName(), "impl-bpel-validation-messages", Severity.ERROR, model, message, ex); + monitor.problem(problem); + } + } +} diff --git a/java/sca/modules/implementation-bpel/src/main/java/org/apache/tuscany/sca/implementation/bpel/xml/BPELImplementationProcessor.java b/java/sca/modules/implementation-bpel/src/main/java/org/apache/tuscany/sca/implementation/bpel/xml/BPELImplementationProcessor.java new file mode 100644 index 0000000000..cf7b5ef36a --- /dev/null +++ b/java/sca/modules/implementation-bpel/src/main/java/org/apache/tuscany/sca/implementation/bpel/xml/BPELImplementationProcessor.java @@ -0,0 +1,520 @@ +/* + * 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.implementation.bpel.xml; + +import static javax.xml.stream.XMLStreamConstants.END_ELEMENT; + +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.wsdl.PortType; +import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; +import javax.xml.stream.XMLStreamWriter; + +import org.apache.tuscany.sca.assembly.AssemblyFactory; +import org.apache.tuscany.sca.assembly.ComponentType; +import org.apache.tuscany.sca.assembly.Multiplicity; +import org.apache.tuscany.sca.assembly.Property; +import org.apache.tuscany.sca.assembly.Reference; +import org.apache.tuscany.sca.assembly.Service; +import org.apache.tuscany.sca.assembly.builder.impl.ProblemImpl; +import org.apache.tuscany.sca.assembly.xml.Constants; +import org.apache.tuscany.sca.contribution.ModelFactoryExtensionPoint; +import org.apache.tuscany.sca.contribution.processor.BaseStAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.contribution.service.ContributionReadException; +import org.apache.tuscany.sca.contribution.service.ContributionResolveException; +import org.apache.tuscany.sca.contribution.service.ContributionWriteException; +import org.apache.tuscany.sca.databinding.xml.DOMDataBinding; +import org.apache.tuscany.sca.implementation.bpel.BPELFactory; +import org.apache.tuscany.sca.implementation.bpel.BPELImplementation; +import org.apache.tuscany.sca.implementation.bpel.BPELProcessDefinition; +import org.apache.tuscany.sca.interfacedef.wsdl.WSDLFactory; +import org.apache.tuscany.sca.interfacedef.wsdl.WSDLInterface; +import org.apache.tuscany.sca.interfacedef.wsdl.WSDLInterfaceContract; +import org.apache.tuscany.sca.monitor.Monitor; +import org.apache.tuscany.sca.monitor.Problem; +import org.apache.tuscany.sca.monitor.Problem.Severity; + +/** + * Implements a StAX artifact processor for BPEL implementations. + * + * The artifact processor is responsible for processing + * elements in SCA assembly XML composite files and populating the BPEL + * implementation model, resolving its references to other artifacts in the SCA + * contribution, and optionally write the model back to SCA assembly XML. + * + * @version $Rev$ $Date$ + */ +public class BPELImplementationProcessor extends BaseStAXArtifactProcessor implements StAXArtifactProcessor { + private static final String PROCESS = "process"; + private static final String IMPLEMENTATION_BPEL = "implementation.bpel"; + private static final QName IMPLEMENTATION_BPEL_QNAME = new QName(Constants.SCA10_NS, IMPLEMENTATION_BPEL); + + private AssemblyFactory assemblyFactory; + private BPELFactory bpelFactory; + private WSDLFactory wsdlFactory; + private Monitor monitor; + + public BPELImplementationProcessor(ModelFactoryExtensionPoint modelFactories, Monitor monitor) { + this.assemblyFactory = modelFactories.getFactory(AssemblyFactory.class); + this.wsdlFactory = modelFactories.getFactory(WSDLFactory.class); + this.bpelFactory = modelFactories.getFactory(BPELFactory.class); + this.monitor = monitor; + } + + public QName getArtifactType() { + // Returns the QName of the XML element processed by this processor + return IMPLEMENTATION_BPEL_QNAME; + } + + public Class getModelType() { + // Returns the type of model processed by this processor + return BPELImplementation.class; + } + + public BPELImplementation read(XMLStreamReader reader) throws ContributionReadException, XMLStreamException { + assert IMPLEMENTATION_BPEL_QNAME.equals(reader.getName()); + + // Read an element + BPELImplementation implementation = null; + + // Read the process attribute. + QName process = getAttributeValueNS(reader, PROCESS); + if (process == null) { + return implementation; + } + + // Create and initialize the BPEL implementation model + implementation = bpelFactory.createBPELImplementation(); + implementation.setProcess(process); + implementation.setUnresolved(true); + + // Skip to end element + while (reader.hasNext()) { + if (reader.next() == END_ELEMENT && IMPLEMENTATION_BPEL_QNAME.equals(reader.getName())) { + break; + } + } + + return implementation; + } + + public void resolve(BPELImplementation implementation, ModelResolver resolver) throws ContributionResolveException { + + if( implementation != null && implementation.isUnresolved()) + { + BPELProcessDefinition processDefinition = resolveBPELProcessDefinition(implementation, resolver); + if(processDefinition.isUnresolved()) { + error("BPELProcessNotFound", implementation, processDefinition.getName()); + } else { + implementation.setProcessDefinition(processDefinition); + + // Get the component type from the process definition + generateComponentType( implementation ); + + //resolve component type + mergeComponentType(resolver, implementation); + + //set current implementation resolved + implementation.setUnresolved(false); + } + } + + } // end resolve + + public void write( BPELImplementation bpelImplementation, + XMLStreamWriter writer ) throws ContributionWriteException, XMLStreamException { + //FIXME Deal with policy processing... + // Write + // policyProcessor.writePolicyPrefixes(bpelImplementation, writer); + writer.writeStartElement(Constants.SCA10_NS, IMPLEMENTATION_BPEL); + // policyProcessor.writePolicyAttributes(bpelImplementation, writer); + + if (bpelImplementation.getProcess() != null) { + writer.writeAttribute(PROCESS, bpelImplementation.getProcess().toString() ); + } + + writer.writeEndElement(); + + } // end write + + private BPELProcessDefinition resolveBPELProcessDefinition(BPELImplementation impl, ModelResolver resolver) throws ContributionResolveException { + QName processName = impl.getProcess(); + BPELProcessDefinition processDefinition = this.bpelFactory.createBPELProcessDefinition(); + processDefinition.setName(processName); + processDefinition.setUnresolved(true); + + return resolver.resolveModel(BPELProcessDefinition.class, processDefinition); + } // end resolveBPELProcessDefinition + + /** + * Calculates the component type of the supplied implementation and attaches it to the + * implementation. + * + * @param impl + * @throws ContributionResolveException + */ + private void generateComponentType(BPELImplementation impl) throws ContributionResolveException { + + // Create a ComponentType and mark it unresolved + ComponentType componentType = assemblyFactory.createComponentType(); + componentType.setUnresolved(true); + impl.setComponentType(componentType); + + // Each partner link in the process represents either a service or a + // reference + // - or both, in the sense of involving a callback + BPELProcessDefinition theProcess = impl.getProcessDefinition(); + List partnerLinks = theProcess.getPartnerLinks(); + + for (BPELPartnerLinkElement pLink : partnerLinks) { + + // check that the partner link has been designated as service or + // reference in SCA terms + if (pLink.isSCATyped()) { + String SCAName = pLink.getSCAName(); + if (pLink.querySCAType().equals("reference")) { + componentType.getReferences().add(generateReference(SCAName, pLink.getMyRolePortType(), pLink.getPartnerRolePortType(), theProcess.getInterfaces())); + } else { + componentType.getServices().add(generateService(SCAName, pLink.getMyRolePortType(), pLink.getPartnerRolePortType(), theProcess.getInterfaces())); + } // end if + } // end if + } // end for + + } // end getComponentType + + /** + * Create an SCA reference for a partnerLink + * @param name - name of the reference + * @param myRolePT - partner link type of myRole + * @param partnerRolePT - partner link type of partnerRole + * @param theInterfaces - list of WSDL interfaces associated with the BPEL process + * @return + */ + private Reference generateReference( String name, PortType myRolePT, + PortType partnerRolePT, Collection theInterfaces) throws ContributionResolveException { + + Reference reference = assemblyFactory.createReference(); + WSDLInterfaceContract interfaceContract = wsdlFactory.createWSDLInterfaceContract(); + reference.setInterfaceContract(interfaceContract); + + // Establish whether there is just a call interface or a call + callback + // interface + PortType callPT = null; + PortType callbackPT = null; + if (myRolePT != null) { + callPT = myRolePT; + // If the 2 port types are not the same one, there is a callback... + if (partnerRolePT != null) { + if (!myRolePT.getQName().equals(partnerRolePT.getQName())) { + callbackPT = partnerRolePT; + } // end if + } // end if + } else if (partnerRolePT != null) { + callPT = partnerRolePT; + } // end if + + // No interfaces mean an error + if (callPT == null && callbackPT == null) { + error("MyRolePartnerRoleNull", theInterfaces); + } // end if + + // Set the name of the reference to the supplied name and the + // multiplicity of the reference + // to 1..1 + // TODO: support other multiplicities + reference.setName(name); + reference.setMultiplicity(Multiplicity.ONE_ONE); + + if (callPT != null) { + // Set the call interface and, if present, the callback interface + WSDLInterface callInterface = null; + for (WSDLInterface anInterface : theInterfaces) { + if (anInterface.getPortType().getQName().equals(callPT.getQName())) + callInterface = anInterface; + } // end for + if (callInterface == null) { + error("NoInterfaceForPortType", theInterfaces, callPT.getQName().toString()); + } else + reference.getInterfaceContract().setInterface(callInterface); + } + + // There is a callback if the partner role is not null and if the + // partner role port type + // is not the same as the port type for my role + if (callbackPT != null) { + WSDLInterface callbackInterface = null; + for (WSDLInterface anInterface : theInterfaces) { + if (anInterface.getPortType().getQName().equals(callbackPT.getQName())) + callbackInterface = anInterface; + } // end for + if (callbackInterface == null) { + error("NoInterfaceForPortType", theInterfaces, callbackPT.getQName().toString()); + } else + reference.getInterfaceContract().setCallbackInterface(callbackInterface); + } // end if + + return reference; + } // end generateReference + + /** + * Create an SCA service for a partnerLink + * @param name - name of the reference + * @param myRolePT - partner link type of myRole + * @param partnerRolePT - partner link type of partnerRole + * @param theInterfaces - list of WSDL interfaces associated with the BPEL process + * @return + */ + private Service generateService( String name, PortType myRolePT, + PortType partnerRolePT, Collection theInterfaces ) + throws ContributionResolveException { + Service service = assemblyFactory.createService(); + WSDLInterfaceContract interfaceContract = wsdlFactory.createWSDLInterfaceContract(); + service.setInterfaceContract(interfaceContract); + + // Set the name of the service to the supplied name + service.setName(name); + + // Establish whether there is just a call interface or a call + callback + // interface + PortType callPT = null; + PortType callbackPT = null; + if (myRolePT != null) { + callPT = myRolePT; + // If the 2 port types are not the same one, there is a callback... + if (partnerRolePT != null) { + if (!myRolePT.getQName().equals(partnerRolePT.getQName())) { + callbackPT = partnerRolePT; + } // end if + } // end if + } else if (partnerRolePT != null) { + callPT = partnerRolePT; + } // end if + + // No interfaces mean an error + if (callPT == null && callbackPT == null) { + error("MyRolePartnerRoleNull", theInterfaces); + } // end if + + if (callPT != null) { + // Set the call interface and, if present, the callback interface + WSDLInterface callInterface = null; + for (WSDLInterface anInterface : theInterfaces) { + if (anInterface.getPortType().getQName().equals(callPT.getQName())) + callInterface = anInterface; + } // end for + if (callInterface == null) { + error("NoInterfaceForPortType", theInterfaces, callPT.getQName().toString()); + } else + service.getInterfaceContract().setInterface(callInterface); + } // end if + + // There is a callback if the partner role is not null and if the + // partner role port type + // is not the same as the port type for my role + if (callbackPT != null) { + WSDLInterface callbackInterface = null; + for (WSDLInterface anInterface : theInterfaces) { + if (anInterface.getPortType().getQName().equals(callbackPT.getQName())) + callbackInterface = anInterface; + } // end for + if (callbackInterface == null) { + error("NoInterfaceForPortType", theInterfaces, callbackPT.getQName().toString()); + } else + service.getInterfaceContract().setCallbackInterface(callbackInterface); + } // end if + + return service; + } // end generateService + + /** + * Merge the componentType from introspection and from external file + * + * Note the setting of the DataBinding for both Services and References to DOM, since this is + * the data format expected by the ODE BPEL implementation code. + * + * @param resolver + * @param impl + */ + private void mergeComponentType(ModelResolver resolver, BPELImplementation impl) { + + // Load the component type from a component type file, if any + ComponentType componentType = getComponentType(resolver, impl); + if (componentType != null && !componentType.isUnresolved()) { + + // References... + Map refMap = new HashMap(); + for (Reference reference : componentType.getReferences()) { + reference.getInterfaceContract().getInterface().resetDataBinding(DOMDataBinding.NAME); + refMap.put(reference.getName(), reference); + } // end for + + // For the present, overwrite anything arising from the component + // type sidefile if + // equivalent services are defined in the implementation. + // TODO - a more careful merge must be done, using the + // implementation introspection data + // as the master but adding any additional and non-conflicting + // information from the + // sidefile + for (Reference ref : impl.getReferences()) { + ref.getInterfaceContract().getInterface().resetDataBinding(DOMDataBinding.NAME); + refMap.put(ref.getName(), ref); + } // end for + + impl.getReferences().clear(); + impl.getReferences().addAll(refMap.values()); + + // Services..... + Map serviceMap = new HashMap(); + for (Service service : componentType.getServices()) { + service.getInterfaceContract().getInterface().resetDataBinding(DOMDataBinding.NAME); + serviceMap.put(service.getName(), service); + } // end for + + // For the present, overwrite anything arising from the component + // type sidefile if + // equivalent services are defined in the implementation. + // TODO - a more careful merge must be done, using the + // implementation introspection data + // as the master but adding any additional and non-conflicting + // information from the + // sidefile + for (Service svc : impl.getServices()) { + svc.getInterfaceContract().getInterface().resetDataBinding(DOMDataBinding.NAME); + serviceMap.put(svc.getName(), svc); + } // end for + + impl.getServices().clear(); + impl.getServices().addAll(serviceMap.values()); + + // Properties + Map propMap = new HashMap(); + for (Property property : componentType.getProperties()) { + propMap.put(property.getName(), property); + } // end for + + // A simple overwrite of any equivalent properties from the + // component type sidefile + for (Property prop : impl.getProperties()) { + propMap.put(prop.getName(), prop); + } + } + } + + + /** + * Find the componentType side file based on the BPEL implementation artifact + * @param resolver + * @param impl + * @return + */ + private ComponentType getComponentType(ModelResolver resolver, BPELImplementation impl) { + String bpelProcessURI = impl.getProcessDefinition().getURI().toString(); + + // Get the component type definition contained in the componentType file, if any + String componentTypeURI = bpelProcessURI.replace(".bpel", ".componentType"); + ComponentType componentType = assemblyFactory.createComponentType(); + componentType.setUnresolved(true); + componentType.setURI(componentTypeURI); + componentType = resolver.resolveModel(ComponentType.class, componentType); + if (!componentType.isUnresolved()) { + return componentType; + } + return null; + } // end getComponentType + + /** + * Returns a QName from its string representation in a named attribute of an XML element + * supplied in an XMLStreamReader + * + * QName attributes of an XML element (such as BPEL process) is presented in one of + * two alternative formats: + * 1) In the form of a local name with a prefix, with the prefix referencing a namespace + * URI declaration elsewhere in the composite (typically on the composite element) + * + * ie: nms:SomeName + * xmlns:nms="http://example.com/somenamespace" + * + * 2) In the XML Namespaces recommendation format (see http://jclark.com/xml/xmlns.htm ) + * where the namespace URI and the local name are encoded into a single string, with the + * namespace URI enclosed between a pair of braces {...} + * + * ie: {http://example.com/somenamespace}SomeName + */ + private QName getAttributeValueNS(XMLStreamReader reader, String attribute) { + String fullValue = reader.getAttributeValue(null, attribute); + if (fullValue == null) { + error("AttributeProcessMissing", reader); + return null; + } + + // Deal with the attribute in the XML Namespaces recommendation format + // - trim off any leading/trailing spaces and check that the first + // character is '{' + if (fullValue.trim().charAt(0) == '{') { + try { + // Attempt conversion to a QName object + QName theProcess = QName.valueOf(fullValue); + return theProcess; + } catch (IllegalArgumentException e) { + // This exception happens if the attribute begins with '{' but + // doesn't conform + // to the XML Namespaces recommendation format + error("AttributeWithoutNamespace", reader, attribute, fullValue); + return null; + } + } // endif + + // Deal with the attribute in the local name + prefix format + if (fullValue.indexOf(":") < 0) { + error("AttributeWithoutPrefix", reader, attribute, fullValue); + return null; + } + String prefix = fullValue.substring(0, fullValue.indexOf(":")); + String name = fullValue.substring(fullValue.indexOf(":") + 1); + String nsUri = reader.getNamespaceContext().getNamespaceURI(prefix); + if (nsUri == null) { + error("AttributeUnrecognizedNamespace", reader, attribute, fullValue); + return null; + } + return new QName(nsUri, name, prefix); + } + + /** + * Report a error. + * + * @param problems + * @param message + * @param model + */ + private void error(String message, Object model, Object... messageParameters) { + if (monitor != null) { + Problem problem = new ProblemImpl(this.getClass().getName(), "impl-bpel-validation-messages", Severity.ERROR, model, message, (Object[])messageParameters); + monitor.problem(problem); + } + } + +} diff --git a/java/sca/modules/implementation-bpel/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor b/java/sca/modules/implementation-bpel/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor index 4763c69768..03ef3e3a7f 100644 --- a/java/sca/modules/implementation-bpel/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor +++ b/java/sca/modules/implementation-bpel/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor @@ -16,4 +16,4 @@ # under the License. # Implementation class for the artifact processor extension -org.apache.tuscany.sca.implementation.bpel.impl.BPELImplementationProcessor;qname=http://www.osoa.org/xmlns/sca/1.0#implementation.bpel,model=org.apache.tuscany.sca.implementation.bpel.BPELImplementation \ No newline at end of file +org.apache.tuscany.sca.implementation.bpel.xml.BPELImplementationProcessor;qname=http://www.osoa.org/xmlns/sca/1.0#implementation.bpel,model=org.apache.tuscany.sca.implementation.bpel.BPELImplementation \ No newline at end of file diff --git a/java/sca/modules/implementation-bpel/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.URLArtifactProcessor b/java/sca/modules/implementation-bpel/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.URLArtifactProcessor index 066fdc864b..f6fd442f56 100644 --- a/java/sca/modules/implementation-bpel/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.URLArtifactProcessor +++ b/java/sca/modules/implementation-bpel/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.URLArtifactProcessor @@ -16,4 +16,4 @@ # under the License. # Implementation class for the artifact processor extension -org.apache.tuscany.sca.implementation.bpel.impl.BPELDocumentProcessor;type=.bpel,model=org.apache.tuscany.sca.implementation.bpel.BPELProcessDefinition \ No newline at end of file +org.apache.tuscany.sca.implementation.bpel.xml.BPELDocumentProcessor;type=.bpel,model=org.apache.tuscany.sca.implementation.bpel.BPELProcessDefinition \ No newline at end of file diff --git a/java/sca/modules/implementation-bpel/src/main/resources/META-INF/services/org.apache.tuscany.sca.implementation.bpel.BPELFactory b/java/sca/modules/implementation-bpel/src/main/resources/META-INF/services/org.apache.tuscany.sca.implementation.bpel.BPELFactory index 53ffd1b72a..4276e938f1 100644 --- a/java/sca/modules/implementation-bpel/src/main/resources/META-INF/services/org.apache.tuscany.sca.implementation.bpel.BPELFactory +++ b/java/sca/modules/implementation-bpel/src/main/resources/META-INF/services/org.apache.tuscany.sca.implementation.bpel.BPELFactory @@ -15,4 +15,4 @@ # specific language governing permissions and limitations # under the License. -org.apache.tuscany.sca.implementation.bpel.DefaultBPELFactory +org.apache.tuscany.sca.implementation.bpel.impl.BPELFactoryImpl -- cgit v1.2.3