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

Fri, 04 Oct 2013 16:21:34 +0100

author
mkos
date
Fri, 04 Oct 2013 16:21:34 +0100
changeset 408
b0610cd08440
parent 368
0989ad8c0860
child 637
9c07ef4934dd
permissions
-rw-r--r--

8025054: Update JAX-WS RI integration to 2.2.9-b130926.1035
Reviewed-by: chegar

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

mercurial