ohair@286: /* alanb@368: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. ohair@286: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ohair@286: * ohair@286: * This code is free software; you can redistribute it and/or modify it ohair@286: * under the terms of the GNU General Public License version 2 only, as ohair@286: * published by the Free Software Foundation. Oracle designates this ohair@286: * particular file as subject to the "Classpath" exception as provided ohair@286: * by Oracle in the LICENSE file that accompanied this code. ohair@286: * ohair@286: * This code is distributed in the hope that it will be useful, but WITHOUT ohair@286: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ohair@286: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ohair@286: * version 2 for more details (a copy is included in the LICENSE file that ohair@286: * accompanied this code). ohair@286: * ohair@286: * You should have received a copy of the GNU General Public License version ohair@286: * 2 along with this work; if not, write to the Free Software Foundation, ohair@286: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ohair@286: * ohair@286: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@286: * or visit www.oracle.com if you need additional information or have any ohair@286: * questions. ohair@286: */ ohair@286: ohair@286: package com.sun.tools.internal.jxc; ohair@286: ohair@286: import com.sun.tools.internal.jxc.ap.Options; ohair@286: import com.sun.tools.internal.xjc.BadCommandLineException; ohair@286: import com.sun.xml.internal.bind.util.Which; ohair@286: ohair@286: import javax.lang.model.SourceVersion; ohair@286: import javax.tools.DiagnosticCollector; ohair@286: import javax.tools.JavaCompiler; ohair@286: import javax.tools.JavaFileObject; ohair@286: import javax.tools.OptionChecker; ohair@286: import javax.tools.StandardJavaFileManager; ohair@286: import javax.tools.ToolProvider; ohair@286: import javax.xml.bind.JAXBContext; ohair@286: import java.io.File; ohair@286: import java.lang.reflect.InvocationTargetException; ohair@286: import java.lang.reflect.Method; ohair@286: import java.net.MalformedURLException; ohair@286: import java.net.URISyntaxException; ohair@286: import java.net.URL; alanb@368: import java.net.URLClassLoader; ohair@286: import java.util.ArrayList; alanb@368: import java.util.Collection; ohair@286: import java.util.Collections; ohair@286: import java.util.List; ohair@286: import java.util.logging.Level; ohair@286: import java.util.logging.Logger; ohair@286: ohair@286: /** ohair@286: * CLI entry-point to the schema generator. ohair@286: * ohair@286: * @author Bhakti Mehta ohair@286: */ ohair@286: public class SchemaGenerator { ohair@286: /** ohair@286: * Runs the schema generator. ohair@286: */ ohair@286: public static void main(String[] args) throws Exception { ohair@286: System.exit(run(args)); ohair@286: } ohair@286: ohair@286: public static int run(String[] args) throws Exception { ohair@286: try { ohair@286: ClassLoader cl = SecureLoader.getClassClassLoader(SchemaGenerator.class); ohair@286: if (cl==null) { ohair@286: cl = SecureLoader.getSystemClassLoader(); ohair@286: } ohair@286: return run(args, cl); ohair@286: } catch(Exception e) { ohair@286: System.err.println(e.getMessage()); ohair@286: return -1; ohair@286: } ohair@286: } ohair@286: ohair@286: /** ohair@286: * Runs the schema generator. ohair@286: * ohair@286: * @param classLoader ohair@286: * the schema generator will run in this classLoader. ohair@286: * It needs to be able to load annotation processing and JAXB RI classes. Note that ohair@286: * JAXB RI classes refer to annotation processing classes. Must not be null. ohair@286: * ohair@286: * @return ohair@286: * exit code. 0 if success. ohair@286: * ohair@286: */ ohair@286: public static int run(String[] args, ClassLoader classLoader) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException { ohair@286: final Options options = new Options(); ohair@286: if (args.length ==0) { ohair@286: usage(); ohair@286: return -1; ohair@286: } ohair@286: for (String arg : args) { ohair@286: if (arg.equals("-help")) { ohair@286: usage(); ohair@286: return -1; ohair@286: } ohair@286: ohair@286: if (arg.equals("-version")) { ohair@286: System.out.println(Messages.VERSION.format()); ohair@286: return -1; ohair@286: } ohair@286: ohair@286: if (arg.equals("-fullversion")) { ohair@286: System.out.println(Messages.FULLVERSION.format()); ohair@286: return -1; ohair@286: } ohair@286: ohair@286: } ohair@286: ohair@286: try { ohair@286: options.parseArguments(args); ohair@286: } catch (BadCommandLineException e) { ohair@286: // there was an error in the command line. ohair@286: // print usage and abort. ohair@286: System.out.println(e.getMessage()); ohair@286: System.out.println(); ohair@286: usage(); ohair@286: return -1; ohair@286: } ohair@286: ohair@286: Class schemagenRunner = classLoader.loadClass(Runner.class.getName()); ohair@286: Method compileMethod = schemagenRunner.getDeclaredMethod("compile",String[].class,File.class); ohair@286: ohair@286: List aptargs = new ArrayList(); ohair@286: ohair@286: if (options.encoding != null) { ohair@286: aptargs.add("-encoding"); ohair@286: aptargs.add(options.encoding); ohair@286: } ohair@286: ohair@286: aptargs.add("-cp"); alanb@368: aptargs.add(setClasspath(options.classpath)); // set original classpath + jaxb-api to be visible to annotation processor ohair@286: ohair@286: if(options.targetDir!=null) { ohair@286: aptargs.add("-d"); ohair@286: aptargs.add(options.targetDir.getPath()); ohair@286: } ohair@286: ohair@286: aptargs.addAll(options.arguments); ohair@286: ohair@286: String[] argsarray = aptargs.toArray(new String[aptargs.size()]); ohair@286: return ((Boolean) compileMethod.invoke(null, argsarray, options.episodeFile)) ? 0 : 1; ohair@286: } ohair@286: alanb@368: private static String setClasspath(String givenClasspath) { alanb@368: StringBuilder cp = new StringBuilder(); alanb@368: appendPath(cp, givenClasspath); alanb@368: ClassLoader cl = Thread.currentThread().getContextClassLoader(); alanb@368: while (cl != null) { alanb@368: if (cl instanceof URLClassLoader) { alanb@368: for (URL url : ((URLClassLoader) cl).getURLs()) { alanb@368: appendPath(cp, url.getPath()); alanb@368: } alanb@368: } alanb@368: cl = cl.getParent(); alanb@368: } alanb@368: alanb@368: appendPath(cp, findJaxbApiJar()); alanb@368: return cp.toString(); alanb@368: } alanb@368: alanb@368: private static void appendPath(StringBuilder cp, String url) { alanb@368: if (url == null || url.trim().isEmpty()) alanb@368: return; alanb@368: if (cp.length() != 0) alanb@368: cp.append(File.pathSeparatorChar); alanb@368: cp.append(url); alanb@368: } alanb@368: ohair@286: /** ohair@286: * Computes the file system path of jaxb-api.jar so that ohair@286: * Annotation Processing will see them in the -cp option. ohair@286: * ohair@286: *

ohair@286: * In Java, you can't do this reliably (for that matter there's no guarantee ohair@286: * that such a jar file exists, such as in Glassfish), so we do the best we can. ohair@286: * ohair@286: * @return ohair@286: * null if failed to locate it. ohair@286: */ alanb@368: private static String findJaxbApiJar() { ohair@286: String url = Which.which(JAXBContext.class); ohair@286: if(url==null) return null; // impossible, but hey, let's be defensive ohair@286: ohair@286: if(!url.startsWith("jar:") || url.lastIndexOf('!')==-1) ohair@286: // no jar file ohair@286: return null; ohair@286: ohair@286: String jarFileUrl = url.substring(4,url.lastIndexOf('!')); ohair@286: if(!jarFileUrl.startsWith("file:")) ohair@286: return null; // not from file system ohair@286: ohair@286: try { ohair@286: File f = new File(new URL(jarFileUrl).toURI()); ohair@286: if (f.exists() && f.getName().endsWith(".jar")) { // see 6510966 alanb@368: return f.getPath(); ohair@286: } ohair@286: f = new File(new URL(jarFileUrl).getFile()); ohair@286: if (f.exists() && f.getName().endsWith(".jar")) { // this is here for potential backw. compatibility issues alanb@368: return f.getPath(); ohair@286: } ohair@286: } catch (URISyntaxException ex) { ohair@286: Logger.getLogger(SchemaGenerator.class.getName()).log(Level.SEVERE, null, ex); ohair@286: } catch (MalformedURLException ex) { ohair@286: Logger.getLogger(SchemaGenerator.class.getName()).log(Level.SEVERE, null, ex); ohair@286: } ohair@286: return null; ohair@286: } ohair@286: ohair@286: private static void usage( ) { ohair@286: System.out.println(Messages.USAGE.format()); ohair@286: } ohair@286: ohair@286: public static final class Runner { ohair@286: public static boolean compile(String[] args, File episode) throws Exception { ohair@286: ohair@286: JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); ohair@286: DiagnosticCollector diagnostics = new DiagnosticCollector(); ohair@286: StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null); ohair@286: JavacOptions options = JavacOptions.parse(compiler, fileManager, args); ohair@286: List unrecognizedOptions = options.getUnrecognizedOptions(); ohair@286: if (!unrecognizedOptions.isEmpty()) ohair@286: Logger.getLogger(SchemaGenerator.class.getName()).log(Level.WARNING, "Unrecognized options found: {0}", unrecognizedOptions); ohair@286: Iterable compilationUnits = fileManager.getJavaFileObjectsFromFiles(options.getFiles()); ohair@286: JavaCompiler.CompilationTask task = compiler.getTask( ohair@286: null, ohair@286: fileManager, ohair@286: diagnostics, ohair@286: options.getRecognizedOptions(), ohair@286: options.getClassNames(), ohair@286: compilationUnits); ohair@286: com.sun.tools.internal.jxc.ap.SchemaGenerator r = new com.sun.tools.internal.jxc.ap.SchemaGenerator(); ohair@286: if (episode != null) ohair@286: r.setEpisodeFile(episode); ohair@286: task.setProcessors(Collections.singleton(r)); ohair@286: return task.call(); ohair@286: } ohair@286: } ohair@286: ohair@286: /** ohair@286: * @author Peter von der Ahe ohair@286: */ ohair@286: private static final class JavacOptions { ohair@286: private final List recognizedOptions; ohair@286: private final List classNames; ohair@286: private final List files; ohair@286: private final List unrecognizedOptions; ohair@286: ohair@286: private JavacOptions(List recognizedOptions, List classNames, List files, ohair@286: List unrecognizedOptions) { ohair@286: this.recognizedOptions = recognizedOptions; ohair@286: this.classNames = classNames; ohair@286: this.files = files; ohair@286: this.unrecognizedOptions = unrecognizedOptions; ohair@286: } ohair@286: ohair@286: public static JavacOptions parse(OptionChecker primary, OptionChecker secondary, String... arguments) { ohair@286: List recognizedOptions = new ArrayList(); ohair@286: List unrecognizedOptions = new ArrayList(); ohair@286: List classNames = new ArrayList(); ohair@286: List files = new ArrayList(); ohair@286: for (int i = 0; i < arguments.length; i++) { ohair@286: String argument = arguments[i]; ohair@286: int optionCount = primary.isSupportedOption(argument); ohair@286: if (optionCount < 0) { ohair@286: optionCount = secondary.isSupportedOption(argument); ohair@286: } ohair@286: if (optionCount < 0) { ohair@286: File file = new File(argument); ohair@286: if (file.exists()) ohair@286: files.add(file); ohair@286: else if (SourceVersion.isName(argument)) ohair@286: classNames.add(argument); ohair@286: else ohair@286: unrecognizedOptions.add(argument); ohair@286: } else { ohair@286: for (int j = 0; j < optionCount + 1; j++) { ohair@286: int index = i + j; ohair@286: if (index == arguments.length) throw new IllegalArgumentException(argument); ohair@286: recognizedOptions.add(arguments[index]); ohair@286: } ohair@286: i += optionCount; ohair@286: } ohair@286: } ohair@286: return new JavacOptions(recognizedOptions, classNames, files, unrecognizedOptions); ohair@286: } ohair@286: ohair@286: /** ohair@286: * Returns the list of recognized options and their arguments. ohair@286: * ohair@286: * @return a list of options ohair@286: */ ohair@286: public List getRecognizedOptions() { ohair@286: return Collections.unmodifiableList(recognizedOptions); ohair@286: } ohair@286: ohair@286: /** ohair@286: * Returns the list of file names. ohair@286: * ohair@286: * @return a list of file names ohair@286: */ ohair@286: public List getFiles() { ohair@286: return Collections.unmodifiableList(files); ohair@286: } ohair@286: ohair@286: /** ohair@286: * Returns the list of class names. ohair@286: * ohair@286: * @return a list of class names ohair@286: */ ohair@286: public List getClassNames() { ohair@286: return Collections.unmodifiableList(classNames); ohair@286: } ohair@286: ohair@286: /** ohair@286: * Returns the list of unrecognized options. ohair@286: * ohair@286: * @return a list of unrecognized options ohair@286: */ ohair@286: public List getUnrecognizedOptions() { ohair@286: return Collections.unmodifiableList(unrecognizedOptions); ohair@286: } ohair@286: ohair@286: @Override ohair@286: public String toString() { ohair@286: return String.format("recognizedOptions = %s; classNames = %s; " + "files = %s; unrecognizedOptions = %s", recognizedOptions, classNames, files, unrecognizedOptions); ohair@286: } ohair@286: } ohair@286: }