001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.tools.jxc;
038:
039: import java.io.File;
040: import java.lang.reflect.InvocationTargetException;
041: import java.lang.reflect.Method;
042: import java.net.MalformedURLException;
043: import java.net.URL;
044: import java.util.ArrayList;
045: import java.util.List;
046:
047: import javax.xml.bind.JAXBContext;
048:
049: import com.sun.mirror.apt.AnnotationProcessorFactory;
050: import com.sun.tools.jxc.apt.Options;
051: import com.sun.tools.xjc.BadCommandLineException;
052: import com.sun.tools.xjc.api.util.APTClassLoader;
053: import com.sun.tools.xjc.api.util.ToolsJarNotFoundException;
054: import com.sun.xml.bind.util.Which;
055:
056: /**
057: * CLI entry-point to the schema generator.
058: *
059: * @author Bhakti Mehta
060: */
061: public class SchemaGenerator {
062: /**
063: * Runs the schema generator.
064: */
065: public static void main(String[] args) throws Exception {
066: System.exit(run(args));
067: }
068:
069: public static int run(String[] args) throws Exception {
070: try {
071: ClassLoader cl = SchemaGenerator.class.getClassLoader();
072: if (cl == null)
073: cl = ClassLoader.getSystemClassLoader();
074: ClassLoader classLoader = new APTClassLoader(cl,
075: packagePrefixes);
076: return run(args, classLoader);
077: } catch (ToolsJarNotFoundException e) {
078: System.err.println(e.getMessage());
079: return -1;
080: }
081: }
082:
083: /**
084: * List of package prefixes we want to load in the same package
085: */
086: private static final String[] packagePrefixes = {
087: "com.sun.tools.jxc.", "com.sun.tools.xjc.",
088: "com.sun.istack.tools.", "com.sun.tools.apt.",
089: "com.sun.tools.javac.", "com.sun.tools.javadoc.",
090: "com.sun.mirror." };
091:
092: /**
093: * Runs the schema generator.
094: *
095: * @param classLoader
096: * the schema generator will run in this classLoader.
097: * It needs to be able to load APT and JAXB RI classes. Note that
098: * JAXB RI classes refer to APT classes. Must not be null.
099: *
100: * @return
101: * exit code. 0 if success.
102: *
103: */
104: public static int run(String[] args, ClassLoader classLoader)
105: throws ClassNotFoundException, NoSuchMethodException,
106: IllegalAccessException, InvocationTargetException {
107: final Options options = new Options();
108: if (args.length == 0) {
109: usage();
110: return -1;
111: }
112: for (String arg : args) {
113: if (arg.equals("-help")) {
114: usage();
115: return -1;
116: }
117:
118: if (arg.equals("-version")) {
119: System.out.println(Messages.VERSION.format());
120: return -1;
121: }
122: }
123:
124: try {
125: options.parseArguments(args);
126: } catch (BadCommandLineException e) {
127: // there was an error in the command line.
128: // print usage and abort.
129: System.out.println(e.getMessage());
130: System.out.println();
131: usage();
132: return -1;
133: }
134:
135: Class schemagenRunner = classLoader.loadClass(Runner.class
136: .getName());
137: Method mainMethod = schemagenRunner.getDeclaredMethod("main",
138: String[].class, File.class);
139:
140: List<String> aptargs = new ArrayList<String>();
141:
142: if (hasClass(options.arguments))
143: aptargs.add("-XclassesAsDecls");
144:
145: // make jaxb-api.jar visible to classpath
146: File jaxbApi = findJaxbApiJar();
147: if (jaxbApi != null) {
148: if (options.classpath != null) {
149: options.classpath = options.classpath
150: + File.pathSeparatorChar + jaxbApi;
151: } else {
152: options.classpath = jaxbApi.getPath();
153: }
154: }
155:
156: aptargs.add("-cp");
157: aptargs.add(options.classpath);
158:
159: if (options.targetDir != null) {
160: aptargs.add("-d");
161: aptargs.add(options.targetDir.getPath());
162: }
163:
164: aptargs.addAll(options.arguments);
165:
166: String[] argsarray = aptargs
167: .toArray(new String[aptargs.size()]);
168: return (Integer) mainMethod.invoke(null, new Object[] {
169: argsarray, options.episodeFile });
170: }
171:
172: /**
173: * Computes the file system path of <tt>jaxb-api.jar</tt> so that
174: * APT will see them in the <tt>-cp</tt> option.
175: *
176: * <p>
177: * In Java, you can't do this reliably (for that matter there's no guarantee
178: * that such a jar file exists, such as in Glassfish), so we do the best we can.
179: *
180: * @return
181: * null if failed to locate it.
182: */
183: private static File findJaxbApiJar() {
184: String url = Which.which(JAXBContext.class);
185: if (url == null)
186: return null; // impossible, but hey, let's be defensive
187:
188: if (!url.startsWith("jar:") || url.lastIndexOf('!') == -1)
189: // no jar file
190: return null;
191:
192: String jarFileUrl = url.substring(4, url.lastIndexOf('!'));
193: if (!jarFileUrl.startsWith("file:"))
194: return null; // not from file system
195:
196: try {
197: File f = new File(new URL(jarFileUrl).getFile());
198: if (f.exists() && f.getName().endsWith(".jar"))
199: return f;
200: else
201: return null;
202: } catch (MalformedURLException e) {
203: return null; // impossible
204: }
205: }
206:
207: /**
208: * Returns true if the list of arguments have an argument
209: * that looks like a class name.
210: */
211: private static boolean hasClass(List<String> args) {
212: for (String arg : args) {
213: if (!arg.endsWith(".java"))
214: return true;
215: }
216: return false;
217: }
218:
219: private static void usage() {
220: System.out.println(Messages.USAGE.format());
221: }
222:
223: public static final class Runner {
224: public static int main(String[] args, File episode)
225: throws Exception {
226: ClassLoader cl = Runner.class.getClassLoader();
227: Class apt = cl.loadClass("com.sun.tools.apt.Main");
228: Method processMethod = apt.getMethod("process",
229: AnnotationProcessorFactory.class, String[].class);
230:
231: com.sun.tools.jxc.apt.SchemaGenerator r = new com.sun.tools.jxc.apt.SchemaGenerator();
232: if (episode != null)
233: r.setEpisodeFile(episode);
234: return (Integer) processMethod.invoke(null, r, args);
235: }
236: }
237: }
|