| 1 | /* |
| 2 | * Copyright 2005-2006 Sun Microsystems, Inc. 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. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package javax.tools; |
| 27 | |
| 28 | import java.io.File; |
| 29 | import java.net.URL; |
| 30 | import java.net.URLClassLoader; |
| 31 | import java.net.MalformedURLException; |
| 32 | import java.util.Locale; |
| 33 | import java.util.logging.Logger; |
| 34 | import java.util.logging.Level; |
| 35 | import static java.util.logging.Level.*; |
| 36 | |
| 37 | /** |
| 38 | * Provides methods for locating tool providers, for example, |
| 39 | * providers of compilers. This class complements the |
| 40 | * functionality of {@link java.util.ServiceLoader}. |
| 41 | * |
| 42 | * @author Peter von der Ahé |
| 43 | * @since 1.6 |
| 44 | */ |
| 45 | public class ToolProvider { |
| 46 | |
| 47 | private ToolProvider() {} |
| 48 | |
| 49 | private static final String propertyName = "sun.tools.ToolProvider"; |
| 50 | private static final String loggerName = "javax.tools"; |
| 51 | |
| 52 | /* |
| 53 | * Define the system property "sun.tools.ToolProvider" to enable |
| 54 | * debugging: |
| 55 | * |
| 56 | * java ... -Dsun.tools.ToolProvider ... |
| 57 | */ |
| 58 | static <T> T trace(Level level, Object reason) { |
| 59 | // NOTE: do not make this method private as it affects stack traces |
| 60 | try { |
| 61 | if (System.getProperty(propertyName) != null) { |
| 62 | StackTraceElement[] st = Thread.currentThread().getStackTrace(); |
| 63 | String method = "???"; |
| 64 | String cls = ToolProvider.class.getName(); |
| 65 | if (st.length > 2) { |
| 66 | StackTraceElement frame = st[2]; |
| 67 | method = String.format((Locale)null, "%s(%s:%s)", |
| 68 | frame.getMethodName(), |
| 69 | frame.getFileName(), |
| 70 | frame.getLineNumber()); |
| 71 | cls = frame.getClassName(); |
| 72 | } |
| 73 | Logger logger = Logger.getLogger(loggerName); |
| 74 | if (reason instanceof Throwable) { |
| 75 | logger.logp(level, cls, method, |
| 76 | reason.getClass().getName(), (Throwable)reason); |
| 77 | } else { |
| 78 | logger.logp(level, cls, method, String.valueOf(reason)); |
| 79 | } |
| 80 | } |
| 81 | } catch (SecurityException ex) { |
| 82 | System.err.format((Locale)null, "%s: %s; %s%n", |
| 83 | ToolProvider.class.getName(), |
| 84 | reason, |
| 85 | ex.getLocalizedMessage()); |
| 86 | } |
| 87 | return null; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Gets the Java™ programming language compiler provided |
| 92 | * with this platform. |
| 93 | * @return the compiler provided with this platform or |
| 94 | * {@code null} if no compiler is provided |
| 95 | */ |
| 96 | public static JavaCompiler getSystemJavaCompiler() { |
| 97 | if (Lazy.compilerClass == null) |
| 98 | return trace(WARNING, "Lazy.compilerClass == null"); |
| 99 | try { |
| 100 | return Lazy.compilerClass.newInstance(); |
| 101 | } catch (Throwable e) { |
| 102 | return trace(WARNING, e); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Returns the class loader for tools provided with this platform. |
| 108 | * This does not include user-installed tools. Use the |
| 109 | * {@linkplain java.util.ServiceLoader service provider mechanism} |
| 110 | * for locating user installed tools. |
| 111 | * |
| 112 | * @return the class loader for tools provided with this platform |
| 113 | * or {@code null} if no tools are provided |
| 114 | */ |
| 115 | public static ClassLoader getSystemToolClassLoader() { |
| 116 | if (Lazy.compilerClass == null) |
| 117 | return trace(WARNING, "Lazy.compilerClass == null"); |
| 118 | return Lazy.compilerClass.getClassLoader(); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * This class will not be initialized until one of the above |
| 123 | * methods are called. This ensures that searching for the |
| 124 | * compiler does not affect platform start up. |
| 125 | */ |
| 126 | static class Lazy { |
| 127 | private static final String defaultJavaCompilerName |
| 128 | = "com.sun.tools.javac.api.JavacTool"; |
| 129 | private static final String[] defaultToolsLocation |
| 130 | = { "lib", "tools.jar" }; |
| 131 | static final Class<? extends JavaCompiler> compilerClass; |
| 132 | static { |
| 133 | Class<? extends JavaCompiler> c = null; |
| 134 | try { |
| 135 | c = findClass().asSubclass(JavaCompiler.class); |
| 136 | } catch (Throwable t) { |
| 137 | trace(WARNING, t); |
| 138 | } |
| 139 | compilerClass = c; |
| 140 | } |
| 141 | |
| 142 | private static Class<?> findClass() |
| 143 | throws MalformedURLException, ClassNotFoundException |
| 144 | { |
| 145 | try { |
| 146 | return enableAsserts(Class.forName(defaultJavaCompilerName, false, null)); |
| 147 | } catch (ClassNotFoundException e) { |
| 148 | trace(FINE, e); |
| 149 | } |
| 150 | File file = new File(System.getProperty("java.home")); |
| 151 | if (file.getName().equalsIgnoreCase("jre")) |
| 152 | file = file.getParentFile(); |
| 153 | for (String name : defaultToolsLocation) |
| 154 | file = new File(file, name); |
| 155 | URL[] urls = {file.toURI().toURL()}; |
| 156 | trace(FINE, urls[0].toString()); |
| 157 | ClassLoader cl = URLClassLoader.newInstance(urls); |
| 158 | cl.setPackageAssertionStatus("com.sun.tools.javac", true); |
| 159 | return Class.forName(defaultJavaCompilerName, false, cl); |
| 160 | } |
| 161 | |
| 162 | private static Class<?> enableAsserts(Class<?> cls) { |
| 163 | try { |
| 164 | ClassLoader loader = cls.getClassLoader(); |
| 165 | if (loader != null) |
| 166 | loader.setPackageAssertionStatus("com.sun.tools.javac", true); |
| 167 | else |
| 168 | trace(FINE, "loader == null"); |
| 169 | } catch (SecurityException ex) { |
| 170 | trace(FINE, ex); |
| 171 | } |
| 172 | return cls; |
| 173 | } |
| 174 | } |
| 175 | } |