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

Tue, 06 Mar 2012 16:09:35 -0800

author
ohair
date
Tue, 06 Mar 2012 16:09:35 -0800
changeset 286
f50545b5e2f1
child 368
0989ad8c0860
permissions
-rw-r--r--

7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom

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

mercurial