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

mercurial