Skip to content

Commit 4fc2392

Browse files
committed
TIKA-4864: resolve the default plugins dir against the install layout and make it absolute
1 parent 72f4df7 commit 4fc2392

2 files changed

Lines changed: 58 additions & 13 deletions

File tree

tika-server/tika-server-core/src/main/java/org/apache/tika/server/core/TikaServerProcess.java

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -727,28 +727,40 @@ private static PipesParsingHelper initPipesParsingHelper(TikaServerConfig tikaSe
727727

728728
/**
729729
* Resolves the default plugins directory. Looks for a "plugins" directory
730-
* next to the running jar first, then falls back to the current working directory.
730+
* next to the jar this class was loaded from, then next to that directory's
731+
* parent (the class may live in a "lib" directory beside the server jar),
732+
* then in the current working directory. Always returns an absolute path,
733+
* so the forked pipes server does not depend on its own working directory.
731734
*/
732735
private static String resolveDefaultPluginsDir() {
736+
Path codeSourceDir = null;
733737
try {
734738
Path jarPath = Path.of(
735739
TikaServerProcess.class.getProtectionDomain()
736740
.getCodeSource().getLocation().toURI());
737-
Path jarDir = jarPath.getParent();
738-
if (jarDir != null) {
739-
Path pluginsNextToJar = jarDir.resolve(DEFAULT_PLUGINS_DIR);
740-
if (Files.isDirectory(pluginsNextToJar)) {
741-
return pluginsNextToJar.toAbsolutePath().toString();
742-
}
743-
}
741+
codeSourceDir = jarPath.getParent();
744742
} catch (Exception e) {
745-
// Fall through to cwd-relative
743+
// no code source, probe the working directory only
746744
}
747-
Path cwdPlugins = Path.of(DEFAULT_PLUGINS_DIR);
748-
if (Files.isDirectory(cwdPlugins)) {
749-
return cwdPlugins.toAbsolutePath().toString();
745+
return resolveDefaultPluginsDir(codeSourceDir, Path.of("")).toString();
746+
}
747+
748+
// package-private for testing
749+
static Path resolveDefaultPluginsDir(Path codeSourceDir, Path cwd) {
750+
if (codeSourceDir != null) {
751+
Path nextToJar = codeSourceDir.resolve(DEFAULT_PLUGINS_DIR);
752+
if (Files.isDirectory(nextToJar)) {
753+
return nextToJar.toAbsolutePath();
754+
}
755+
Path parent = codeSourceDir.getParent();
756+
if (parent != null) {
757+
Path nextToParent = parent.resolve(DEFAULT_PLUGINS_DIR);
758+
if (Files.isDirectory(nextToParent)) {
759+
return nextToParent.toAbsolutePath();
760+
}
761+
}
750762
}
751-
return DEFAULT_PLUGINS_DIR;
763+
return cwd.resolve(DEFAULT_PLUGINS_DIR).toAbsolutePath();
752764
}
753765

754766
/**

tika-server/tika-server-core/src/test/java/org/apache/tika/server/core/TikaServerProcessTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,14 @@
2323
import static org.junit.jupiter.api.Assertions.assertThrows;
2424
import static org.junit.jupiter.api.Assertions.assertTrue;
2525

26+
import java.nio.file.Files;
27+
import java.nio.file.Path;
2628
import java.util.ArrayList;
2729
import java.util.List;
2830
import java.util.Set;
2931

3032
import org.junit.jupiter.api.Test;
33+
import org.junit.jupiter.api.io.TempDir;
3134

3235
import org.apache.tika.exception.TikaConfigException;
3336
import org.apache.tika.server.core.resource.MetadataResource;
@@ -41,6 +44,36 @@ private static TikaServerConfig config(boolean allowPipes, String... endpoints)
4144
return c;
4245
}
4346

47+
@Test
48+
public void pluginsDirNextToTheCodeSourceJar(@TempDir Path install) throws Exception {
49+
Path plugins = Files.createDirectories(install.resolve("plugins"));
50+
assertEquals(plugins.toAbsolutePath(),
51+
TikaServerProcess.resolveDefaultPluginsDir(install, Path.of("")));
52+
}
53+
54+
@Test
55+
public void pluginsDirBesideTheLibDirectory(@TempDir Path install) throws Exception {
56+
// the resolving class lives in lib/, the plugins next to the server jar
57+
Path lib = Files.createDirectories(install.resolve("lib"));
58+
Path plugins = Files.createDirectories(install.resolve("plugins"));
59+
assertEquals(plugins.toAbsolutePath(),
60+
TikaServerProcess.resolveDefaultPluginsDir(lib, Path.of("")));
61+
}
62+
63+
@Test
64+
public void pluginsDirFromTheWorkingDirectory(@TempDir Path install, @TempDir Path cwd)
65+
throws Exception {
66+
Path plugins = Files.createDirectories(cwd.resolve("plugins"));
67+
assertEquals(plugins.toAbsolutePath(),
68+
TikaServerProcess.resolveDefaultPluginsDir(install.resolve("lib"), cwd));
69+
}
70+
71+
@Test
72+
public void missingPluginsDirStaysAbsolute(@TempDir Path cwd) {
73+
// the forked pipes server must not re-resolve the path against its own cwd
74+
assertTrue(TikaServerProcess.resolveDefaultPluginsDir(null, cwd).isAbsolute());
75+
}
76+
4477
@Test
4578
public void pipesAndAsyncRequireAllowPipes() {
4679
// The pipes/async endpoints fork processes and read/write via fetchers/emitters; the

0 commit comments

Comments
 (0)