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