Tue, 06 Mar 2012 16:09:35 -0800
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.jxc; |
ohair@286 | 27 | |
ohair@286 | 28 | import com.sun.tools.internal.jxc.ap.Options; |
ohair@286 | 29 | import com.sun.tools.internal.xjc.BadCommandLineException; |
ohair@286 | 30 | import com.sun.tools.internal.xjc.api.util.ApClassLoader; |
ohair@286 | 31 | import com.sun.tools.internal.xjc.api.util.ToolsJarNotFoundException; |
ohair@286 | 32 | import com.sun.xml.internal.bind.util.Which; |
ohair@286 | 33 | |
ohair@286 | 34 | import javax.lang.model.SourceVersion; |
ohair@286 | 35 | import javax.tools.DiagnosticCollector; |
ohair@286 | 36 | import javax.tools.JavaCompiler; |
ohair@286 | 37 | import javax.tools.JavaFileObject; |
ohair@286 | 38 | import javax.tools.OptionChecker; |
ohair@286 | 39 | import javax.tools.StandardJavaFileManager; |
ohair@286 | 40 | import javax.tools.ToolProvider; |
ohair@286 | 41 | import javax.xml.bind.JAXBContext; |
ohair@286 | 42 | import java.io.File; |
ohair@286 | 43 | import java.lang.reflect.InvocationTargetException; |
ohair@286 | 44 | import java.lang.reflect.Method; |
ohair@286 | 45 | import java.net.MalformedURLException; |
ohair@286 | 46 | import java.net.URISyntaxException; |
ohair@286 | 47 | import java.net.URL; |
ohair@286 | 48 | import java.util.ArrayList; |
ohair@286 | 49 | import java.util.Collections; |
ohair@286 | 50 | import java.util.List; |
ohair@286 | 51 | import java.util.logging.Level; |
ohair@286 | 52 | import java.util.logging.Logger; |
ohair@286 | 53 | |
ohair@286 | 54 | /** |
ohair@286 | 55 | * CLI entry-point to the schema generator. |
ohair@286 | 56 | * |
ohair@286 | 57 | * @author Bhakti Mehta |
ohair@286 | 58 | */ |
ohair@286 | 59 | public class SchemaGenerator { |
ohair@286 | 60 | /** |
ohair@286 | 61 | * Runs the schema generator. |
ohair@286 | 62 | */ |
ohair@286 | 63 | public static void main(String[] args) throws Exception { |
ohair@286 | 64 | System.exit(run(args)); |
ohair@286 | 65 | } |
ohair@286 | 66 | |
ohair@286 | 67 | public static int run(String[] args) throws Exception { |
ohair@286 | 68 | try { |
ohair@286 | 69 | ClassLoader cl = SecureLoader.getClassClassLoader(SchemaGenerator.class); |
ohair@286 | 70 | if (cl==null) { |
ohair@286 | 71 | cl = SecureLoader.getSystemClassLoader(); |
ohair@286 | 72 | } |
ohair@286 | 73 | // ClassLoader classLoader = new ApClassLoader(cl, packagePrefixes); // todo: check if can be removed |
ohair@286 | 74 | return run(args, cl); |
ohair@286 | 75 | } catch(Exception e) { |
ohair@286 | 76 | System.err.println(e.getMessage()); |
ohair@286 | 77 | return -1; |
ohair@286 | 78 | } |
ohair@286 | 79 | } |
ohair@286 | 80 | |
ohair@286 | 81 | /** |
ohair@286 | 82 | * List of package prefixes we want to load in the same package |
ohair@286 | 83 | */ |
ohair@286 | 84 | private static final String[] packagePrefixes = { |
ohair@286 | 85 | "com.sun.tools.internal.jxc.", |
ohair@286 | 86 | "com.sun.tools.internal.xjc.", |
ohair@286 | 87 | "com.sun.istack.internal.tools.", |
ohair@286 | 88 | "com.sun.tools.javac.", |
ohair@286 | 89 | "com.sun.tools.javadoc.", |
ohair@286 | 90 | "javax.annotation.processing.", |
ohair@286 | 91 | "javax.lang.model." |
ohair@286 | 92 | }; |
ohair@286 | 93 | |
ohair@286 | 94 | /** |
ohair@286 | 95 | * Runs the schema generator. |
ohair@286 | 96 | * |
ohair@286 | 97 | * @param classLoader |
ohair@286 | 98 | * the schema generator will run in this classLoader. |
ohair@286 | 99 | * It needs to be able to load annotation processing and JAXB RI classes. Note that |
ohair@286 | 100 | * JAXB RI classes refer to annotation processing classes. Must not be null. |
ohair@286 | 101 | * |
ohair@286 | 102 | * @return |
ohair@286 | 103 | * exit code. 0 if success. |
ohair@286 | 104 | * |
ohair@286 | 105 | */ |
ohair@286 | 106 | public static int run(String[] args, ClassLoader classLoader) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException { |
ohair@286 | 107 | final Options options = new Options(); |
ohair@286 | 108 | if (args.length ==0) { |
ohair@286 | 109 | usage(); |
ohair@286 | 110 | return -1; |
ohair@286 | 111 | } |
ohair@286 | 112 | for (String arg : args) { |
ohair@286 | 113 | if (arg.equals("-help")) { |
ohair@286 | 114 | usage(); |
ohair@286 | 115 | return -1; |
ohair@286 | 116 | } |
ohair@286 | 117 | |
ohair@286 | 118 | if (arg.equals("-version")) { |
ohair@286 | 119 | System.out.println(Messages.VERSION.format()); |
ohair@286 | 120 | return -1; |
ohair@286 | 121 | } |
ohair@286 | 122 | |
ohair@286 | 123 | if (arg.equals("-fullversion")) { |
ohair@286 | 124 | System.out.println(Messages.FULLVERSION.format()); |
ohair@286 | 125 | return -1; |
ohair@286 | 126 | } |
ohair@286 | 127 | |
ohair@286 | 128 | } |
ohair@286 | 129 | |
ohair@286 | 130 | try { |
ohair@286 | 131 | options.parseArguments(args); |
ohair@286 | 132 | } catch (BadCommandLineException e) { |
ohair@286 | 133 | // there was an error in the command line. |
ohair@286 | 134 | // print usage and abort. |
ohair@286 | 135 | System.out.println(e.getMessage()); |
ohair@286 | 136 | System.out.println(); |
ohair@286 | 137 | usage(); |
ohair@286 | 138 | return -1; |
ohair@286 | 139 | } |
ohair@286 | 140 | |
ohair@286 | 141 | Class schemagenRunner = classLoader.loadClass(Runner.class.getName()); |
ohair@286 | 142 | Method compileMethod = schemagenRunner.getDeclaredMethod("compile",String[].class,File.class); |
ohair@286 | 143 | |
ohair@286 | 144 | List<String> aptargs = new ArrayList<String>(); |
ohair@286 | 145 | |
ohair@286 | 146 | if (options.encoding != null) { |
ohair@286 | 147 | aptargs.add("-encoding"); |
ohair@286 | 148 | aptargs.add(options.encoding); |
ohair@286 | 149 | } |
ohair@286 | 150 | |
ohair@286 | 151 | // make jaxb-api.jar visible to classpath |
ohair@286 | 152 | File jaxbApi = findJaxbApiJar(); |
ohair@286 | 153 | if(jaxbApi!=null) { |
ohair@286 | 154 | if(options.classpath!=null) { |
ohair@286 | 155 | options.classpath = options.classpath+File.pathSeparatorChar+jaxbApi; |
ohair@286 | 156 | } else { |
ohair@286 | 157 | options.classpath = jaxbApi.getPath(); |
ohair@286 | 158 | } |
ohair@286 | 159 | } |
ohair@286 | 160 | |
ohair@286 | 161 | aptargs.add("-cp"); |
ohair@286 | 162 | aptargs.add(options.classpath); |
ohair@286 | 163 | |
ohair@286 | 164 | if(options.targetDir!=null) { |
ohair@286 | 165 | aptargs.add("-d"); |
ohair@286 | 166 | aptargs.add(options.targetDir.getPath()); |
ohair@286 | 167 | } |
ohair@286 | 168 | |
ohair@286 | 169 | aptargs.addAll(options.arguments); |
ohair@286 | 170 | |
ohair@286 | 171 | String[] argsarray = aptargs.toArray(new String[aptargs.size()]); |
ohair@286 | 172 | return ((Boolean) compileMethod.invoke(null, argsarray, options.episodeFile)) ? 0 : 1; |
ohair@286 | 173 | } |
ohair@286 | 174 | |
ohair@286 | 175 | /** |
ohair@286 | 176 | * Computes the file system path of <tt>jaxb-api.jar</tt> so that |
ohair@286 | 177 | * Annotation Processing will see them in the <tt>-cp</tt> option. |
ohair@286 | 178 | * |
ohair@286 | 179 | * <p> |
ohair@286 | 180 | * In Java, you can't do this reliably (for that matter there's no guarantee |
ohair@286 | 181 | * that such a jar file exists, such as in Glassfish), so we do the best we can. |
ohair@286 | 182 | * |
ohair@286 | 183 | * @return |
ohair@286 | 184 | * null if failed to locate it. |
ohair@286 | 185 | */ |
ohair@286 | 186 | private static File findJaxbApiJar() { |
ohair@286 | 187 | String url = Which.which(JAXBContext.class); |
ohair@286 | 188 | if(url==null) return null; // impossible, but hey, let's be defensive |
ohair@286 | 189 | |
ohair@286 | 190 | if(!url.startsWith("jar:") || url.lastIndexOf('!')==-1) |
ohair@286 | 191 | // no jar file |
ohair@286 | 192 | return null; |
ohair@286 | 193 | |
ohair@286 | 194 | String jarFileUrl = url.substring(4,url.lastIndexOf('!')); |
ohair@286 | 195 | if(!jarFileUrl.startsWith("file:")) |
ohair@286 | 196 | return null; // not from file system |
ohair@286 | 197 | |
ohair@286 | 198 | try { |
ohair@286 | 199 | File f = new File(new URL(jarFileUrl).toURI()); |
ohair@286 | 200 | if (f.exists() && f.getName().endsWith(".jar")) { // see 6510966 |
ohair@286 | 201 | return f; |
ohair@286 | 202 | } |
ohair@286 | 203 | f = new File(new URL(jarFileUrl).getFile()); |
ohair@286 | 204 | if (f.exists() && f.getName().endsWith(".jar")) { // this is here for potential backw. compatibility issues |
ohair@286 | 205 | return f; |
ohair@286 | 206 | } |
ohair@286 | 207 | } catch (URISyntaxException ex) { |
ohair@286 | 208 | Logger.getLogger(SchemaGenerator.class.getName()).log(Level.SEVERE, null, ex); |
ohair@286 | 209 | } catch (MalformedURLException ex) { |
ohair@286 | 210 | Logger.getLogger(SchemaGenerator.class.getName()).log(Level.SEVERE, null, ex); |
ohair@286 | 211 | } |
ohair@286 | 212 | return null; |
ohair@286 | 213 | } |
ohair@286 | 214 | |
ohair@286 | 215 | /** |
ohair@286 | 216 | * Returns true if the list of arguments have an argument |
ohair@286 | 217 | * that looks like a class name. |
ohair@286 | 218 | */ |
ohair@286 | 219 | private static boolean hasClass(List<String> args) { |
ohair@286 | 220 | for (String arg : args) { |
ohair@286 | 221 | if(!arg.endsWith(".java")) |
ohair@286 | 222 | return true; |
ohair@286 | 223 | } |
ohair@286 | 224 | return false; |
ohair@286 | 225 | } |
ohair@286 | 226 | |
ohair@286 | 227 | private static void usage( ) { |
ohair@286 | 228 | System.out.println(Messages.USAGE.format()); |
ohair@286 | 229 | } |
ohair@286 | 230 | |
ohair@286 | 231 | public static final class Runner { |
ohair@286 | 232 | public static boolean compile(String[] args, File episode) throws Exception { |
ohair@286 | 233 | |
ohair@286 | 234 | JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); |
ohair@286 | 235 | DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>(); |
ohair@286 | 236 | StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null); |
ohair@286 | 237 | JavacOptions options = JavacOptions.parse(compiler, fileManager, args); |
ohair@286 | 238 | List<String> unrecognizedOptions = options.getUnrecognizedOptions(); |
ohair@286 | 239 | if (!unrecognizedOptions.isEmpty()) |
ohair@286 | 240 | Logger.getLogger(SchemaGenerator.class.getName()).log(Level.WARNING, "Unrecognized options found: {0}", unrecognizedOptions); |
ohair@286 | 241 | Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(options.getFiles()); |
ohair@286 | 242 | JavaCompiler.CompilationTask task = compiler.getTask( |
ohair@286 | 243 | null, |
ohair@286 | 244 | fileManager, |
ohair@286 | 245 | diagnostics, |
ohair@286 | 246 | options.getRecognizedOptions(), |
ohair@286 | 247 | options.getClassNames(), |
ohair@286 | 248 | compilationUnits); |
ohair@286 | 249 | com.sun.tools.internal.jxc.ap.SchemaGenerator r = new com.sun.tools.internal.jxc.ap.SchemaGenerator(); |
ohair@286 | 250 | if (episode != null) |
ohair@286 | 251 | r.setEpisodeFile(episode); |
ohair@286 | 252 | task.setProcessors(Collections.singleton(r)); |
ohair@286 | 253 | return task.call(); |
ohair@286 | 254 | } |
ohair@286 | 255 | } |
ohair@286 | 256 | |
ohair@286 | 257 | /** |
ohair@286 | 258 | * @author Peter von der Ahe |
ohair@286 | 259 | */ |
ohair@286 | 260 | private static final class JavacOptions { |
ohair@286 | 261 | private final List<String> recognizedOptions; |
ohair@286 | 262 | private final List<String> classNames; |
ohair@286 | 263 | private final List<File> files; |
ohair@286 | 264 | private final List<String> unrecognizedOptions; |
ohair@286 | 265 | |
ohair@286 | 266 | private JavacOptions(List<String> recognizedOptions, List<String> classNames, List<File> files, |
ohair@286 | 267 | List<String> unrecognizedOptions) { |
ohair@286 | 268 | this.recognizedOptions = recognizedOptions; |
ohair@286 | 269 | this.classNames = classNames; |
ohair@286 | 270 | this.files = files; |
ohair@286 | 271 | this.unrecognizedOptions = unrecognizedOptions; |
ohair@286 | 272 | } |
ohair@286 | 273 | |
ohair@286 | 274 | public static JavacOptions parse(OptionChecker primary, OptionChecker secondary, String... arguments) { |
ohair@286 | 275 | List<String> recognizedOptions = new ArrayList<String>(); |
ohair@286 | 276 | List<String> unrecognizedOptions = new ArrayList<String>(); |
ohair@286 | 277 | List<String> classNames = new ArrayList<String>(); |
ohair@286 | 278 | List<File> files = new ArrayList<File>(); |
ohair@286 | 279 | for (int i = 0; i < arguments.length; i++) { |
ohair@286 | 280 | String argument = arguments[i]; |
ohair@286 | 281 | int optionCount = primary.isSupportedOption(argument); |
ohair@286 | 282 | if (optionCount < 0) { |
ohair@286 | 283 | optionCount = secondary.isSupportedOption(argument); |
ohair@286 | 284 | } |
ohair@286 | 285 | if (optionCount < 0) { |
ohair@286 | 286 | File file = new File(argument); |
ohair@286 | 287 | if (file.exists()) |
ohair@286 | 288 | files.add(file); |
ohair@286 | 289 | else if (SourceVersion.isName(argument)) |
ohair@286 | 290 | classNames.add(argument); |
ohair@286 | 291 | else |
ohair@286 | 292 | unrecognizedOptions.add(argument); |
ohair@286 | 293 | } else { |
ohair@286 | 294 | for (int j = 0; j < optionCount + 1; j++) { |
ohair@286 | 295 | int index = i + j; |
ohair@286 | 296 | if (index == arguments.length) throw new IllegalArgumentException(argument); |
ohair@286 | 297 | recognizedOptions.add(arguments[index]); |
ohair@286 | 298 | } |
ohair@286 | 299 | i += optionCount; |
ohair@286 | 300 | } |
ohair@286 | 301 | } |
ohair@286 | 302 | return new JavacOptions(recognizedOptions, classNames, files, unrecognizedOptions); |
ohair@286 | 303 | } |
ohair@286 | 304 | |
ohair@286 | 305 | /** |
ohair@286 | 306 | * Returns the list of recognized options and their arguments. |
ohair@286 | 307 | * |
ohair@286 | 308 | * @return a list of options |
ohair@286 | 309 | */ |
ohair@286 | 310 | public List<String> getRecognizedOptions() { |
ohair@286 | 311 | return Collections.unmodifiableList(recognizedOptions); |
ohair@286 | 312 | } |
ohair@286 | 313 | |
ohair@286 | 314 | /** |
ohair@286 | 315 | * Returns the list of file names. |
ohair@286 | 316 | * |
ohair@286 | 317 | * @return a list of file names |
ohair@286 | 318 | */ |
ohair@286 | 319 | public List<File> getFiles() { |
ohair@286 | 320 | return Collections.unmodifiableList(files); |
ohair@286 | 321 | } |
ohair@286 | 322 | |
ohair@286 | 323 | /** |
ohair@286 | 324 | * Returns the list of class names. |
ohair@286 | 325 | * |
ohair@286 | 326 | * @return a list of class names |
ohair@286 | 327 | */ |
ohair@286 | 328 | public List<String> getClassNames() { |
ohair@286 | 329 | return Collections.unmodifiableList(classNames); |
ohair@286 | 330 | } |
ohair@286 | 331 | |
ohair@286 | 332 | /** |
ohair@286 | 333 | * Returns the list of unrecognized options. |
ohair@286 | 334 | * |
ohair@286 | 335 | * @return a list of unrecognized options |
ohair@286 | 336 | */ |
ohair@286 | 337 | public List<String> getUnrecognizedOptions() { |
ohair@286 | 338 | return Collections.unmodifiableList(unrecognizedOptions); |
ohair@286 | 339 | } |
ohair@286 | 340 | |
ohair@286 | 341 | @Override |
ohair@286 | 342 | public String toString() { |
ohair@286 | 343 | return String.format("recognizedOptions = %s; classNames = %s; " + "files = %s; unrecognizedOptions = %s", recognizedOptions, classNames, files, unrecognizedOptions); |
ohair@286 | 344 | } |
ohair@286 | 345 | } |
ohair@286 | 346 | } |