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