Wed, 08 Oct 2014 14:16:40 -0700
Merge
duke@1 | 1 | /* |
jjg@1413 | 2 | * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved. |
duke@1 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@1 | 4 | * |
duke@1 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@1 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ohair@554 | 7 | * published by the Free Software Foundation. Oracle designates this |
duke@1 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@554 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
duke@1 | 10 | * |
duke@1 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@1 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@1 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@1 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@1 | 15 | * accompanied this code). |
duke@1 | 16 | * |
duke@1 | 17 | * You should have received a copy of the GNU General Public License version |
duke@1 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@1 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@1 | 20 | * |
ohair@554 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@554 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohair@554 | 23 | * questions. |
duke@1 | 24 | */ |
duke@1 | 25 | |
duke@1 | 26 | package javax.tools; |
duke@1 | 27 | |
duke@1 | 28 | import java.io.File; |
jjg@659 | 29 | import java.lang.ref.Reference; |
jjg@659 | 30 | import java.lang.ref.WeakReference; |
duke@1 | 31 | import java.net.URL; |
duke@1 | 32 | import java.net.URLClassLoader; |
duke@1 | 33 | import java.net.MalformedURLException; |
jjg@659 | 34 | import java.util.HashMap; |
duke@1 | 35 | import java.util.Locale; |
jjg@659 | 36 | import java.util.Map; |
duke@1 | 37 | import java.util.logging.Logger; |
duke@1 | 38 | import java.util.logging.Level; |
duke@1 | 39 | import static java.util.logging.Level.*; |
duke@1 | 40 | |
duke@1 | 41 | /** |
duke@1 | 42 | * Provides methods for locating tool providers, for example, |
duke@1 | 43 | * providers of compilers. This class complements the |
duke@1 | 44 | * functionality of {@link java.util.ServiceLoader}. |
duke@1 | 45 | * |
duke@1 | 46 | * @author Peter von der Ahé |
duke@1 | 47 | * @since 1.6 |
duke@1 | 48 | */ |
duke@1 | 49 | public class ToolProvider { |
duke@1 | 50 | |
duke@1 | 51 | private static final String propertyName = "sun.tools.ToolProvider"; |
duke@1 | 52 | private static final String loggerName = "javax.tools"; |
duke@1 | 53 | |
duke@1 | 54 | /* |
duke@1 | 55 | * Define the system property "sun.tools.ToolProvider" to enable |
duke@1 | 56 | * debugging: |
duke@1 | 57 | * |
duke@1 | 58 | * java ... -Dsun.tools.ToolProvider ... |
duke@1 | 59 | */ |
duke@1 | 60 | static <T> T trace(Level level, Object reason) { |
duke@1 | 61 | // NOTE: do not make this method private as it affects stack traces |
duke@1 | 62 | try { |
duke@1 | 63 | if (System.getProperty(propertyName) != null) { |
duke@1 | 64 | StackTraceElement[] st = Thread.currentThread().getStackTrace(); |
duke@1 | 65 | String method = "???"; |
duke@1 | 66 | String cls = ToolProvider.class.getName(); |
duke@1 | 67 | if (st.length > 2) { |
duke@1 | 68 | StackTraceElement frame = st[2]; |
duke@1 | 69 | method = String.format((Locale)null, "%s(%s:%s)", |
duke@1 | 70 | frame.getMethodName(), |
duke@1 | 71 | frame.getFileName(), |
duke@1 | 72 | frame.getLineNumber()); |
duke@1 | 73 | cls = frame.getClassName(); |
duke@1 | 74 | } |
duke@1 | 75 | Logger logger = Logger.getLogger(loggerName); |
duke@1 | 76 | if (reason instanceof Throwable) { |
duke@1 | 77 | logger.logp(level, cls, method, |
duke@1 | 78 | reason.getClass().getName(), (Throwable)reason); |
duke@1 | 79 | } else { |
duke@1 | 80 | logger.logp(level, cls, method, String.valueOf(reason)); |
duke@1 | 81 | } |
duke@1 | 82 | } |
duke@1 | 83 | } catch (SecurityException ex) { |
duke@1 | 84 | System.err.format((Locale)null, "%s: %s; %s%n", |
duke@1 | 85 | ToolProvider.class.getName(), |
duke@1 | 86 | reason, |
duke@1 | 87 | ex.getLocalizedMessage()); |
duke@1 | 88 | } |
duke@1 | 89 | return null; |
duke@1 | 90 | } |
duke@1 | 91 | |
jjg@659 | 92 | private static final String defaultJavaCompilerName |
jjg@659 | 93 | = "com.sun.tools.javac.api.JavacTool"; |
jjg@659 | 94 | |
duke@1 | 95 | /** |
duke@1 | 96 | * Gets the Java™ programming language compiler provided |
duke@1 | 97 | * with this platform. |
duke@1 | 98 | * @return the compiler provided with this platform or |
duke@1 | 99 | * {@code null} if no compiler is provided |
duke@1 | 100 | */ |
duke@1 | 101 | public static JavaCompiler getSystemJavaCompiler() { |
jjg@659 | 102 | return instance().getSystemTool(JavaCompiler.class, defaultJavaCompilerName); |
duke@1 | 103 | } |
duke@1 | 104 | |
jjg@1413 | 105 | private static final String defaultDocumentationToolName |
jjg@1413 | 106 | = "com.sun.tools.javadoc.api.JavadocTool"; |
jjg@1413 | 107 | |
jjg@1413 | 108 | /** |
jjg@1413 | 109 | * Gets the Java™ programming language documentation tool provided |
jjg@1413 | 110 | * with this platform. |
jjg@1413 | 111 | * @return the documentation tool provided with this platform or |
jjg@1413 | 112 | * {@code null} if no documentation tool is provided |
jjg@1413 | 113 | */ |
jjg@1413 | 114 | public static DocumentationTool getSystemDocumentationTool() { |
jjg@1413 | 115 | return instance().getSystemTool(DocumentationTool.class, defaultDocumentationToolName); |
jjg@1413 | 116 | } |
jjg@1413 | 117 | |
duke@1 | 118 | /** |
duke@1 | 119 | * Returns the class loader for tools provided with this platform. |
duke@1 | 120 | * This does not include user-installed tools. Use the |
duke@1 | 121 | * {@linkplain java.util.ServiceLoader service provider mechanism} |
duke@1 | 122 | * for locating user installed tools. |
duke@1 | 123 | * |
duke@1 | 124 | * @return the class loader for tools provided with this platform |
duke@1 | 125 | * or {@code null} if no tools are provided |
duke@1 | 126 | */ |
duke@1 | 127 | public static ClassLoader getSystemToolClassLoader() { |
jjg@659 | 128 | try { |
jjg@659 | 129 | Class<? extends JavaCompiler> c = |
jjg@659 | 130 | instance().getSystemToolClass(JavaCompiler.class, defaultJavaCompilerName); |
jjg@659 | 131 | return c.getClassLoader(); |
jjg@659 | 132 | } catch (Throwable e) { |
jjg@659 | 133 | return trace(WARNING, e); |
jjg@659 | 134 | } |
duke@1 | 135 | } |
duke@1 | 136 | |
jjg@659 | 137 | |
jjg@659 | 138 | private static ToolProvider instance; |
jjg@659 | 139 | |
jjg@659 | 140 | private static synchronized ToolProvider instance() { |
jjg@659 | 141 | if (instance == null) |
jjg@659 | 142 | instance = new ToolProvider(); |
jjg@659 | 143 | return instance; |
jjg@659 | 144 | } |
jjg@659 | 145 | |
jjg@659 | 146 | // Cache for tool classes. |
jjg@659 | 147 | // Use weak references to avoid keeping classes around unnecessarily |
jjg@659 | 148 | private Map<String, Reference<Class<?>>> toolClasses = new HashMap<String, Reference<Class<?>>>(); |
jjg@659 | 149 | |
jjg@659 | 150 | // Cache for tool classloader. |
jjg@659 | 151 | // Use a weak reference to avoid keeping it around unnecessarily |
jjg@659 | 152 | private Reference<ClassLoader> refToolClassLoader = null; |
jjg@659 | 153 | |
jjg@659 | 154 | |
jjg@659 | 155 | private ToolProvider() { } |
jjg@659 | 156 | |
jjg@659 | 157 | private <T> T getSystemTool(Class<T> clazz, String name) { |
jjg@659 | 158 | Class<? extends T> c = getSystemToolClass(clazz, name); |
jjg@659 | 159 | try { |
jjg@659 | 160 | return c.asSubclass(clazz).newInstance(); |
jjg@659 | 161 | } catch (Throwable e) { |
jjg@659 | 162 | trace(WARNING, e); |
jjg@659 | 163 | return null; |
jjg@659 | 164 | } |
jjg@659 | 165 | } |
jjg@659 | 166 | |
jjg@659 | 167 | private <T> Class<? extends T> getSystemToolClass(Class<T> clazz, String name) { |
jjg@659 | 168 | Reference<Class<?>> refClass = toolClasses.get(name); |
jjg@659 | 169 | Class<?> c = (refClass == null ? null : refClass.get()); |
jjg@659 | 170 | if (c == null) { |
duke@1 | 171 | try { |
jjg@659 | 172 | c = findSystemToolClass(name); |
jjg@659 | 173 | } catch (Throwable e) { |
jjg@659 | 174 | return trace(WARNING, e); |
duke@1 | 175 | } |
jjg@659 | 176 | toolClasses.put(name, new WeakReference<Class<?>>(c)); |
jjg@659 | 177 | } |
jjg@659 | 178 | return c.asSubclass(clazz); |
jjg@659 | 179 | } |
jjg@659 | 180 | |
jjg@659 | 181 | private static final String[] defaultToolsLocation = { "lib", "tools.jar" }; |
jjg@659 | 182 | |
jjg@659 | 183 | private Class<?> findSystemToolClass(String toolClassName) |
jjg@659 | 184 | throws MalformedURLException, ClassNotFoundException |
jjg@659 | 185 | { |
jjg@659 | 186 | // try loading class directly, in case tool is on the bootclasspath |
jjg@659 | 187 | try { |
jjg@816 | 188 | return Class.forName(toolClassName, false, null); |
jjg@659 | 189 | } catch (ClassNotFoundException e) { |
jjg@659 | 190 | trace(FINE, e); |
jjg@659 | 191 | |
jjg@659 | 192 | // if tool not on bootclasspath, look in default tools location (tools.jar) |
jjg@659 | 193 | ClassLoader cl = (refToolClassLoader == null ? null : refToolClassLoader.get()); |
jjg@659 | 194 | if (cl == null) { |
jjg@659 | 195 | File file = new File(System.getProperty("java.home")); |
jjg@659 | 196 | if (file.getName().equalsIgnoreCase("jre")) |
jjg@659 | 197 | file = file.getParentFile(); |
jjg@659 | 198 | for (String name : defaultToolsLocation) |
jjg@659 | 199 | file = new File(file, name); |
jjg@659 | 200 | |
jjg@659 | 201 | // if tools not found, no point in trying a URLClassLoader |
jjg@659 | 202 | // so rethrow the original exception. |
jjg@659 | 203 | if (!file.exists()) |
jjg@659 | 204 | throw e; |
jjg@659 | 205 | |
jjg@659 | 206 | URL[] urls = { file.toURI().toURL() }; |
jjg@659 | 207 | trace(FINE, urls[0].toString()); |
jjg@659 | 208 | |
jjg@659 | 209 | cl = URLClassLoader.newInstance(urls); |
jjg@659 | 210 | refToolClassLoader = new WeakReference<ClassLoader>(cl); |
jjg@659 | 211 | } |
jjg@659 | 212 | |
jjg@659 | 213 | return Class.forName(toolClassName, false, cl); |
duke@1 | 214 | } |
jjg@659 | 215 | } |
duke@1 | 216 | } |