|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.tika.config; |
| 18 | + |
| 19 | +import java.io.File; |
| 20 | +import java.net.URL; |
| 21 | +import java.net.URLClassLoader; |
| 22 | +import java.nio.file.DirectoryStream; |
| 23 | +import java.nio.file.Files; |
| 24 | +import java.nio.file.Path; |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.Collections; |
| 27 | +import java.util.Comparator; |
| 28 | +import java.util.List; |
| 29 | + |
| 30 | +import org.slf4j.Logger; |
| 31 | +import org.slf4j.LoggerFactory; |
| 32 | + |
| 33 | +/** |
| 34 | + * Opt-in mechanism for adding user-supplied "extras" jars (extra |
| 35 | + * {@code EncodingDetector}s, {@code Parser}s, etc.) to Tika's SPI discovery |
| 36 | + * without repackaging the application. |
| 37 | + * |
| 38 | + * <p><b>Off by default.</b> Nothing is loaded unless the |
| 39 | + * {@value #EXTRAS_DIR_PROPERTY} system property points at a directory; then every |
| 40 | + * {@code *.jar} in it is made visible to service-loading. There is no implicit or |
| 41 | + * default directory — the feature is off unless the property is set. (A relative |
| 42 | + * property value is resolved against the process working directory, like any path.) |
| 43 | + * |
| 44 | + * <p><b>Security:</b> this is a trusted code directory — anything in it runs with |
| 45 | + * the full privileges of the Tika process. Treat write access to it exactly like |
| 46 | + * write access to {@code lib/}; it must not be writable by less-trusted principals |
| 47 | + * (for servers, not reachable by request handling). Being opt-in keeps "we are |
| 48 | + * now loading extra code" an explicit, auditable choice. |
| 49 | + */ |
| 50 | +public final class TikaExtras { |
| 51 | + |
| 52 | + /** System property naming the extras directory. Unset = feature off. */ |
| 53 | + public static final String EXTRAS_DIR_PROPERTY = "tika.extras.dir"; |
| 54 | + |
| 55 | + private static final Logger LOG = LoggerFactory.getLogger(TikaExtras.class); |
| 56 | + |
| 57 | + private TikaExtras() { |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * If {@value #EXTRAS_DIR_PROPERTY} is set, installs a classloader over the |
| 62 | + * {@code *.jar} files in that directory as the thread + Tika |
| 63 | + * {@link ServiceLoader} context classloader, so they join SPI discovery. |
| 64 | + * No-op (returns {@code null}) when the property is unset or the directory is |
| 65 | + * missing/empty. Call exactly once at startup, before any Tika component is |
| 66 | + * loaded: each call builds a new classloader, so repeated calls stack them and |
| 67 | + * leave the earlier ones' open jar handles dangling. |
| 68 | + * |
| 69 | + * @return the installed classloader, or {@code null} if extras are off/empty |
| 70 | + */ |
| 71 | + public static ClassLoader install() { |
| 72 | + List<Path> jars = extraJars(); |
| 73 | + if (jars.isEmpty()) { |
| 74 | + return null; |
| 75 | + } |
| 76 | + List<URL> urls = new ArrayList<>(jars.size()); |
| 77 | + List<Path> loaded = new ArrayList<>(jars.size()); |
| 78 | + for (Path jar : jars) { |
| 79 | + try { |
| 80 | + urls.add(jar.toUri().toURL()); |
| 81 | + loaded.add(jar); |
| 82 | + } catch (Exception e) { |
| 83 | + LOG.warn("Skipping extra jar {}: {}", jar, e.toString()); |
| 84 | + } |
| 85 | + } |
| 86 | + if (urls.isEmpty()) { |
| 87 | + return null; |
| 88 | + } |
| 89 | + ClassLoader parent = Thread.currentThread().getContextClassLoader(); |
| 90 | + if (parent == null) { |
| 91 | + parent = TikaExtras.class.getClassLoader(); |
| 92 | + } |
| 93 | + URLClassLoader cl = new URLClassLoader(urls.toArray(new URL[0]), parent); |
| 94 | + Thread.currentThread().setContextClassLoader(cl); |
| 95 | + ServiceLoader.setContextClassLoader(cl); |
| 96 | + LOG.info("{}: loaded {} extra jar(s): {}", EXTRAS_DIR_PROPERTY, loaded.size(), loaded); |
| 97 | + return cl; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * The {@code *.jar} files in the {@value #EXTRAS_DIR_PROPERTY} directory — for |
| 102 | + * callers that extend a forked process's classpath rather than installing a |
| 103 | + * classloader. Empty when the property is unset or the directory is |
| 104 | + * missing/has no jars. |
| 105 | + */ |
| 106 | + public static List<Path> extraJars() { |
| 107 | + Path dir = extrasDir(); |
| 108 | + if (dir == null || !Files.isDirectory(dir)) { |
| 109 | + return Collections.emptyList(); |
| 110 | + } |
| 111 | + List<Path> jars = new ArrayList<>(); |
| 112 | + try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, "*.jar")) { |
| 113 | + for (Path jar : stream) { |
| 114 | + jars.add(jar); |
| 115 | + } |
| 116 | + } catch (Exception e) { |
| 117 | + LOG.warn("Could not scan {}={}: {}", EXTRAS_DIR_PROPERTY, dir, e.toString()); |
| 118 | + } |
| 119 | + // Sort by file name so jar load order (classloader URL order / forked-child |
| 120 | + // classpath order, hence SPI precedence) is deterministic across platforms |
| 121 | + // and filesystems rather than depending on directory iteration order. |
| 122 | + jars.sort(Comparator.comparing(jar -> jar.getFileName().toString())); |
| 123 | + return jars; |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Appends the {@link #extraJars()} (as absolute paths, joined with the |
| 128 | + * platform path separator) to the given classpath string — for extending a |
| 129 | + * forked process's {@code -cp} with the extras jars. Returns {@code classpath} |
| 130 | + * unchanged when the feature is off or the directory has no jars. |
| 131 | + * |
| 132 | + * @param classpath the base classpath to extend |
| 133 | + * @return the classpath with any extras jars appended |
| 134 | + */ |
| 135 | + public static String appendJarsToClasspath(String classpath) { |
| 136 | + List<Path> jars = extraJars(); |
| 137 | + if (jars.isEmpty()) { |
| 138 | + return classpath; |
| 139 | + } |
| 140 | + String separator = File.pathSeparator; |
| 141 | + StringBuilder sb = new StringBuilder(); |
| 142 | + if (classpath != null && !classpath.isEmpty()) { |
| 143 | + sb.append(classpath); |
| 144 | + } |
| 145 | + for (Path jar : jars) { |
| 146 | + if (sb.length() > 0) { |
| 147 | + sb.append(separator); |
| 148 | + } |
| 149 | + sb.append(jar.toAbsolutePath()); |
| 150 | + } |
| 151 | + return sb.toString(); |
| 152 | + } |
| 153 | + |
| 154 | + /** The configured extras directory, or {@code null} if the feature is off. */ |
| 155 | + public static Path extrasDir() { |
| 156 | + String prop = System.getProperty(EXTRAS_DIR_PROPERTY); |
| 157 | + if (prop == null || prop.isBlank()) { |
| 158 | + return null; |
| 159 | + } |
| 160 | + try { |
| 161 | + return Path.of(prop.trim()); |
| 162 | + } catch (java.nio.file.InvalidPathException e) { |
| 163 | + LOG.warn("Ignoring invalid {}: {}", EXTRAS_DIR_PROPERTY, e.getMessage()); |
| 164 | + return null; |
| 165 | + } |
| 166 | + } |
| 167 | +} |
0 commit comments