src/share/jaxws_classes/com/sun/tools/internal/ws/wscompile/WsgenOptions.java

Sat, 07 Nov 2020 10:30:02 +0800

author
aoqi
date
Sat, 07 Nov 2020 10:30:02 +0800
changeset 1921
7166269ef0f1
parent 637
9c07ef4934dd
permissions
-rw-r--r--

Added tag mips-jdk8u275-b01 for changeset fdbe50121f48

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.tools.internal.ws.wscompile;
aoqi@0 27
aoqi@0 28 import com.sun.tools.internal.ws.api.WsgenExtension;
aoqi@0 29 import com.sun.tools.internal.ws.api.WsgenProtocol;
aoqi@0 30 import com.sun.tools.internal.ws.resources.WscompileMessages;
aoqi@0 31 import com.sun.xml.internal.ws.api.BindingID;
aoqi@0 32 import com.sun.xml.internal.ws.binding.SOAPBindingImpl;
aoqi@0 33 import com.sun.xml.internal.ws.util.ServiceFinder;
aoqi@0 34
aoqi@0 35 import javax.jws.WebService;
aoqi@0 36 import javax.xml.namespace.QName;
aoqi@0 37 import java.io.File;
aoqi@0 38 import java.util.ArrayList;
aoqi@0 39 import java.util.LinkedHashMap;
aoqi@0 40 import java.util.LinkedHashSet;
aoqi@0 41 import java.util.List;
aoqi@0 42 import java.util.Map;
aoqi@0 43 import java.util.Set;
aoqi@0 44
aoqi@0 45 /**
aoqi@0 46 * @author Vivek Pandey
aoqi@0 47 */
aoqi@0 48 public class WsgenOptions extends Options {
aoqi@0 49 /**
aoqi@0 50 * -servicename
aoqi@0 51 */
aoqi@0 52 public QName serviceName;
aoqi@0 53
aoqi@0 54 /**
aoqi@0 55 * -portname
aoqi@0 56 */
aoqi@0 57 public QName portName;
aoqi@0 58
aoqi@0 59 /**
aoqi@0 60 * -r
aoqi@0 61 */
aoqi@0 62 public File nonclassDestDir;
aoqi@0 63
aoqi@0 64
aoqi@0 65 /**
aoqi@0 66 * -wsdl
aoqi@0 67 */
aoqi@0 68 public boolean genWsdl;
aoqi@0 69
aoqi@0 70 /**
aoqi@0 71 * -inlineSchemas
aoqi@0 72 */
aoqi@0 73 public boolean inlineSchemas;
aoqi@0 74
aoqi@0 75 /**
aoqi@0 76 * protocol value
aoqi@0 77 */
aoqi@0 78 public String protocol = "soap1.1";
aoqi@0 79
aoqi@0 80 public Set<String> protocols = new LinkedHashSet<String>();
aoqi@0 81 public Map<String, String> nonstdProtocols = new LinkedHashMap<String, String>();
aoqi@0 82
aoqi@0 83 /**
aoqi@0 84 * -XwsgenReport
aoqi@0 85 */
aoqi@0 86 public File wsgenReport;
aoqi@0 87
aoqi@0 88 /**
aoqi@0 89 * -Xdonotoverwrite
aoqi@0 90 */
aoqi@0 91 public boolean doNotOverWrite;
aoqi@0 92
aoqi@0 93 /**
aoqi@0 94 * Tells if user specified a specific protocol
aoqi@0 95 */
aoqi@0 96 public boolean protocolSet = false;
aoqi@0 97
aoqi@0 98 /**
aoqi@0 99 * <code>-x file1 -x file2 ...<code/><br />
aoqi@0 100 * Files to be parsed to get classes' metadata in addition/instead of using annotations and reflection API
aoqi@0 101 */
aoqi@0 102 public List<String> externalMetadataFiles = new ArrayList<String>();
aoqi@0 103
aoqi@0 104 private static final String SERVICENAME_OPTION = "-servicename";
aoqi@0 105 private static final String PORTNAME_OPTION = "-portname";
aoqi@0 106 private static final String HTTP = "http";
aoqi@0 107 private static final String SOAP11 = "soap1.1";
aoqi@0 108 public static final String X_SOAP12 = "Xsoap1.2";
aoqi@0 109
aoqi@0 110 public WsgenOptions() {
aoqi@0 111 protocols.add(SOAP11);
aoqi@0 112 protocols.add(X_SOAP12);
aoqi@0 113 nonstdProtocols.put(X_SOAP12, SOAPBindingImpl.X_SOAP12HTTP_BINDING);
aoqi@0 114 ServiceFinder<WsgenExtension> extn = ServiceFinder.find(WsgenExtension.class);
aoqi@0 115 for(WsgenExtension ext : extn) {
aoqi@0 116 Class clazz = ext.getClass();
aoqi@0 117 WsgenProtocol pro = (WsgenProtocol)clazz.getAnnotation(WsgenProtocol.class);
aoqi@0 118 protocols.add(pro.token());
aoqi@0 119 nonstdProtocols.put(pro.token(), pro.lexical());
aoqi@0 120 }
aoqi@0 121 }
aoqi@0 122
aoqi@0 123 @Override
aoqi@0 124 protected int parseArguments(String[] args, int i) throws BadCommandLineException {
aoqi@0 125
aoqi@0 126 int j = super.parseArguments(args, i);
aoqi@0 127 if (args[i].equals(SERVICENAME_OPTION)) {
aoqi@0 128 serviceName = QName.valueOf(requireArgument(SERVICENAME_OPTION, args, ++i));
aoqi@0 129 if (serviceName.getNamespaceURI() == null || serviceName.getNamespaceURI().length() == 0) {
aoqi@0 130 throw new BadCommandLineException(WscompileMessages.WSGEN_SERVICENAME_MISSING_NAMESPACE(args[i]));
aoqi@0 131 }
aoqi@0 132 if (serviceName.getLocalPart() == null || serviceName.getLocalPart().length() == 0) {
aoqi@0 133 throw new BadCommandLineException(WscompileMessages.WSGEN_SERVICENAME_MISSING_LOCALNAME(args[i]));
aoqi@0 134 }
aoqi@0 135 return 2;
aoqi@0 136 } else if (args[i].equals(PORTNAME_OPTION)) {
aoqi@0 137 portName = QName.valueOf(requireArgument(PORTNAME_OPTION, args, ++i));
aoqi@0 138 if (portName.getNamespaceURI() == null || portName.getNamespaceURI().length() == 0) {
aoqi@0 139 throw new BadCommandLineException(WscompileMessages.WSGEN_PORTNAME_MISSING_NAMESPACE(args[i]));
aoqi@0 140 }
aoqi@0 141 if (portName.getLocalPart() == null || portName.getLocalPart().length() == 0) {
aoqi@0 142 throw new BadCommandLineException(WscompileMessages.WSGEN_PORTNAME_MISSING_LOCALNAME(args[i]));
aoqi@0 143 }
aoqi@0 144 return 2;
aoqi@0 145 } else if (args[i].equals("-r")) {
aoqi@0 146 nonclassDestDir = new File(requireArgument("-r", args, ++i));
aoqi@0 147 if (!nonclassDestDir.exists()) {
aoqi@0 148 throw new BadCommandLineException(WscompileMessages.WSCOMPILE_NO_SUCH_DIRECTORY(nonclassDestDir.getPath()));
aoqi@0 149 }
aoqi@0 150 return 2;
aoqi@0 151 } else if (args[i].startsWith("-wsdl")) {
aoqi@0 152 genWsdl = true;
aoqi@0 153 //String value = requireArgument("-wsdl", args, ++i).substring(5);
aoqi@0 154 String value = args[i].substring(5);
aoqi@0 155 int index = value.indexOf(':');
aoqi@0 156 if (index == 0) {
aoqi@0 157 value = value.substring(1);
aoqi@0 158 index = value.indexOf('/');
aoqi@0 159 if (index == -1) {
aoqi@0 160 protocol = value;
aoqi@0 161 } else {
aoqi@0 162 protocol = value.substring(0, index);
aoqi@0 163 }
aoqi@0 164 protocolSet = true;
aoqi@0 165 }
aoqi@0 166 return 1;
aoqi@0 167 } else if (args[i].equals("-XwsgenReport")) {
aoqi@0 168 // undocumented switch for the test harness
aoqi@0 169 wsgenReport = new File(requireArgument("-XwsgenReport", args, ++i));
aoqi@0 170 return 2;
aoqi@0 171 } else if (args[i].equals("-Xdonotoverwrite")) {
aoqi@0 172 doNotOverWrite = true;
aoqi@0 173 return 1;
aoqi@0 174 } else if (args[i].equals("-inlineSchemas")) {
aoqi@0 175 inlineSchemas = true;
aoqi@0 176 return 1;
aoqi@0 177 } else if ("-x".equals(args[i])) {
aoqi@0 178 externalMetadataFiles.add(requireArgument("-x", args, ++i));
aoqi@0 179 return 1;
aoqi@0 180 }
aoqi@0 181
aoqi@0 182 return j;
aoqi@0 183 }
aoqi@0 184
aoqi@0 185
aoqi@0 186 @Override
aoqi@0 187 protected void addFile(String arg) {
aoqi@0 188 endpoints.add(arg);
aoqi@0 189 }
aoqi@0 190
aoqi@0 191 List<String> endpoints = new ArrayList<String>();
aoqi@0 192
aoqi@0 193 public Class endpoint;
aoqi@0 194
aoqi@0 195
aoqi@0 196 private boolean isImplClass;
aoqi@0 197
aoqi@0 198 public void validate() throws BadCommandLineException {
aoqi@0 199 if(nonclassDestDir == null)
aoqi@0 200 nonclassDestDir = destDir;
aoqi@0 201
aoqi@0 202 if (!protocols.contains(protocol)) {
aoqi@0 203 throw new BadCommandLineException(WscompileMessages.WSGEN_INVALID_PROTOCOL(protocol, protocols));
aoqi@0 204 }
aoqi@0 205
aoqi@0 206 if (endpoints.isEmpty()) {
aoqi@0 207 throw new BadCommandLineException(WscompileMessages.WSGEN_MISSING_FILE());
aoqi@0 208 }
aoqi@0 209 if (protocol == null || protocol.equalsIgnoreCase(X_SOAP12) && !isExtensionMode()) {
aoqi@0 210 throw new BadCommandLineException(WscompileMessages.WSGEN_SOAP_12_WITHOUT_EXTENSION());
aoqi@0 211 }
aoqi@0 212
aoqi@0 213 if (nonstdProtocols.containsKey(protocol) && !isExtensionMode()) {
aoqi@0 214 throw new BadCommandLineException(WscompileMessages.WSGEN_PROTOCOL_WITHOUT_EXTENSION(protocol));
aoqi@0 215 }
aoqi@0 216 if (inlineSchemas && !genWsdl) {
aoqi@0 217 throw new BadCommandLineException(WscompileMessages.WSGEN_INLINE_SCHEMAS_ONLY_WITH_WSDL());
aoqi@0 218 }
aoqi@0 219
aoqi@0 220 validateEndpointClass();
aoqi@0 221 validateArguments();
aoqi@0 222 }
aoqi@0 223 /**
aoqi@0 224 * Get an implementation class annotated with @WebService annotation.
aoqi@0 225 */
aoqi@0 226 private void validateEndpointClass() throws BadCommandLineException {
aoqi@0 227 Class clazz = null;
aoqi@0 228 for(String cls : endpoints){
aoqi@0 229 clazz = getClass(cls);
aoqi@0 230 if (clazz == null)
aoqi@0 231 continue;
aoqi@0 232
aoqi@0 233 if (clazz.isEnum() || clazz.isInterface() ||
aoqi@0 234 clazz.isPrimitive()) {
aoqi@0 235 continue;
aoqi@0 236 }
aoqi@0 237 isImplClass = true;
aoqi@0 238 WebService webService = (WebService) clazz.getAnnotation(WebService.class);
aoqi@0 239 if(webService == null)
aoqi@0 240 continue;
aoqi@0 241 break;
aoqi@0 242 }
aoqi@0 243 if(clazz == null){
aoqi@0 244 throw new BadCommandLineException(WscompileMessages.WSGEN_CLASS_NOT_FOUND(endpoints.get(0)));
aoqi@0 245 }
aoqi@0 246 if(!isImplClass){
aoqi@0 247 throw new BadCommandLineException(WscompileMessages.WSGEN_CLASS_MUST_BE_IMPLEMENTATION_CLASS(clazz.getName()));
aoqi@0 248 }
aoqi@0 249 endpoint = clazz;
aoqi@0 250 validateBinding();
aoqi@0 251 }
aoqi@0 252
aoqi@0 253 private void validateBinding() throws BadCommandLineException {
aoqi@0 254 if (genWsdl) {
aoqi@0 255 BindingID binding = BindingID.parse(endpoint);
aoqi@0 256 if ((binding.equals(BindingID.SOAP12_HTTP) ||
aoqi@0 257 binding.equals(BindingID.SOAP12_HTTP_MTOM)) &&
aoqi@0 258 !(protocol.equals(X_SOAP12) && isExtensionMode())) {
aoqi@0 259 throw new BadCommandLineException(WscompileMessages.WSGEN_CANNOT_GEN_WSDL_FOR_SOAP_12_BINDING(binding.toString(), endpoint.getName()));
aoqi@0 260 }
aoqi@0 261 if (binding.equals(BindingID.XML_HTTP)) {
aoqi@0 262 throw new BadCommandLineException(WscompileMessages.WSGEN_CANNOT_GEN_WSDL_FOR_NON_SOAP_BINDING(binding.toString(), endpoint.getName()));
aoqi@0 263 }
aoqi@0 264 }
aoqi@0 265 }
aoqi@0 266
aoqi@0 267 private void validateArguments() throws BadCommandLineException {
aoqi@0 268 if (!genWsdl) {
aoqi@0 269 if (serviceName != null) {
aoqi@0 270 throw new BadCommandLineException(WscompileMessages.WSGEN_WSDL_ARG_NO_GENWSDL(SERVICENAME_OPTION));
aoqi@0 271 }
aoqi@0 272 if (portName != null) {
aoqi@0 273 throw new BadCommandLineException(WscompileMessages.WSGEN_WSDL_ARG_NO_GENWSDL(PORTNAME_OPTION));
aoqi@0 274 }
aoqi@0 275 }
aoqi@0 276 }
aoqi@0 277
aoqi@0 278 BindingID getBindingID(String protocol) {
aoqi@0 279 if (protocol.equals(SOAP11))
aoqi@0 280 return BindingID.SOAP11_HTTP;
aoqi@0 281 if (protocol.equals(X_SOAP12))
aoqi@0 282 return BindingID.SOAP12_HTTP;
aoqi@0 283 String lexical = nonstdProtocols.get(protocol);
aoqi@0 284 return (lexical != null) ? BindingID.parse(lexical) : null;
aoqi@0 285 }
aoqi@0 286
aoqi@0 287
aoqi@0 288 private Class getClass(String className) {
aoqi@0 289 try {
aoqi@0 290 return getClassLoader().loadClass(className);
aoqi@0 291 } catch (ClassNotFoundException e) {
aoqi@0 292 return null;
aoqi@0 293 }
aoqi@0 294 }
aoqi@0 295
aoqi@0 296 }

mercurial