1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/tools/internal/ws/processor/modeler/wsdl/ModelerUtils.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,281 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.tools.internal.ws.processor.modeler.wsdl; 1.30 + 1.31 +import com.sun.tools.internal.ws.processor.model.AbstractType; 1.32 +import com.sun.tools.internal.ws.processor.model.Block; 1.33 +import com.sun.tools.internal.ws.processor.model.ModelProperties; 1.34 +import com.sun.tools.internal.ws.processor.model.Parameter; 1.35 +import com.sun.tools.internal.ws.processor.model.java.JavaSimpleType; 1.36 +import com.sun.tools.internal.ws.processor.model.java.JavaStructureMember; 1.37 +import com.sun.tools.internal.ws.processor.model.java.JavaStructureType; 1.38 +import com.sun.tools.internal.ws.processor.model.java.JavaType; 1.39 +import com.sun.tools.internal.ws.processor.model.jaxb.*; 1.40 +import com.sun.tools.internal.ws.resources.ModelerMessages; 1.41 +import com.sun.tools.internal.ws.util.ClassNameInfo; 1.42 +import com.sun.tools.internal.ws.wscompile.AbortException; 1.43 +import com.sun.tools.internal.ws.wscompile.ErrorReceiverFilter; 1.44 +import com.sun.tools.internal.ws.wsdl.document.Message; 1.45 +import com.sun.tools.internal.ws.wsdl.document.MessagePart; 1.46 +import com.sun.tools.internal.xjc.api.S2JJAXBModel; 1.47 +import com.sun.tools.internal.xjc.api.TypeAndAnnotation; 1.48 + 1.49 +import javax.xml.namespace.QName; 1.50 +import java.util.ArrayList; 1.51 +import java.util.Iterator; 1.52 +import java.util.List; 1.53 + 1.54 +/** 1.55 + * Utilities to be used by WSDLModeler 1.56 + * 1.57 + * @author Vivek Pandey 1.58 + * 1.59 + */ 1.60 +class ModelerUtils { 1.61 + 1.62 + /** 1.63 + * This method should be called incase of wrapper style operations. This is 1.64 + * equivalent to wrapper style schema component or JAXB Mapping object. 1.65 + * 1.66 + * @param jaxbType JAXBType from which a JAXBStructured type will be created. 1.67 + * @return returns JAXBStructured type 1.68 + */ 1.69 + public static JAXBStructuredType createJAXBStructureType(JAXBType jaxbType) { 1.70 + JAXBStructuredType type = new JAXBStructuredType(jaxbType); 1.71 + type.setName(jaxbType.getName()); 1.72 + type.setJavaType(jaxbType.getJavaType()); 1.73 + return type; 1.74 + } 1.75 + 1.76 + /** 1.77 + * This method uses JAXBStructured type (wrapper style operations) and 1.78 + * unwraps it to create list of parameters. 1.79 + * 1.80 + * 1.81 + * @param jaxbType instance of JAXBType, could be JAXBStructured type. 1.82 + * @param block The Block (body/Header/Attachment) to which the created Parameter belong. 1.83 + * @return list of Parameters 1.84 + */ 1.85 + public static List<Parameter> createUnwrappedParameters(JAXBType jaxbType, 1.86 + Block block) { 1.87 + List<Parameter> paramList = new ArrayList<Parameter>(); 1.88 + JAXBStructuredType type = null; 1.89 + if (!(jaxbType instanceof JAXBStructuredType)) 1.90 + type = createJAXBStructureType(jaxbType); 1.91 + else 1.92 + type = (JAXBStructuredType) jaxbType; 1.93 + 1.94 + JavaStructureType jst = new JavaStructureType(jaxbType.getJavaType() 1.95 + .getRealName(), true, type); 1.96 + type.setJavaType(jst); 1.97 + block.setType(type); 1.98 + List memberList = jaxbType.getWrapperChildren(); 1.99 + Iterator props = memberList.iterator(); 1.100 + while (props.hasNext()) { 1.101 + JAXBProperty prop = (JAXBProperty) props.next(); 1.102 + paramList.add(createUnwrappedParameter(prop, jaxbType, block, type, 1.103 + jst)); 1.104 + } 1.105 + 1.106 + return paramList; 1.107 + } 1.108 + 1.109 + /** 1.110 + * @param prop 1.111 + * @param jaxbType 1.112 + * @param block 1.113 + * @return unwrapped parameter 1.114 + */ 1.115 + private static Parameter createUnwrappedParameter(JAXBProperty prop, 1.116 + JAXBType jaxbType, Block block, JAXBStructuredType type, 1.117 + JavaStructureType jst) { 1.118 + QName elementName = prop.getElementName(); 1.119 + JavaType javaType = new JavaSimpleType(prop.getType()); 1.120 + JAXBElementMember eType = new JAXBElementMember(elementName, jaxbType); 1.121 + JavaStructureMember jsm = new JavaStructureMember(elementName 1.122 + .getLocalPart(), javaType, eType); 1.123 + eType.setJavaStructureMember(jsm); 1.124 + jst.add(jsm); 1.125 + eType.setProperty(prop); 1.126 + type.add(eType); 1.127 + JAXBType t = new JAXBType(elementName, javaType, jaxbType 1.128 + .getJaxbMapping(), jaxbType.getJaxbModel()); 1.129 + t.setUnwrapped(true); 1.130 + Parameter parameter = createParameter(elementName.getLocalPart(), t, block); 1.131 + parameter.setEmbedded(true); 1.132 + return parameter; 1.133 + } 1.134 + 1.135 + public static List<Parameter> createRpcLitParameters(Message message, Block block, S2JJAXBModel jaxbModel, ErrorReceiverFilter errReceiver){ 1.136 + RpcLitStructure rpcStruct = (RpcLitStructure)block.getType(); 1.137 + 1.138 + List<Parameter> parameters = new ArrayList<Parameter>(); 1.139 + for(MessagePart part : message.getParts()){ 1.140 + if(!ModelerUtils.isBoundToSOAPBody(part)) 1.141 + continue; 1.142 + QName name = part.getDescriptor(); 1.143 + TypeAndAnnotation typeAndAnn = jaxbModel.getJavaType(name); 1.144 + if(typeAndAnn == null){ 1.145 + String msgQName = "{"+message.getDefining().getTargetNamespaceURI()+"}"+message.getName(); 1.146 + errReceiver.error(part.getLocator(), ModelerMessages.WSDLMODELER_RPCLIT_UNKOWNSCHEMATYPE(name.toString(), 1.147 + part.getName(), msgQName)); 1.148 + throw new AbortException(); 1.149 + } 1.150 + String type = typeAndAnn.getTypeClass().fullName(); 1.151 + type = ClassNameInfo.getGenericClass(type); 1.152 + RpcLitMember param = new RpcLitMember(new QName("", part.getName()), type); 1.153 + JavaType javaType = new JavaSimpleType(new JAXBTypeAndAnnotation(typeAndAnn)); 1.154 + param.setJavaType(javaType); 1.155 + rpcStruct.addRpcLitMember(param); 1.156 + Parameter parameter = ModelerUtils.createParameter(part.getName(), param, block); 1.157 + parameter.setEmbedded(true); 1.158 + parameters.add(parameter); 1.159 + } 1.160 + return parameters; 1.161 + } 1.162 + 1.163 + /** 1.164 + * Called for non-wrapper style operations. It returns a Parameter constructed 1.165 + * using the JAXBType and the Block. 1.166 + * 1.167 + * @param partName typically wsdl:part or any name to be given to the parameter 1.168 + * @param jaxbType type of Parameter 1.169 + * @param block Block to which the parameter belongs to 1.170 + * @return Parameter created. 1.171 + */ 1.172 + public static Parameter createParameter(String partName, AbstractType jaxbType, 1.173 + Block block) { 1.174 + Parameter parameter = new Parameter(partName, block.getEntity()); 1.175 + parameter.setProperty(ModelProperties.PROPERTY_PARAM_MESSAGE_PART_NAME, 1.176 + partName); 1.177 + parameter.setEmbedded(false); 1.178 + parameter.setType(jaxbType); 1.179 + parameter.setTypeName(jaxbType.getJavaType().getType().getName()); 1.180 + parameter.setBlock(block); 1.181 + return parameter; 1.182 + } 1.183 + 1.184 + /** 1.185 + * Get Parameter from the list of parameters. 1.186 + * 1.187 + * @param paramName 1.188 + * @param parameters 1.189 + * @return the Parameter with name paramName from parameters 1.190 + */ 1.191 + public static Parameter getParameter(String paramName, List<Parameter> parameters){ 1.192 + if(parameters == null) 1.193 + return null; 1.194 + for(Parameter param: parameters){ 1.195 + //if(param.getName().equals("_return") && paramName.equals("return") || param.getName().equals(paramName)) { 1.196 + if(param.getName().equals(paramName)){ 1.197 + return param; 1.198 + } 1.199 + } 1.200 + return null; 1.201 + } 1.202 + 1.203 + /** 1.204 + * Compares two JAXBStructures. 1.205 + * 1.206 + * @param struct1 1.207 + * @param struct2 1.208 + * @return true if struct1 and struct2 are equivalent. 1.209 + */ 1.210 + public static boolean isEquivalentLiteralStructures( 1.211 + JAXBStructuredType struct1, 1.212 + JAXBStructuredType struct2) { 1.213 + if (struct1.getElementMembersCount() != struct2.getElementMembersCount()) 1.214 + return false; 1.215 + Iterator members = struct1.getElementMembers(); 1.216 + JAXBElementMember member1; 1.217 + JavaStructureMember javaMember1, javaMember2; 1.218 + for (int i = 0; members.hasNext(); i++) { 1.219 + member1 = (JAXBElementMember)members.next(); 1.220 + javaMember1 = member1.getJavaStructureMember(); 1.221 + javaMember2 = 1.222 + ((JavaStructureType)struct2.getJavaType()).getMemberByName( 1.223 + member1.getJavaStructureMember().getName()); 1.224 + if (javaMember2.getConstructorPos() != i 1.225 + || !javaMember1.getType().equals(javaMember2.getType())) { 1.226 + return false; 1.227 + } 1.228 + } 1.229 + return false; 1.230 + } 1.231 + 1.232 + public static QName getRawTypeName(Parameter parameter) { 1.233 + String name = parameter.getName(); 1.234 + 1.235 + if (parameter.getType() instanceof JAXBType) { 1.236 + JAXBType jt = (JAXBType)parameter.getType(); 1.237 + if (jt.isUnwrappable()) { 1.238 + List<JAXBProperty> props = jt.getWrapperChildren(); 1.239 + for(JAXBProperty prop: props) { 1.240 + if (prop.getName().equals(name)) { 1.241 + return prop.getRawTypeName(); 1.242 + } 1.243 + } 1.244 + } 1.245 + } 1.246 + return null; 1.247 + } 1.248 + 1.249 + /** 1.250 + * @param part 1.251 + * @return true if part is bound to Mime content 1.252 + */ 1.253 + public static boolean isBoundToMimeContent(MessagePart part) { 1.254 + if((part != null) && part.getBindingExtensibilityElementKind() == MessagePart.WSDL_MIME_BINDING) 1.255 + return true; 1.256 + return false; 1.257 + } 1.258 + 1.259 + /** 1.260 + * @param part 1.261 + * @return true if part is bound to SOAPBody 1.262 + */ 1.263 + public static boolean isBoundToSOAPBody(MessagePart part) { 1.264 + if((part != null) && part.getBindingExtensibilityElementKind() == MessagePart.SOAP_BODY_BINDING) 1.265 + return true; 1.266 + return false; 1.267 + } 1.268 + 1.269 + /** 1.270 + * @param part 1.271 + * @return true if part is bound to SOAPHeader 1.272 + */ 1.273 + public static boolean isBoundToSOAPHeader(MessagePart part) { 1.274 + if((part != null) && part.getBindingExtensibilityElementKind() == MessagePart.SOAP_HEADER_BINDING) 1.275 + return true; 1.276 + return false; 1.277 + } 1.278 + 1.279 + public static boolean isUnbound(MessagePart part) { 1.280 + if((part != null) && part.getBindingExtensibilityElementKind() == MessagePart.PART_NOT_BOUNDED) 1.281 + return true; 1.282 + return false; 1.283 + } 1.284 +}