Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ToolProvider |
|
| 0.0;0 | ||||
ToolProvider$CompilerClassLoader |
|
| 0.0;0 |
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 com.sun.javafx.api; | |
27 | ||
28 | import java.io.File; | |
29 | import java.net.MalformedURLException; | |
30 | import java.net.URL; | |
31 | import java.net.URLClassLoader; | |
32 | import java.util.logging.Logger; | |
33 | ||
34 | /** | |
35 | * JavaFX version of javac's ToolProvider class. | |
36 | * | |
37 | * @author Tom Ball | |
38 | */ | |
39 | 0 | public class ToolProvider { |
40 | 0 | private static Logger logger = Logger.getLogger("com.sun.javafx"); |
41 | ||
42 | 0 | private ToolProvider() {} |
43 | ||
44 | /** | |
45 | * Gets a JavaFX Script compiler instance. | |
46 | * @return the compiler instance or {@code null} if no compiler | |
47 | * is included as part of the application classpath | |
48 | */ | |
49 | public static JavafxCompiler getJavafxCompiler() { | |
50 | try { | |
51 | 0 | URL[] urls = new URL[] { |
52 | getPath("com.sun.tools.javafx.api.JavafxcTool"), | |
53 | getPath("com.sun.tools.javac.util.Context") | |
54 | }; | |
55 | 0 | ClassLoader parent = JavafxCompiler.class.getClassLoader(); |
56 | 0 | ClassLoader cl = new CompilerClassLoader(urls, parent); |
57 | 0 | Class<?> cls = Class.forName("com.sun.tools.javafx.api.JavafxcTool", false, cl); |
58 | 0 | return (JavafxCompiler)cls.newInstance(); |
59 | 0 | } catch (Throwable t) { |
60 | 0 | throw new RuntimeException(t); |
61 | } | |
62 | } | |
63 | ||
64 | /** | |
65 | * Gets a JavaFX Script script engine instance. This is an alternative | |
66 | * to the general use where the script engine is looked up as a service. | |
67 | * @return the script engine instance or {@code null} if no script engine | |
68 | * is included as part of the application classpath | |
69 | */ | |
70 | public static JavaFXScriptEngine getJavaFXScriptEngine() { | |
71 | try { | |
72 | 0 | URL[] urls = new URL[] { |
73 | getPath("com.sun.tools.javafx.script.JavaFXScriptEngineImpl"), | |
74 | getPath("com.sun.tools.javac.util.Context") | |
75 | }; | |
76 | 0 | ClassLoader parent = JavafxCompiler.class.getClassLoader(); |
77 | 0 | ClassLoader cl = new CompilerClassLoader(urls, parent); |
78 | 0 | Class<?> cls = Class.forName("com.sun.tools.javafx.script.JavaFXScriptEngineImpl", false, cl); |
79 | 0 | return (JavaFXScriptEngine)cls.newInstance(); |
80 | 0 | } catch (Throwable t) { |
81 | 0 | throw new RuntimeException(t); |
82 | } | |
83 | } | |
84 | ||
85 | /** | |
86 | * ClassLoader which loads internal javafxc and javac classes from the | |
87 | * specified path instead of the classpath default. | |
88 | */ | |
89 | private static class CompilerClassLoader extends URLClassLoader { | |
90 | public CompilerClassLoader(URL[] urls, ClassLoader parent) { | |
91 | 0 | super(urls, parent); |
92 | 0 | } |
93 | ||
94 | @Override | |
95 | public URL findResource(String name) { | |
96 | 0 | URL url = super.findResource(name); |
97 | 0 | return url; |
98 | } | |
99 | ||
100 | @Override | |
101 | protected synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { | |
102 | 0 | if (name.indexOf("sun.tools") >= 0) { |
103 | 0 | Class c = findLoadedClass(name); |
104 | 0 | if (c != null) { |
105 | 0 | logger.info("found loaded class: " + name); |
106 | 0 | return c; |
107 | } | |
108 | 0 | c = findClass(name); |
109 | 0 | if (c == null) { |
110 | 0 | logger.info("didn't find class: " + name); |
111 | 0 | return super.loadClass(name, resolve); |
112 | } | |
113 | 0 | if (resolve) |
114 | 0 | resolveClass(c); |
115 | 0 | return c; |
116 | } | |
117 | 0 | return super.loadClass(name, resolve); |
118 | } | |
119 | } | |
120 | ||
121 | /** | |
122 | * Return the classpath element that contains the specified class. | |
123 | * @return the classpath element, usually the class's jar file | |
124 | * @throws java.net.MalformedURLException | |
125 | */ | |
126 | private static URL getPath(String className) throws MalformedURLException { | |
127 | 0 | String classFile = className.replace('.', '/') + ".class"; |
128 | 0 | ClassLoader cl = ToolProvider.class.getClassLoader(); |
129 | 0 | if (cl == null) |
130 | 0 | cl = ClassLoader.getSystemClassLoader(); |
131 | 0 | URL classURL = cl.getResource(classFile); |
132 | 0 | String path = classURL.getPath(); |
133 | 0 | assert path.endsWith(classFile); |
134 | 0 | path = path.substring(0, path.indexOf(classFile) - 1); |
135 | 0 | if (path.endsWith("!")) { |
136 | 0 | path = path.substring(0, path.length() - 1); |
137 | } | |
138 | 0 | File f = new File(path); |
139 | 0 | if (f.exists()) { |
140 | 0 | return f.toURI().toURL(); |
141 | } | |
142 | 0 | return new URL(path); |
143 | } | |
144 | } |