1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/tools/internal/ws/wscompile/Options.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,491 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2013, 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.wscompile; 1.30 + 1.31 +import com.sun.tools.internal.ws.resources.WscompileMessages; 1.32 +import com.sun.tools.internal.ws.Invoker; 1.33 + 1.34 +import javax.annotation.processing.Filer; 1.35 +import java.io.File; 1.36 +import java.io.IOException; 1.37 +import java.net.MalformedURLException; 1.38 +import java.net.URL; 1.39 +import java.net.URLClassLoader; 1.40 +import java.nio.charset.Charset; 1.41 +import java.nio.charset.IllegalCharsetNameException; 1.42 +import java.text.MessageFormat; 1.43 +import java.util.ArrayList; 1.44 +import java.util.List; 1.45 +import java.util.StringTokenizer; 1.46 + 1.47 +/** 1.48 + * Provide common jaxws tool options. 1.49 + * 1.50 + * @author Vivek Pandey 1.51 + */ 1.52 +public class Options { 1.53 + /** 1.54 + * -verbose 1.55 + */ 1.56 + public boolean verbose; 1.57 + 1.58 + /** 1.59 + * - quite 1.60 + */ 1.61 + public boolean quiet; 1.62 + 1.63 + /** 1.64 + * -keep 1.65 + */ 1.66 + public boolean keep; 1.67 + 1.68 + 1.69 + 1.70 + /** 1.71 + * -d 1.72 + */ 1.73 + public File destDir = new File("."); 1.74 + 1.75 + 1.76 + /** 1.77 + * -s 1.78 + */ 1.79 + public File sourceDir; 1.80 + 1.81 + /** 1.82 + * The filer that can use used to write out the generated files 1.83 + */ 1.84 + public Filer filer; 1.85 + 1.86 + /** 1.87 + * -encoding 1.88 + */ 1.89 + public String encoding; 1.90 + 1.91 + public String classpath = System.getProperty("java.class.path"); 1.92 + 1.93 + /** 1.94 + * -javacOptions 1.95 + * 1.96 + * @since 2.2.9 1.97 + */ 1.98 + public List<String> javacOptions; 1.99 + 1.100 + 1.101 + /** 1.102 + * -Xnocompile 1.103 + */ 1.104 + public boolean nocompile; 1.105 + 1.106 + /** 1.107 + * If true XML security features when parsing XML documents will be disabled. 1.108 + * The default value is false. 1.109 + * 1.110 + * Boolean 1.111 + * @since 2.2.9 1.112 + */ 1.113 + public boolean disableXmlSecurity; 1.114 + 1.115 + public enum Target { 1.116 + V2_0, V2_1, V2_2; 1.117 + 1.118 + /** 1.119 + * Returns true if this version is equal or later than the given one. 1.120 + */ 1.121 + public boolean isLaterThan(Target t) { 1.122 + return this.ordinal() >= t.ordinal(); 1.123 + } 1.124 + 1.125 + /** 1.126 + * Parses "2.0" and "2.1" into the {@link Target} object. 1.127 + * 1.128 + * @return null for parsing failure. 1.129 + */ 1.130 + public static Target parse(String token) { 1.131 + if (token.equals("2.0")) 1.132 + return Target.V2_0; 1.133 + else if (token.equals("2.1")) 1.134 + return Target.V2_1; 1.135 + else if (token.equals("2.2")) 1.136 + return Target.V2_2; 1.137 + return null; 1.138 + } 1.139 + 1.140 + /** 1.141 + * Gives the String representation of the {@link Target} 1.142 + */ 1.143 + public String getVersion(){ 1.144 + switch(this){ 1.145 + case V2_0: 1.146 + return "2.0"; 1.147 + case V2_1: 1.148 + return "2.1"; 1.149 + case V2_2: 1.150 + return "2.2"; 1.151 + default: 1.152 + return null; 1.153 + } 1.154 + } 1.155 + 1.156 + public static Target getDefault() { 1.157 + return V2_2; 1.158 + } 1.159 + 1.160 + public static Target getLoadedAPIVersion() { 1.161 + return LOADED_API_VERSION; 1.162 + } 1.163 + 1.164 + private static final Target LOADED_API_VERSION; 1.165 + 1.166 + static { 1.167 + // check if we are indeed loading JAX-WS 2.2 API 1.168 + if (Invoker.checkIfLoading22API()) { 1.169 + LOADED_API_VERSION = Target.V2_2; 1.170 + } // check if we are indeed loading JAX-WS 2.1 API 1.171 + else if (Invoker.checkIfLoading21API()) { 1.172 + LOADED_API_VERSION = Target.V2_1; 1.173 + } else { 1.174 + LOADED_API_VERSION = Target.V2_0; 1.175 + } 1.176 + } 1.177 + } 1.178 + 1.179 + public Target target = Target.V2_2; 1.180 + 1.181 + /** 1.182 + * strictly follow the compatibility rules specified in JAXWS spec 1.183 + */ 1.184 + public static final int STRICT = 1; 1.185 + 1.186 + /** 1.187 + * loosely follow the compatibility rules and allow the use of vendor 1.188 + * binding extensions 1.189 + */ 1.190 + public static final int EXTENSION = 2; 1.191 + 1.192 + /** 1.193 + * this switch determines how carefully the compiler will follow 1.194 + * the compatibility rules in the spec. Either <code>STRICT</code> 1.195 + * or <code>EXTENSION</code>. 1.196 + */ 1.197 + public int compatibilityMode = STRICT; 1.198 + 1.199 + public boolean isExtensionMode() { 1.200 + return compatibilityMode == EXTENSION; 1.201 + } 1.202 + 1.203 + public boolean debug = false; 1.204 + 1.205 + /** 1.206 + * -Xdebug - gives complete stack trace 1.207 + */ 1.208 + public boolean debugMode = false; 1.209 + 1.210 + 1.211 + private final List<File> generatedFiles = new ArrayList<File>(); 1.212 + private ClassLoader classLoader; 1.213 + 1.214 + 1.215 + /** 1.216 + * Remember info on generated source file generated so that it 1.217 + * can be removed later, if appropriate. 1.218 + */ 1.219 + public void addGeneratedFile(File file) { 1.220 + generatedFiles.add(file); 1.221 + } 1.222 + 1.223 + /** 1.224 + * Remove generated files 1.225 + */ 1.226 + public void removeGeneratedFiles(){ 1.227 + for(File file : generatedFiles){ 1.228 + if (file.getName().endsWith(".java")) { 1.229 + boolean deleted = file.delete(); 1.230 + if (verbose && !deleted) { 1.231 + System.out.println(MessageFormat.format("{0} could not be deleted.", file)); 1.232 + } 1.233 + } 1.234 + } 1.235 + generatedFiles.clear(); 1.236 + } 1.237 + 1.238 + /** 1.239 + * Return all the generated files and its types. 1.240 + */ 1.241 + public Iterable<File> getGeneratedFiles() { 1.242 + return generatedFiles; 1.243 + } 1.244 + 1.245 + /** 1.246 + * Delete all the generated source files made during the execution 1.247 + * of this environment (those that have been registered with the 1.248 + * "addGeneratedFile" method). 1.249 + */ 1.250 + public void deleteGeneratedFiles() { 1.251 + synchronized (generatedFiles) { 1.252 + for (File file : generatedFiles) { 1.253 + if (file.getName().endsWith(".java")) { 1.254 + boolean deleted = file.delete(); 1.255 + if (verbose && !deleted) { 1.256 + System.out.println(MessageFormat.format("{0} could not be deleted.", file)); 1.257 + } 1.258 + } 1.259 + } 1.260 + generatedFiles.clear(); 1.261 + } 1.262 + } 1.263 + 1.264 + /** 1.265 + * Parses arguments and fill fields of this object. 1.266 + * 1.267 + * @exception BadCommandLineException 1.268 + * thrown when there's a problem in the command-line arguments 1.269 + */ 1.270 + public void parseArguments( String[] args ) throws BadCommandLineException { 1.271 + 1.272 + for (int i = 0; i < args.length; i++) { 1.273 + if(args[i].length()==0) 1.274 + throw new BadCommandLineException(); 1.275 + if (args[i].charAt(0) == '-') { 1.276 + int j = parseArguments(args,i); 1.277 + if(j==0) 1.278 + throw new BadCommandLineException(WscompileMessages.WSCOMPILE_INVALID_OPTION(args[i])); 1.279 + i += (j-1); 1.280 + } else { 1.281 + addFile(args[i]); 1.282 + } 1.283 + } 1.284 + if(destDir == null) 1.285 + destDir = new File("."); 1.286 + if(sourceDir == null) 1.287 + sourceDir = destDir; 1.288 + } 1.289 + 1.290 + 1.291 + /** 1.292 + * Adds a file from the argume 1.293 + * 1.294 + * @param arg a file, could be a wsdl or xsd or a Class 1.295 + */ 1.296 + protected void addFile(String arg) throws BadCommandLineException {} 1.297 + 1.298 + /** 1.299 + * Parses an option <code>args[i]</code> and return 1.300 + * the number of tokens consumed. 1.301 + * 1.302 + * @return 1.303 + * 0 if the argument is not understood. Returning 0 1.304 + * will let the caller report an error. 1.305 + * @exception BadCommandLineException 1.306 + * If the callee wants to provide a custom message for an error. 1.307 + */ 1.308 + protected int parseArguments(String[] args, int i) throws BadCommandLineException { 1.309 + if (args[i].equals("-g")) { 1.310 + debug = true; 1.311 + return 1; 1.312 + } else if (args[i].equals("-Xdebug")) { 1.313 + debugMode = true; 1.314 + return 1; 1.315 + } else if (args[i].equals("-Xendorsed")) { 1.316 + // this option is processed much earlier, so just ignore. 1.317 + return 1; 1.318 + } else if (args[i].equals("-verbose")) { 1.319 + verbose = true; 1.320 + return 1; 1.321 + } else if (args[i].equals("-quiet")) { 1.322 + quiet = true; 1.323 + return 1; 1.324 + } else if (args[i].equals("-keep")) { 1.325 + keep = true; 1.326 + return 1; 1.327 + } else if (args[i].equals("-target")) { 1.328 + String token = requireArgument("-target", args, ++i); 1.329 + target = Target.parse(token); 1.330 + if(target == null) 1.331 + throw new BadCommandLineException(WscompileMessages.WSIMPORT_ILLEGAL_TARGET_VERSION(token)); 1.332 + return 2; 1.333 + } else if (args[i].equals("-classpath") || args[i].equals("-cp")) { 1.334 + classpath = requireArgument("-classpath", args, ++i) + File.pathSeparator + System.getProperty("java.class.path"); 1.335 + return 2; 1.336 + } else if (args[i].equals("-d")) { 1.337 + destDir = new File(requireArgument("-d", args, ++i)); 1.338 + if (!destDir.exists()) 1.339 + throw new BadCommandLineException(WscompileMessages.WSCOMPILE_NO_SUCH_DIRECTORY(destDir.getPath())); 1.340 + return 2; 1.341 + } else if (args[i].equals("-s")) { 1.342 + sourceDir = new File(requireArgument("-s", args, ++i)); 1.343 + keep = true; 1.344 + if (!sourceDir.exists()) { 1.345 + throw new BadCommandLineException(WscompileMessages.WSCOMPILE_NO_SUCH_DIRECTORY(sourceDir.getPath())); 1.346 + } 1.347 + return 2; 1.348 + } else if (args[i].equals("-extension")) { 1.349 + compatibilityMode = EXTENSION; 1.350 + return 1; 1.351 + } else if (args[i].startsWith("-help")) { 1.352 + WeAreDone done = new WeAreDone(); 1.353 + done.initOptions(this); 1.354 + throw done; 1.355 + } else if (args[i].equals("-Xnocompile")) { 1.356 + // -nocompile implies -keep. this is undocumented switch. 1.357 + nocompile = true; 1.358 + keep = true; 1.359 + return 1; 1.360 + } else if (args[i].equals("-encoding")) { 1.361 + encoding = requireArgument("-encoding", args, ++i); 1.362 + try { 1.363 + if (!Charset.isSupported(encoding)) { 1.364 + throw new BadCommandLineException(WscompileMessages.WSCOMPILE_UNSUPPORTED_ENCODING(encoding)); 1.365 + } 1.366 + } catch (IllegalCharsetNameException icne) { 1.367 + throw new BadCommandLineException(WscompileMessages.WSCOMPILE_UNSUPPORTED_ENCODING(encoding)); 1.368 + } 1.369 + return 2; 1.370 + } else if (args[i].equals("-disableXmlSecurity")) { 1.371 + disableXmlSecurity(); 1.372 + return 1; 1.373 + } else if (args[i].startsWith("-J")) { 1.374 + if (javacOptions == null) { 1.375 + javacOptions = new ArrayList<String>(); 1.376 + } 1.377 + javacOptions.add(args[i].substring(2)); 1.378 + return 1; 1.379 + } 1.380 + return 0; 1.381 + } 1.382 + 1.383 + // protected method to allow overriding 1.384 + protected void disableXmlSecurity() { 1.385 + disableXmlSecurity= true; 1.386 + } 1.387 + 1.388 + /** 1.389 + * Obtains an operand and reports an error if it's not there. 1.390 + */ 1.391 + public String requireArgument(String optionName, String[] args, int i) throws BadCommandLineException { 1.392 + //if (i == args.length || args[i].startsWith("-")) { 1.393 + if (args[i].startsWith("-")) { 1.394 + throw new BadCommandLineException(WscompileMessages.WSCOMPILE_MISSING_OPTION_ARGUMENT(optionName)); 1.395 + } 1.396 + return args[i]; 1.397 + } 1.398 + 1.399 + List<String> getJavacOptions(List<String> existingOptions, WsimportListener listener) { 1.400 + List<String> result = new ArrayList<String>(); 1.401 + for (String o: javacOptions) { 1.402 + if (o.contains("=") && !o.startsWith("A")) { 1.403 + int i = o.indexOf('='); 1.404 + String key = o.substring(0, i); 1.405 + if (existingOptions.contains(key)) { 1.406 + listener.message(WscompileMessages.WSCOMPILE_EXISTING_OPTION(key)); 1.407 + } else { 1.408 + result.add(key); 1.409 + result.add(o.substring(i + 1)); 1.410 + } 1.411 + } else { 1.412 + if (existingOptions.contains(o)) { 1.413 + listener.message(WscompileMessages.WSCOMPILE_EXISTING_OPTION(o)); 1.414 + } else { 1.415 + result.add(o); 1.416 + } 1.417 + } 1.418 + } 1.419 + return result; 1.420 + } 1.421 + 1.422 + /** 1.423 + * Used to signal that we've finished processing. 1.424 + */ 1.425 + public static final class WeAreDone extends BadCommandLineException {} 1.426 + 1.427 + /** 1.428 + * Get a URLClassLoader from using the classpath 1.429 + */ 1.430 + public ClassLoader getClassLoader() { 1.431 + if (classLoader == null) { 1.432 + classLoader = 1.433 + new URLClassLoader(pathToURLs(classpath), 1.434 + this.getClass().getClassLoader()); 1.435 + } 1.436 + return classLoader; 1.437 + } 1.438 + 1.439 + /** 1.440 + * Utility method for converting a search path string to an array 1.441 + * of directory and JAR file URLs. 1.442 + * 1.443 + * @param path the search path string 1.444 + * @return the resulting array of directory and JAR file URLs 1.445 + */ 1.446 + public static URL[] pathToURLs(String path) { 1.447 + StringTokenizer st = new StringTokenizer(path, File.pathSeparator); 1.448 + URL[] urls = new URL[st.countTokens()]; 1.449 + int count = 0; 1.450 + while (st.hasMoreTokens()) { 1.451 + URL url = fileToURL(new File(st.nextToken())); 1.452 + if (url != null) { 1.453 + urls[count++] = url; 1.454 + } 1.455 + } 1.456 + if (urls.length != count) { 1.457 + URL[] tmp = new URL[count]; 1.458 + System.arraycopy(urls, 0, tmp, 0, count); 1.459 + urls = tmp; 1.460 + } 1.461 + return urls; 1.462 + } 1.463 + 1.464 + /** 1.465 + * Returns the directory or JAR file URL corresponding to the specified 1.466 + * local file name. 1.467 + * 1.468 + * @param file the File object 1.469 + * @return the resulting directory or JAR file URL, or null if unknown 1.470 + */ 1.471 + public static URL fileToURL(File file) { 1.472 + String name; 1.473 + try { 1.474 + name = file.getCanonicalPath(); 1.475 + } catch (IOException e) { 1.476 + name = file.getAbsolutePath(); 1.477 + } 1.478 + name = name.replace(File.separatorChar, '/'); 1.479 + if (!name.startsWith("/")) { 1.480 + name = "/" + name; 1.481 + } 1.482 + 1.483 + // If the file does not exist, then assume that it's a directory 1.484 + if (!file.isFile()) { 1.485 + name = name + "/"; 1.486 + } 1.487 + try { 1.488 + return new URL("file", "", name); 1.489 + } catch (MalformedURLException e) { 1.490 + throw new IllegalArgumentException("file"); 1.491 + } 1.492 + } 1.493 + 1.494 +}