1/*
2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.tools.internal.xjc;
27
28import java.io.Closeable;
29import java.lang.reflect.InvocationTargetException;
30import java.lang.reflect.Method;
31import java.net.URLClassLoader;
32
33/**
34 * A shabby driver to invoke XJC1 or XJC2 depending on the command line switch.
35 *
36 * <p>
37 * This class is compiled with -source 1.2 so that we can report a nice
38 * user-friendly "you require Tiger" error message.
39 *
40 * @author Kohsuke Kawaguchi
41 */
42public class XJCFacade {
43
44    private static final String JDK6_REQUIRED = "XJC requires JDK 6.0 or later. Please download it from http://www.oracle.com/technetwork/java/javase/downloads";
45
46    public static void main(String[] args) throws Throwable {
47        String v = "2.0";      // by default, we go 2.0
48
49        for (int i = 0; i < args.length; i++) {
50            if (args[i].equals("-source")) {
51                if (i + 1 < args.length) {
52                    v = parseVersion(args[i + 1]);
53                }
54            }
55        }
56
57        ClassLoader oldContextCl = SecureLoader.getContextClassLoader();
58        try {
59            ClassLoader cl = ClassLoaderBuilder.createProtectiveClassLoader(SecureLoader.getClassClassLoader(XJCFacade.class), v);
60            SecureLoader.setContextClassLoader(cl);
61            Class<?> driver = cl.loadClass("com.sun.tools.internal.xjc.Driver");
62            Method mainMethod = driver.getDeclaredMethod("main", new Class[]{String[].class});
63            try {
64                mainMethod.invoke(null, new Object[]{args});
65            } catch (InvocationTargetException e) {
66                if (e.getTargetException() != null) {
67                    throw e.getTargetException();
68                }
69            }
70        } catch (UnsupportedClassVersionError e) {
71            System.err.println(JDK6_REQUIRED);
72        } finally {
73            ClassLoader cl = SecureLoader.getContextClassLoader();
74            SecureLoader.setContextClassLoader(oldContextCl);
75
76            //close/cleanup all classLoaders but the one which loaded this class
77            while (cl != null && !oldContextCl.equals(cl)) {
78                if (cl instanceof Closeable) {
79                    //JDK7+, ParallelWorldClassLoader
80                    ((Closeable) cl).close();
81                } else {
82                    if (cl instanceof URLClassLoader) {
83                        //JDK6 - API jars are loaded by instance of URLClassLoader
84                        //so use proprietary API to release holded resources
85                        try {
86                            Class<?> clUtil = oldContextCl.loadClass("sun.misc.ClassLoaderUtil");
87                            Method release = clUtil.getDeclaredMethod("releaseLoader", URLClassLoader.class);
88                            release.invoke(null, cl);
89                        } catch (ClassNotFoundException ex) {
90                            //not Sun JDK 6, ignore
91                            System.err.println(JDK6_REQUIRED);
92                        }
93                    }
94                }
95                cl = SecureLoader.getParentClassLoader(cl);
96            }
97        }
98    }
99
100    public static String parseVersion(String version) {
101        // no other versions supported as of now
102        return "2.0";
103    }
104}
105