Debian Codegraph @

net/sf/saxon/trans/DynamicLoader.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.trans;
9
10 import net.sf.saxon.Configuration;
11 import net.sf.saxon.lib.Logger;
12 import net.sf.saxon.serialize.MessageEmitter;
13
14 import java.io.InputStream;
15 import java.util.HashMap;
16
17 /**
18 * Utility class used to perform dynamic loading of user-hook implementations
19 */
20 public class DynamicLoader {
21
22 private ClassLoader classLoader;
23
24 protected HashMap<String, Class> knownClasses = new HashMap<String, Class>(20);
25
26 public DynamicLoader() {
27 registerKnownClasses();
28 }
29
30 /**
31 * Register classes that might be dynamically loaded even though they are contained
32 * within Saxon itself. This typically occurs for default implementations of classes that
33 * can be substituted or subclassed by the user.
34 */
35
36 protected void registerKnownClasses() {
37 knownClasses.put("net.sf.saxon.serialize.MessageEmitter", MessageEmitter.class);
38 //knownClasses.put("net.sf.saxon.java.JavaPlatform", JavaPlatform.class); // not available on .NET
39 knownClasses.put("net.sf.saxon.Configuration", Configuration.class);
40 }
41
42 /**
43 * Set a ClassLoader to be used when loading external classes. Examples of classes that are
44 * loaded include SAX parsers, localization modules for formatting numbers and dates,
45 * extension functions, external object models. In an environment such as Eclipse that uses
46 * its own ClassLoader, this ClassLoader should be nominated to ensure that any class loaded
47 * by Saxon is identical to a class of the same name loaded by the external environment.
48 *
49 * @param loader the ClassLoader to be used in this configuration
50 */
51
52 public void setClassLoader(ClassLoader loader) {
53 classLoader = loader;
54 }
55
56 /**
57 * Get the ClassLoader supplied using the method {@link #setClassLoader}.
58 * If none has been supplied, return null.
59 *
60 * @return the ClassLoader used in this configuration
61 */
62
63 public ClassLoader getClassLoader() {
64 return classLoader;
65 }
66
67 /**
68 * Load a class using the class name provided.
69 * Note that the method does not check that the object is of the right class.
70 * <p>This method is intended for internal use only.</p>
71 *
72 * @param className A string containing the name of the
73 * class, for example "com.microstar.sax.LarkDriver"
74 * @param traceOut if diagnostic tracing is required, the destination for the output; otherwise null
75 * @param classLoader The ClassLoader to be used to load the class. If this is null, then
76 * the classLoader used will be the first one available of: the classLoader registered
77 * with the Configuration using {@link #setClassLoader}; the context class loader for
78 * the current thread; or failing that, the class loader invoked implicitly by a call
79 * of Class.forName() (which is the ClassLoader that was used to load the Configuration
80 * object itself).
81 * @return an instance of the class named, or null if it is not
82 * loadable.
83 * @throws XPathException if the class cannot be loaded.
84 */
85
86 public Class getClass(String className, Logger traceOut, ClassLoader classLoader) throws XPathException {
87 Class known = knownClasses.get(className);
88 if (known != null) {
89 return known;
90 }
91
92 boolean tracing = traceOut != null;
93 if (tracing) {
94 traceOut.info("Loading " + className);
95 }
96
97 try {
98 ClassLoader loader = classLoader;
99 if (loader == null) {
100 loader = this.classLoader;
101 }
102 if (loader == null) {
103 loader = Thread.currentThread().getContextClassLoader();
104 }
105 if (loader != null) {
106 try {
107 return loader.loadClass(className);
108 } catch (Throwable ex) {
109 // Catching Exception is not enough; Java sometimes throws a NoClassDefFoundError
110 return Class.forName(className);
111 }
112 } else {
113 return Class.forName(className);
114 }
115 } catch (Throwable e) {
116 if (tracing) {
117 // The exception is often masked, especially when calling extension
118 // functions
119 traceOut.error("The class " + className + " could not be loaded: " + e.getMessage());
120 }
121 throw new XPathException("Failed to load " + className + getMissingJarFileMessage(className), e);
122 }
123
124 }
125
126 /**
127 * Instantiate a class using the class name provided.
128 * Note that the method does not check that the object is of the right class.
129 * <p>This method is intended for internal use only.</p>
130 *
131 * @param className A string containing the name of the
132 * class, for example "com.microstar.sax.LarkDriver"
133 * @param classLoader The ClassLoader to be used to load the class. If this is null, then
134 * the classLoader used will be the first one available of: the classLoader registered
135 * with the Configuration using {@link #setClassLoader}; the context class loader for
136 * the current thread; or failing that, the class loader invoked implicitly by a call
137 * of Class.forName() (which is the ClassLoader that was used to load the Configuration
138 * object itself).
139 * @return an instance of the class named, or null if it is not
140 * loadable.
141 * @throws XPathException if the class cannot be loaded.
142 */
143
144 public Object getInstance(String className, /*@Nullable*/ ClassLoader classLoader) throws XPathException {
145 Class theclass = getClass(className, null, classLoader);
146 try {
147 return theclass.newInstance();
148 } catch (Exception err) {
149 throw new XPathException("Failed to instantiate class " + className +
150 " (does it have a public zero-argument constructor?)", err);
151 }
152 }
153
154 /**
155 * Instantiate a class using the class name provided, with the option of tracing
156 * Note that the method does not check that the object is of the right class.
157 * <p>This method is intended for internal use only.</p>
158 *
159 * @param className A string containing the name of the
160 * class, for example "com.microstar.sax.LarkDriver"
161 * @param traceOut if attempts to load classes are to be traced, then the destination
162 * for the trace output; otherwise null
163 * @param classLoader The ClassLoader to be used to load the class. If this is null, then
164 * the classLoader used will be the first one available of: the classLoader registered
165 * with the Configuration using {@link #setClassLoader}; the context class loader for
166 * the current thread; or failing that, the class loader invoked implicitly by a call
167 * of Class.forName() (which is the ClassLoader that was used to load the Configuration
168 * object itself).
169 * @return an instance of the class named, or null if it is not
170 * loadable.
171 * @throws XPathException if the class cannot be loaded.
172 */
173
174 public Object getInstance(String className, Logger traceOut, /*@Nullable*/ ClassLoader classLoader) throws XPathException {
175 Class theclass = getClass(className, traceOut, classLoader);
176 try {
177 return theclass.newInstance();
178 } catch (NoClassDefFoundError err) {
179 throw new XPathException("Failed to load instance of class " + className + getMissingJarFileMessage(className), err);
180 } catch (Exception err) {
181 throw new XPathException("Failed to instantiate class " + className, err);
182 }
183 }
184
185 /**
186 * If a Saxon class is in a particular JAR file, identify the JAR file
187 * @param className the name of a class
188 * @return the name of the JAR file that the class should be found in, if known; otherwise null
189 */
190
191 private String getJarFileForClass(String className) {
192 if (className.startsWith("net.sf.saxon.option.sql.")) {
193 return "saxon9-sql.jar";
194 } else if (className.startsWith("com.saxonica.StatsTransform")) {
195 return "saxon9-stats.jar";
196 } else if (className.startsWith("com.ibm.icu.")) {
197 return "saxon9-icu.jar";
198 } else if (className.startsWith("com.saxonica")) {
199 return "saxon9ee.jar";
200 } else {
201 return null;
202 }
203 }
204
205 private String getMissingJarFileMessage(String className) {
206 String jar = getJarFileForClass(className);
207 return jar==null ? "" : ". Check that " + jar + " is on the classpath";
208 }
209
210 public InputStream getResourceAsStream(String name) {
211 ClassLoader loader = getClassLoader();
212 if (loader == null) {
213 loader = Thread.currentThread().getContextClassLoader();
214 }
215 return loader.getResourceAsStream(name);
216 }
217
218
219 }
220