?? jibx2wsdl.java
字號:
/*Copyright (c) 2007, Dennis M. Sosnoski.All rights reserved.Redistribution and use in source and binary forms, with or without modification,are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of JiBX nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" ANDANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIEDWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE AREDISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FORANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ONANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THISSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/package org.jibx.ws.wsdl;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.net.URL;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;import org.jibx.binding.generator.BindingGenerator;import org.jibx.binding.generator.BindingHolder;import org.jibx.binding.generator.ClassCustom;import org.jibx.binding.generator.CustomBase;import org.jibx.binding.generator.GlobalCustom;import org.jibx.binding.generator.SchemaGenerator;import org.jibx.binding.generator.SchemaHolder;import org.jibx.binding.generator.SchemaMappingDetail;import org.jibx.binding.model.BindingElement;import org.jibx.binding.model.CollectionElement;import org.jibx.binding.model.DocumentFormatter;import org.jibx.binding.model.IClass;import org.jibx.binding.model.IClassLocator;import org.jibx.binding.model.MappingElement;import org.jibx.binding.model.ValidationContext;import org.jibx.binding.model.ValidationProblem;import org.jibx.runtime.BindingDirectory;import org.jibx.runtime.IBindingFactory;import org.jibx.runtime.IMarshallable;import org.jibx.runtime.IMarshallingContext;import org.jibx.runtime.JiBXException;import org.jibx.runtime.QName;import org.jibx.schema.IComponent;import org.jibx.schema.elements.AnnotationElement;import org.jibx.schema.elements.ComplexTypeElement;import org.jibx.schema.elements.DocumentationElement;import org.jibx.schema.elements.ElementElement;import org.jibx.schema.elements.SchemaElement;import org.jibx.schema.elements.SequenceElement;import org.jibx.schema.types.Count;import org.jibx.util.InsertionOrderedSet;import org.jibx.util.Types;import org.w3c.dom.Node;/** * Start-from-code WSDL generator using JiBX data binding. This starts from one * or more service classes, each with one or more methods to be exposed as * service operations, and generates complete bindings and WSDL for the * services. * * @author Dennis M. Sosnoski */public class Jibx2Wsdl{ /** Customizations information. */ private final GlobalCustom m_global; /** WSDL customizations. */ private final WsdlCustom m_wsdlCustom; /** Binding generator. */ private final BindingGenerator m_bindingGenerator; /** Schema generator. */ private final SchemaGenerator m_schemaGenerator; /** Map from schema namespace URIs to schema holders. */ private final Map m_uriSchemaMap; /** Map from fully qualified class name to schema type name. */ private Map m_classTypeMap; /** Locator for class information (<code>null</code> if none). */ private final IClassLocator m_locator; /** Document used for annotations (<code>null</code> if none). */ private final DocumentFormatter m_formatter; /** * Constructor. * * @param loc class locator * @param global customizations information * @param wsdl WSDL customizations information */ private Jibx2Wsdl(IClassLocator loc, GlobalCustom global, WsdlCustom wsdl) { m_locator = loc; m_global = global; m_bindingGenerator = new BindingGenerator(global); m_schemaGenerator = new SchemaGenerator(loc, global); m_uriSchemaMap = new HashMap(); m_formatter = new DocumentFormatter(); m_wsdlCustom = wsdl; } /** * Get the qualified name used for an abstract mapping. This throws an * exception if the qualified name is not found. * * @param type * @param mapping * @return qualified name */ private QName getMappingQName(String type, MappingElement mapping) { SchemaMappingDetail detail = m_schemaGenerator.getMappingDetail(mapping); if (detail == null) { throw new IllegalStateException("No mapping found for type " + type); } else if (detail.isType()) { return detail.getTypeName(); } else { throw new IllegalStateException("Need abstract mapping for type " + type); } } /** * Build an element representing a parameter or return value. * * @param parm * @param typemap map from parameterized type to abstract mapping name * @return constructed element */ private ElementElement buildValueElement(ValueCustom parm, Map typemap) { // create the basic element definition ElementElement elem = new ElementElement(); if (!parm.isRequired()) { elem.setMinOccurs(Count.COUNT_ZERO); } String type = parm.getType(); if (type.endsWith("[]")) { elem.setMaxOccurs(Count.COUNT_UNBOUNDED); } // check type or reference for element boolean isref = false; String ptype = parm.getBoundType(); QName tname = (QName)typemap.get(ptype); if (tname == null) { tname = Types.schemaType(ptype); if (tname == null) { String usetype = ptype.endsWith(">") ? type : ptype; BindingGenerator.MappingDetail detail = m_bindingGenerator.getMappingDetail(usetype); if (detail == null) { throw new IllegalStateException("No mapping found for type " + usetype); } else if (detail.isExtended()) { elem.setRef(detail.getElementQName()); isref = true; } else { MappingElement mapping = detail.getAbstractMapping(); tname = mapping.getTypeQName(); if (tname == null) { tname = getMappingQName(usetype, mapping); } } } } if (!isref) { // set element type and name elem.setType(tname); String ename = parm.getElementName(); if (ename == null) { ename = tname.getName(); } elem.setName(ename); } // add documentation if available List nodes = parm.getDocumentation(); if (nodes != null) { AnnotationElement anno = new AnnotationElement(); DocumentationElement doc = new DocumentationElement(); for (Iterator iter = nodes.iterator(); iter.hasNext();) { Node node = (Node)iter.next(); doc.addContent(node); } anno.getItemsList().add(doc); elem.setAnnotation(anno); } return elem; } /** * Add reference defined by element to schema. This finds the namespace of * the type or element reference used by the provided element, and adds that * namespace to the schema references. * * @param elem * @param holder */ private void addSchemaReference(ElementElement elem, SchemaHolder holder) { QName qname = elem.getType(); if (qname == null) { qname = elem.getRef(); } if (qname != null) { String rns = qname.getUri(); if (!holder.getNamespace().equals(rns) && !IComponent.SCHEMA_NAMESPACE.equals(rns)) { holder.addReference((SchemaHolder)m_uriSchemaMap.get(rns)); } } } /** * Build WSDL for service. * * @param service * @param typemap map from parameterized type to abstract mapping name * @return constructed WSDL definitions */ private Definitions buildWSDL(ServiceCustom service, Map typemap) { // initialize root object of definition String wns = service.getWsdlNamespace(); String sns = service.getNamespace(); String spfx = wns.equals(sns) ? "tns" : "sns"; Definitions def = new Definitions(service.getPortTypeName(), service.getBindingName(), service.getServiceName(), service.getPortName(), "tns", wns, spfx, sns); def.setServiceLocation(service.getServiceAddress()); // add service documentation if available IClass info = m_locator.getClassInfo(service.getClassName()); if (info != null) { List nodes = m_formatter.docToNodes(info.getJavaDoc()); def.setPortTypeDocumentation(nodes); } // find or create the schema element and namespace SchemaHolder holder = (SchemaHolder)m_uriSchemaMap.get(sns); if (holder == null) { holder = new SchemaHolder(sns); } SchemaElement schema = holder.getSchema(); def.getSchemas().add(schema); // process messages and operations used by service ArrayList ops = service.getOperations(); Map fltmap = new HashMap(); for (int i = 0; i < ops.size(); i++) { // get information for operation OperationCustom odef = (OperationCustom)ops.get(i); String oname = odef.getOperationName(); Operation op = new Operation(oname); op.setDocumentation(odef.getDocumentation()); op.setSoapAction(odef.getSoapAction()); // generate input message information QName qname = new QName(sns, odef.getRequestWrapperName()); MessagePart part = new MessagePart("part", qname); Message msg = new Message(odef.getRequestMessageName(), part); op.addInputMessage(msg); def.addMessage(msg); // add corresponding schema definition to schema SequenceElement seq = new SequenceElement(); ArrayList parms = odef.getParameters(); for (int j = 0; j < parms.size(); j++) { ValueCustom parm = (ValueCustom)parms.get(j); ElementElement pelem = buildValueElement(parm, typemap); seq.getParticleList().add(pelem); addSchemaReference(pelem, holder); } ComplexTypeElement tdef = new ComplexTypeElement(); tdef.setContentDefinition(seq); ElementElement elem = new ElementElement(); elem.setName(odef.getRequestWrapperName()); elem.setInlineType(tdef); schema.getTopLevelChildren().add(elem); // generate output message information qname = new QName(sns, odef.getResponseWrapperName()); part = new MessagePart("part", qname); msg = new Message(odef.getResponseMessageName(), part); op.addOutputMessage(msg); def.addMessage(msg); // add corresponding schema definition to schema seq = new SequenceElement(); ValueCustom ret = odef.getReturn(); if (!"void".equals(ret.getType())) { ElementElement relem = buildValueElement(ret, typemap); seq.getParticleList().add(relem); addSchemaReference(relem, holder); } tdef = new ComplexTypeElement(); tdef.setContentDefinition(seq); elem = new ElementElement(); elem.setName(odef.getResponseWrapperName()); elem.setInlineType(tdef); schema.getTopLevelChildren().add(elem); // process fault message(s) for operation ArrayList thrws = odef.getThrows(); for (int j = 0; j < thrws.size(); j++) { ThrowsCustom thrw = (ThrowsCustom)thrws.get(j); String type = thrw.getType(); msg = (Message)fltmap.get(type);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -