src/share/jaxws_classes/com/sun/tools/internal/ws/wscompile/Options.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, 2013, 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.resources.WscompileMessages;
ohair@286 29 import com.sun.tools.internal.ws.Invoker;
ohair@286 30
ohair@286 31 import javax.annotation.processing.Filer;
ohair@286 32 import java.io.File;
ohair@286 33 import java.io.IOException;
ohair@286 34 import java.net.MalformedURLException;
ohair@286 35 import java.net.URL;
ohair@286 36 import java.net.URLClassLoader;
ohair@286 37 import java.nio.charset.Charset;
ohair@286 38 import java.nio.charset.IllegalCharsetNameException;
alanb@368 39 import java.text.MessageFormat;
ohair@286 40 import java.util.ArrayList;
ohair@286 41 import java.util.List;
ohair@286 42 import java.util.StringTokenizer;
ohair@286 43
ohair@286 44 /**
ohair@286 45 * Provide common jaxws tool options.
ohair@286 46 *
ohair@286 47 * @author Vivek Pandey
ohair@286 48 */
ohair@286 49 public class Options {
ohair@286 50 /**
ohair@286 51 * -verbose
ohair@286 52 */
ohair@286 53 public boolean verbose;
ohair@286 54
ohair@286 55 /**
ohair@286 56 * - quite
ohair@286 57 */
ohair@286 58 public boolean quiet;
ohair@286 59
ohair@286 60 /**
ohair@286 61 * -keep
ohair@286 62 */
ohair@286 63 public boolean keep;
ohair@286 64
ohair@286 65
ohair@286 66
ohair@286 67 /**
ohair@286 68 * -d
ohair@286 69 */
ohair@286 70 public File destDir = new File(".");
ohair@286 71
ohair@286 72
ohair@286 73 /**
ohair@286 74 * -s
ohair@286 75 */
ohair@286 76 public File sourceDir;
ohair@286 77
ohair@286 78 /**
ohair@286 79 * The filer that can use used to write out the generated files
ohair@286 80 */
ohair@286 81 public Filer filer;
ohair@286 82
ohair@286 83 /**
ohair@286 84 * -encoding
ohair@286 85 */
ohair@286 86 public String encoding;
ohair@286 87
ohair@286 88 public String classpath = System.getProperty("java.class.path");
ohair@286 89
mkos@408 90 /**
mkos@408 91 * -javacOptions
mkos@408 92 *
mkos@408 93 * @since 2.2.9
mkos@408 94 */
mkos@408 95 public List<String> javacOptions;
mkos@408 96
ohair@286 97
ohair@286 98 /**
ohair@286 99 * -Xnocompile
ohair@286 100 */
ohair@286 101 public boolean nocompile;
ohair@286 102
alanb@368 103 /**
mkos@408 104 * If true XML security features when parsing XML documents will be disabled.
mkos@408 105 * The default value is false.
mkos@408 106 *
mkos@408 107 * Boolean
mkos@408 108 * @since 2.2.9
alanb@368 109 */
mkos@408 110 public boolean disableXmlSecurity;
alanb@368 111
ohair@286 112 public enum Target {
ohair@286 113 V2_0, V2_1, V2_2;
ohair@286 114
ohair@286 115 /**
ohair@286 116 * Returns true if this version is equal or later than the given one.
ohair@286 117 */
ohair@286 118 public boolean isLaterThan(Target t) {
ohair@286 119 return this.ordinal() >= t.ordinal();
ohair@286 120 }
ohair@286 121
ohair@286 122 /**
ohair@286 123 * Parses "2.0" and "2.1" into the {@link Target} object.
ohair@286 124 *
ohair@286 125 * @return null for parsing failure.
ohair@286 126 */
ohair@286 127 public static Target parse(String token) {
ohair@286 128 if (token.equals("2.0"))
ohair@286 129 return Target.V2_0;
ohair@286 130 else if (token.equals("2.1"))
ohair@286 131 return Target.V2_1;
ohair@286 132 else if (token.equals("2.2"))
ohair@286 133 return Target.V2_2;
ohair@286 134 return null;
ohair@286 135 }
ohair@286 136
ohair@286 137 /**
ohair@286 138 * Gives the String representation of the {@link Target}
ohair@286 139 */
ohair@286 140 public String getVersion(){
ohair@286 141 switch(this){
ohair@286 142 case V2_0:
ohair@286 143 return "2.0";
ohair@286 144 case V2_1:
ohair@286 145 return "2.1";
ohair@286 146 case V2_2:
ohair@286 147 return "2.2";
ohair@286 148 default:
ohair@286 149 return null;
ohair@286 150 }
ohair@286 151 }
ohair@286 152
ohair@286 153 public static Target getDefault() {
ohair@286 154 return V2_2;
ohair@286 155 }
ohair@286 156
ohair@286 157 public static Target getLoadedAPIVersion() {
ohair@286 158 return LOADED_API_VERSION;
ohair@286 159 }
ohair@286 160
ohair@286 161 private static final Target LOADED_API_VERSION;
ohair@286 162
ohair@286 163 static {
ohair@286 164 // check if we are indeed loading JAX-WS 2.2 API
ohair@286 165 if (Invoker.checkIfLoading22API()) {
ohair@286 166 LOADED_API_VERSION = Target.V2_2;
ohair@286 167 } // check if we are indeed loading JAX-WS 2.1 API
ohair@286 168 else if (Invoker.checkIfLoading21API()) {
ohair@286 169 LOADED_API_VERSION = Target.V2_1;
ohair@286 170 } else {
ohair@286 171 LOADED_API_VERSION = Target.V2_0;
ohair@286 172 }
ohair@286 173 }
ohair@286 174 }
ohair@286 175
ohair@286 176 public Target target = Target.V2_2;
ohair@286 177
ohair@286 178 /**
ohair@286 179 * strictly follow the compatibility rules specified in JAXWS spec
ohair@286 180 */
ohair@286 181 public static final int STRICT = 1;
ohair@286 182
ohair@286 183 /**
ohair@286 184 * loosely follow the compatibility rules and allow the use of vendor
ohair@286 185 * binding extensions
ohair@286 186 */
ohair@286 187 public static final int EXTENSION = 2;
ohair@286 188
ohair@286 189 /**
ohair@286 190 * this switch determines how carefully the compiler will follow
ohair@286 191 * the compatibility rules in the spec. Either <code>STRICT</code>
ohair@286 192 * or <code>EXTENSION</code>.
ohair@286 193 */
ohair@286 194 public int compatibilityMode = STRICT;
ohair@286 195
ohair@286 196 public boolean isExtensionMode() {
ohair@286 197 return compatibilityMode == EXTENSION;
ohair@286 198 }
ohair@286 199
ohair@286 200 public boolean debug = false;
ohair@286 201
ohair@286 202 /**
ohair@286 203 * -Xdebug - gives complete stack trace
ohair@286 204 */
ohair@286 205 public boolean debugMode = false;
ohair@286 206
ohair@286 207
ohair@286 208 private final List<File> generatedFiles = new ArrayList<File>();
ohair@286 209 private ClassLoader classLoader;
ohair@286 210
ohair@286 211
ohair@286 212 /**
ohair@286 213 * Remember info on generated source file generated so that it
ohair@286 214 * can be removed later, if appropriate.
ohair@286 215 */
ohair@286 216 public void addGeneratedFile(File file) {
ohair@286 217 generatedFiles.add(file);
ohair@286 218 }
ohair@286 219
ohair@286 220 /**
ohair@286 221 * Remove generated files
ohair@286 222 */
ohair@286 223 public void removeGeneratedFiles(){
ohair@286 224 for(File file : generatedFiles){
ohair@286 225 if (file.getName().endsWith(".java")) {
alanb@368 226 boolean deleted = file.delete();
alanb@368 227 if (verbose && !deleted) {
alanb@368 228 System.out.println(MessageFormat.format("{0} could not be deleted.", file));
alanb@368 229 }
ohair@286 230 }
ohair@286 231 }
ohair@286 232 generatedFiles.clear();
ohair@286 233 }
ohair@286 234
ohair@286 235 /**
ohair@286 236 * Return all the generated files and its types.
ohair@286 237 */
ohair@286 238 public Iterable<File> getGeneratedFiles() {
ohair@286 239 return generatedFiles;
ohair@286 240 }
ohair@286 241
ohair@286 242 /**
ohair@286 243 * Delete all the generated source files made during the execution
ohair@286 244 * of this environment (those that have been registered with the
ohair@286 245 * "addGeneratedFile" method).
ohair@286 246 */
ohair@286 247 public void deleteGeneratedFiles() {
ohair@286 248 synchronized (generatedFiles) {
ohair@286 249 for (File file : generatedFiles) {
ohair@286 250 if (file.getName().endsWith(".java")) {
alanb@368 251 boolean deleted = file.delete();
alanb@368 252 if (verbose && !deleted) {
alanb@368 253 System.out.println(MessageFormat.format("{0} could not be deleted.", file));
alanb@368 254 }
ohair@286 255 }
ohair@286 256 }
ohair@286 257 generatedFiles.clear();
ohair@286 258 }
ohair@286 259 }
ohair@286 260
ohair@286 261 /**
ohair@286 262 * Parses arguments and fill fields of this object.
ohair@286 263 *
ohair@286 264 * @exception BadCommandLineException
ohair@286 265 * thrown when there's a problem in the command-line arguments
ohair@286 266 */
ohair@286 267 public void parseArguments( String[] args ) throws BadCommandLineException {
ohair@286 268
ohair@286 269 for (int i = 0; i < args.length; i++) {
ohair@286 270 if(args[i].length()==0)
ohair@286 271 throw new BadCommandLineException();
ohair@286 272 if (args[i].charAt(0) == '-') {
ohair@286 273 int j = parseArguments(args,i);
ohair@286 274 if(j==0)
ohair@286 275 throw new BadCommandLineException(WscompileMessages.WSCOMPILE_INVALID_OPTION(args[i]));
ohair@286 276 i += (j-1);
ohair@286 277 } else {
ohair@286 278 addFile(args[i]);
ohair@286 279 }
ohair@286 280 }
ohair@286 281 if(destDir == null)
ohair@286 282 destDir = new File(".");
ohair@286 283 if(sourceDir == null)
ohair@286 284 sourceDir = destDir;
ohair@286 285 }
ohair@286 286
ohair@286 287
ohair@286 288 /**
ohair@286 289 * Adds a file from the argume
ohair@286 290 *
ohair@286 291 * @param arg a file, could be a wsdl or xsd or a Class
ohair@286 292 */
ohair@286 293 protected void addFile(String arg) throws BadCommandLineException {}
ohair@286 294
ohair@286 295 /**
ohair@286 296 * Parses an option <code>args[i]</code> and return
ohair@286 297 * the number of tokens consumed.
ohair@286 298 *
ohair@286 299 * @return
ohair@286 300 * 0 if the argument is not understood. Returning 0
ohair@286 301 * will let the caller report an error.
ohair@286 302 * @exception BadCommandLineException
ohair@286 303 * If the callee wants to provide a custom message for an error.
ohair@286 304 */
ohair@286 305 protected int parseArguments(String[] args, int i) throws BadCommandLineException {
ohair@286 306 if (args[i].equals("-g")) {
ohair@286 307 debug = true;
ohair@286 308 return 1;
ohair@286 309 } else if (args[i].equals("-Xdebug")) {
ohair@286 310 debugMode = true;
ohair@286 311 return 1;
ohair@286 312 } else if (args[i].equals("-Xendorsed")) {
ohair@286 313 // this option is processed much earlier, so just ignore.
ohair@286 314 return 1;
ohair@286 315 } else if (args[i].equals("-verbose")) {
ohair@286 316 verbose = true;
ohair@286 317 return 1;
ohair@286 318 } else if (args[i].equals("-quiet")) {
ohair@286 319 quiet = true;
ohair@286 320 return 1;
ohair@286 321 } else if (args[i].equals("-keep")) {
ohair@286 322 keep = true;
ohair@286 323 return 1;
ohair@286 324 } else if (args[i].equals("-target")) {
ohair@286 325 String token = requireArgument("-target", args, ++i);
ohair@286 326 target = Target.parse(token);
ohair@286 327 if(target == null)
ohair@286 328 throw new BadCommandLineException(WscompileMessages.WSIMPORT_ILLEGAL_TARGET_VERSION(token));
ohair@286 329 return 2;
ohair@286 330 } else if (args[i].equals("-classpath") || args[i].equals("-cp")) {
ohair@286 331 classpath = requireArgument("-classpath", args, ++i) + File.pathSeparator + System.getProperty("java.class.path");
ohair@286 332 return 2;
ohair@286 333 } else if (args[i].equals("-d")) {
ohair@286 334 destDir = new File(requireArgument("-d", args, ++i));
ohair@286 335 if (!destDir.exists())
ohair@286 336 throw new BadCommandLineException(WscompileMessages.WSCOMPILE_NO_SUCH_DIRECTORY(destDir.getPath()));
ohair@286 337 return 2;
ohair@286 338 } else if (args[i].equals("-s")) {
ohair@286 339 sourceDir = new File(requireArgument("-s", args, ++i));
ohair@286 340 keep = true;
ohair@286 341 if (!sourceDir.exists()) {
ohair@286 342 throw new BadCommandLineException(WscompileMessages.WSCOMPILE_NO_SUCH_DIRECTORY(sourceDir.getPath()));
ohair@286 343 }
ohair@286 344 return 2;
ohair@286 345 } else if (args[i].equals("-extension")) {
ohair@286 346 compatibilityMode = EXTENSION;
ohair@286 347 return 1;
ohair@286 348 } else if (args[i].startsWith("-help")) {
ohair@286 349 WeAreDone done = new WeAreDone();
ohair@286 350 done.initOptions(this);
ohair@286 351 throw done;
ohair@286 352 } else if (args[i].equals("-Xnocompile")) {
ohair@286 353 // -nocompile implies -keep. this is undocumented switch.
ohair@286 354 nocompile = true;
ohair@286 355 keep = true;
ohair@286 356 return 1;
ohair@286 357 } else if (args[i].equals("-encoding")) {
ohair@286 358 encoding = requireArgument("-encoding", args, ++i);
ohair@286 359 try {
ohair@286 360 if (!Charset.isSupported(encoding)) {
ohair@286 361 throw new BadCommandLineException(WscompileMessages.WSCOMPILE_UNSUPPORTED_ENCODING(encoding));
ohair@286 362 }
ohair@286 363 } catch (IllegalCharsetNameException icne) {
ohair@286 364 throw new BadCommandLineException(WscompileMessages.WSCOMPILE_UNSUPPORTED_ENCODING(encoding));
ohair@286 365 }
ohair@286 366 return 2;
mkos@408 367 } else if (args[i].equals("-disableXmlSecurity")) {
mkos@408 368 disableXmlSecurity();
mkos@408 369 return 1;
mkos@408 370 } else if (args[i].startsWith("-J")) {
mkos@408 371 if (javacOptions == null) {
mkos@408 372 javacOptions = new ArrayList<String>();
mkos@408 373 }
mkos@408 374 javacOptions.add(args[i].substring(2));
alanb@368 375 return 1;
ohair@286 376 }
ohair@286 377 return 0;
ohair@286 378 }
ohair@286 379
mkos@408 380 // protected method to allow overriding
mkos@408 381 protected void disableXmlSecurity() {
mkos@408 382 disableXmlSecurity= true;
mkos@408 383 }
mkos@408 384
ohair@286 385 /**
ohair@286 386 * Obtains an operand and reports an error if it's not there.
ohair@286 387 */
ohair@286 388 public String requireArgument(String optionName, String[] args, int i) throws BadCommandLineException {
ohair@286 389 //if (i == args.length || args[i].startsWith("-")) {
ohair@286 390 if (args[i].startsWith("-")) {
ohair@286 391 throw new BadCommandLineException(WscompileMessages.WSCOMPILE_MISSING_OPTION_ARGUMENT(optionName));
ohair@286 392 }
ohair@286 393 return args[i];
ohair@286 394 }
ohair@286 395
mkos@408 396 List<String> getJavacOptions(List<String> existingOptions, WsimportListener listener) {
mkos@408 397 List<String> result = new ArrayList<String>();
mkos@408 398 for (String o: javacOptions) {
mkos@408 399 if (o.contains("=") && !o.startsWith("A")) {
mkos@408 400 int i = o.indexOf('=');
mkos@408 401 String key = o.substring(0, i);
mkos@408 402 if (existingOptions.contains(key)) {
mkos@408 403 listener.message(WscompileMessages.WSCOMPILE_EXISTING_OPTION(key));
mkos@408 404 } else {
mkos@408 405 result.add(key);
mkos@408 406 result.add(o.substring(i + 1));
mkos@408 407 }
mkos@408 408 } else {
mkos@408 409 if (existingOptions.contains(o)) {
mkos@408 410 listener.message(WscompileMessages.WSCOMPILE_EXISTING_OPTION(o));
mkos@408 411 } else {
mkos@408 412 result.add(o);
mkos@408 413 }
mkos@408 414 }
mkos@408 415 }
mkos@408 416 return result;
mkos@408 417 }
ohair@286 418
ohair@286 419 /**
ohair@286 420 * Used to signal that we've finished processing.
ohair@286 421 */
ohair@286 422 public static final class WeAreDone extends BadCommandLineException {}
ohair@286 423
ohair@286 424 /**
ohair@286 425 * Get a URLClassLoader from using the classpath
ohair@286 426 */
ohair@286 427 public ClassLoader getClassLoader() {
ohair@286 428 if (classLoader == null) {
ohair@286 429 classLoader =
ohair@286 430 new URLClassLoader(pathToURLs(classpath),
ohair@286 431 this.getClass().getClassLoader());
ohair@286 432 }
ohair@286 433 return classLoader;
ohair@286 434 }
ohair@286 435
ohair@286 436 /**
ohair@286 437 * Utility method for converting a search path string to an array
ohair@286 438 * of directory and JAR file URLs.
ohair@286 439 *
ohair@286 440 * @param path the search path string
ohair@286 441 * @return the resulting array of directory and JAR file URLs
ohair@286 442 */
ohair@286 443 public static URL[] pathToURLs(String path) {
ohair@286 444 StringTokenizer st = new StringTokenizer(path, File.pathSeparator);
ohair@286 445 URL[] urls = new URL[st.countTokens()];
ohair@286 446 int count = 0;
ohair@286 447 while (st.hasMoreTokens()) {
ohair@286 448 URL url = fileToURL(new File(st.nextToken()));
ohair@286 449 if (url != null) {
ohair@286 450 urls[count++] = url;
ohair@286 451 }
ohair@286 452 }
ohair@286 453 if (urls.length != count) {
ohair@286 454 URL[] tmp = new URL[count];
ohair@286 455 System.arraycopy(urls, 0, tmp, 0, count);
ohair@286 456 urls = tmp;
ohair@286 457 }
ohair@286 458 return urls;
ohair@286 459 }
ohair@286 460
ohair@286 461 /**
ohair@286 462 * Returns the directory or JAR file URL corresponding to the specified
ohair@286 463 * local file name.
ohair@286 464 *
ohair@286 465 * @param file the File object
ohair@286 466 * @return the resulting directory or JAR file URL, or null if unknown
ohair@286 467 */
ohair@286 468 public static URL fileToURL(File file) {
ohair@286 469 String name;
ohair@286 470 try {
ohair@286 471 name = file.getCanonicalPath();
ohair@286 472 } catch (IOException e) {
ohair@286 473 name = file.getAbsolutePath();
ohair@286 474 }
ohair@286 475 name = name.replace(File.separatorChar, '/');
ohair@286 476 if (!name.startsWith("/")) {
ohair@286 477 name = "/" + name;
ohair@286 478 }
ohair@286 479
ohair@286 480 // If the file does not exist, then assume that it's a directory
ohair@286 481 if (!file.isFile()) {
ohair@286 482 name = name + "/";
ohair@286 483 }
ohair@286 484 try {
ohair@286 485 return new URL("file", "", name);
ohair@286 486 } catch (MalformedURLException e) {
ohair@286 487 throw new IllegalArgumentException("file");
ohair@286 488 }
ohair@286 489 }
ohair@286 490
ohair@286 491 }

mercurial