bpelreader.java

来自「ejb3 java session bean」· Java 代码 · 共 1,370 行 · 第 1/4 页

JAVA
1,370
字号
/* * JBoss, Home of Professional Open Source * Copyright 2005, JBoss Inc., and individual contributors as indicated * by the @authors tag. * * This is free software; you can redistribute it and/or modify it * under the terms of the JBPM BPEL PUBLIC LICENSE AGREEMENT as * published by JBoss Inc.; either version 1.0 of the License, or * (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */package org.jbpm.bpel.xml;import java.io.ByteArrayInputStream;import java.io.IOException;import java.net.URI;import java.net.URISyntaxException;import java.util.Arrays;import java.util.Collections;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;import java.util.Map.Entry;import javax.wsdl.Definition;import javax.wsdl.Message;import javax.wsdl.Operation;import javax.wsdl.OperationType;import javax.wsdl.PortType;import javax.wsdl.WSDLException;import javax.wsdl.xml.WSDLReader;import javax.xml.namespace.QName;import javax.xml.parsers.DocumentBuilder;import javax.xml.transform.Templates;import javax.xml.transform.Transformer;import javax.xml.transform.TransformerException;import javax.xml.transform.dom.DOMResult;import javax.xml.transform.dom.DOMSource;import org.apache.commons.collections.Predicate;import org.apache.commons.collections.iterators.FilterIterator;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.w3c.dom.Attr;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.Node;import org.xml.sax.InputSource;import org.xml.sax.SAXException;import org.jbpm.JbpmConfiguration;import org.jbpm.bpel.BpelException;import org.jbpm.bpel.alarm.AlarmAction;import org.jbpm.bpel.graph.basic.Empty;import org.jbpm.bpel.graph.def.Activity;import org.jbpm.bpel.graph.def.BpelProcessDefinition;import org.jbpm.bpel.graph.def.CompositeActivity;import org.jbpm.bpel.graph.def.Import;import org.jbpm.bpel.graph.def.ImportDefinition;import org.jbpm.bpel.graph.scope.Catch;import org.jbpm.bpel.graph.scope.Handler;import org.jbpm.bpel.graph.scope.OnAlarm;import org.jbpm.bpel.graph.scope.OnEvent;import org.jbpm.bpel.graph.scope.Scope;import org.jbpm.bpel.integration.def.Correlation;import org.jbpm.bpel.integration.def.CorrelationSetDefinition;import org.jbpm.bpel.integration.def.Correlations;import org.jbpm.bpel.integration.def.PartnerLinkDefinition;import org.jbpm.bpel.integration.def.ReceiveAction;import org.jbpm.bpel.integration.def.Correlation.Initiate;import org.jbpm.bpel.sublang.def.Expression;import org.jbpm.bpel.sublang.def.PropertyQuery;import org.jbpm.bpel.variable.def.ElementType;import org.jbpm.bpel.variable.def.MessageType;import org.jbpm.bpel.variable.def.SchemaType;import org.jbpm.bpel.variable.def.VariableDefinition;import org.jbpm.bpel.variable.def.VariableType;import org.jbpm.bpel.wsdl.PartnerLinkType;import org.jbpm.bpel.wsdl.Property;import org.jbpm.bpel.wsdl.PropertyAlias;import org.jbpm.bpel.wsdl.PartnerLinkType.Role;import org.jbpm.bpel.wsdl.xml.WsdlConstants;import org.jbpm.bpel.wsdl.xml.WsdlUtil;import org.jbpm.bpel.xml.util.DatatypeUtil;import org.jbpm.bpel.xml.util.NodeIterator;import org.jbpm.bpel.xml.util.XmlUtil;import org.jbpm.jpdl.par.ProcessArchive;import org.jbpm.jpdl.xml.Problem;import org.jbpm.util.ClassLoaderUtil;/** * Converts a process document in XML format to a * {@linkplain BpelProcessDefinition process definition}. * @author Juan Cantu * @author Alejandro Guizar * @version $Revision: 1.34 $ $Date: 2007/11/29 10:16:30 $ */public class BpelReader {  private Map activityReaders = createActivityReaders();  private ProblemHandler problemHandler = new ProblemCounter();  public static final String RESOURCE_ACTIVITY_READERS = "resource.activity.readers";  private static final Log log = LogFactory.getLog(BpelReader.class);  private static final Map activityReaderClasses = readActivityReaderClasses();  private static Templates bpelUpgradeTemplates;  /**   * Reads a BPEL document into a process definition.   * @param processDefinition the definition to read into   * @param source an input source pointing to the BPEL document   */  public void read(BpelProcessDefinition processDefinition, InputSource source) {    DocumentBuilder documentBuilder = XmlUtil.getDocumentBuilder();    // capture parse errors in our problem handler    documentBuilder.setErrorHandler(problemHandler.asSaxErrorHandler());    // save the document location    String location = source.getSystemId();    processDefinition.setLocation(location);    try {      // parse content      Document document = documentBuilder.parse(source);      // halt on parse errors      if (problemHandler.getProblemCount() > 0)        return;      // prepare a locator of imported documents, relative to the process document location      ProcessWsdlLocator wsdlLocator = null;      if (location != null) {        try {          wsdlLocator = new ProcessWsdlLocator(new URI(location));          wsdlLocator.setProblemHandler(problemHandler);        }        catch (URISyntaxException e) {          problemHandler.add(new Problem(Problem.LEVEL_ERROR,              "source system identifier is invalid: " + location, e));        }      }      // read process definition      read(processDefinition, document.getDocumentElement(), wsdlLocator);    }    catch (SAXException e) {      problemHandler.add(new Problem(Problem.LEVEL_ERROR, "bpel document contains invalid xml: "          + location, e));    }    catch (IOException e) {      problemHandler.add(new Problem(Problem.LEVEL_ERROR, "bpel document is not readable: "          + location, e));    }    finally {      // reset error handling behavior      documentBuilder.setErrorHandler(null);    }  }  /**   * Reads a process archive containing a BPEL document into a process definition.   * @param processDefinition the definition to read into; its <code>location</code> property   * specifies the process document entry within the archive   * @param archive the archive to read   */  public void read(BpelProcessDefinition processDefinition, ProcessArchive archive) {    String location = processDefinition.getLocation();    byte[] bpelEntry = archive.getEntry(location);    // check whether the bpel document exists in the archive    if (bpelEntry == null) {      problemHandler.add(new Problem(Problem.LEVEL_ERROR, "process document entry not found"));      return;    }    DocumentBuilder documentBuilder = XmlUtil.getDocumentBuilder();    // capture parse errors in our problem handler    documentBuilder.setErrorHandler(problemHandler.asSaxErrorHandler());    try {      // parse content      Document document = documentBuilder.parse(new ByteArrayInputStream(bpelEntry));      // halt on parse errors      if (problemHandler.getProblemCount() > 0)        return;      /*       * prepare a locator of imported documents, relative to the process document location within       * the archive       */      ProcessWsdlLocator wsdlLocator = new ProcessArchiveWsdlLocator(new URI(location), archive);      wsdlLocator.setProblemHandler(problemHandler);      // read process definition      read(processDefinition, document.getDocumentElement(), wsdlLocator);    }    catch (SAXException e) {      problemHandler.add(new Problem(Problem.LEVEL_ERROR, "bpel document contains invalid xml: "          + location, e));    }    catch (IOException e) {      problemHandler.add(new Problem(Problem.LEVEL_ERROR, "bpel document is not readable: "          + location, e));    }    catch (URISyntaxException e) {      problemHandler.add(new Problem(Problem.LEVEL_ERROR,          "process definition location is invalid: " + location, e));    }    finally {      // reset error handling behavior      documentBuilder.setErrorHandler(null);    }  }  public void read(BpelProcessDefinition processDefinition, DOMSource source) {    // save the document location    String location = source.getSystemId();    processDefinition.setLocation(location);    // prepare a locator of imported documents, relative to the process document location    ProcessWsdlLocator wsdlLocator = null;    if (location != null) {      try {        wsdlLocator = new ProcessWsdlLocator(new URI(location));        wsdlLocator.setProblemHandler(problemHandler);      }      catch (URISyntaxException e) {        problemHandler.add(new Problem(Problem.LEVEL_ERROR, "source system id is invalid"));      }    }    Element processElem;    Node node = source.getNode();    switch (node.getNodeType()) {    case Node.DOCUMENT_NODE:      processElem = ((Document) node).getDocumentElement();      break;    case Node.ELEMENT_NODE:      processElem = (Element) node;      break;    default:      problemHandler.add(new Problem(Problem.LEVEL_ERROR,          "source node must be either an element or a document: " + node));      return;    }    // read process definition    read(processDefinition, processElem, wsdlLocator);  }  protected void read(BpelProcessDefinition processDefinition, Element processElem,      ProcessWsdlLocator wsdlLocator) {    // see if the bpel document requires updating    if (BpelConstants.NS_BPEL_1_1.equals(processElem.getNamespaceURI())) {      try {        // create bpel upgrader        Transformer bpelUpgrader = getBpelUpgradeTemplates().newTransformer();        // install our problem handler as transformer's error listener        bpelUpgrader.setErrorListener(problemHandler.asTraxErrorListener());        // upgrade into dom document        Document resultDocument = XmlUtil.createDocument();        bpelUpgrader.transform(new DOMSource(processElem.getOwnerDocument()), new DOMResult(            resultDocument));        processElem = resultDocument.getDocumentElement();        log.debug("upgraded bpel document: " + processDefinition.getLocation());      }      catch (TransformerException e) {        Problem problem = new Problem(Problem.LEVEL_ERROR, "bpel upgrade failed", e);        problem.setResource(processDefinition.getLocation());        problemHandler.add(problem);      }      // halt on transform errors      if (problemHandler.getProblemCount() > 0)        return;    }    // read attributes in the process element    readProcessAttributes(processElem, processDefinition);    // read imported documents    ImportDefinition importDefinition = processDefinition.getImportDefinition();    readImports(processElem, importDefinition, wsdlLocator);    // halt on import parse errors    if (problemHandler.getProblemCount() > 0)      return;    try {      // registration gets the query language from the process definition      registerPropertyAliases(importDefinition);      // finally read the global scope      readScope(processElem, processDefinition.getGlobalScope());      log.debug("read bpel document: " + processDefinition.getLocation());    }    catch (BpelException e) {      problemHandler.add(new Problem(Problem.LEVEL_ERROR, "bpel process is invalid", e));    }  }  static synchronized Templates getBpelUpgradeTemplates() throws TransformerException {    if (bpelUpgradeTemplates == null)      bpelUpgradeTemplates = XmlUtil.createTemplates(BpelReader.class.getResource("bpel-1-1-converter.xslt"));    return bpelUpgradeTemplates;  }  public void registerPropertyAliases(ImportDefinition importDefinition) {    // register property aliases in each wsdl document    List imports = importDefinition.getImports();    for (int i = 0, n = imports.size(); i < n; i++) {      Import _import = (Import) imports.get(i);      if (Import.Type.WSDL.equals(_import.getType()))        registerPropertyAliases(importDefinition, (Definition) _import.getDocument());    }  }  private void registerPropertyAliases(ImportDefinition importDefinition, Definition wsdlDef) {    BpelProcessDefinition processDefinition = (BpelProcessDefinition) importDefinition.getProcessDefinition();    // first deal with local extensibility elements    for (Iterator i = WsdlUtil.getExtensions(wsdlDef.getExtensibilityElements(),        WsdlConstants.Q_PROPERTY_ALIAS); i.hasNext();) {      PropertyAlias alias = (PropertyAlias) i.next();      // property      Property property = alias.getProperty();      if (!property.isUndefined())        importDefinition.addProperty(property);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?