From dc9369fc4cdc682578a2b199241fc9d80e3493b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Louise=20S=C3=B6derstr=C3=B6m?= Date: Mon, 26 Feb 2024 08:49:12 +0100 Subject: [PATCH] [4Bece3wd] Use Neo4j home to determine default config location. (#586) This will be used in Windows service, where sun.java.command does not work. --- common/src/main/java/apoc/ApocConfig.java | 20 +++++++++++-------- .../apoc/ApocConfigCommandExpansionTest.java | 4 ++++ common/src/test/java/apoc/ApocConfigTest.java | 4 +++- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/common/src/main/java/apoc/ApocConfig.java b/common/src/main/java/apoc/ApocConfig.java index 5257090c7..be198d7b1 100644 --- a/common/src/main/java/apoc/ApocConfig.java +++ b/common/src/main/java/apoc/ApocConfig.java @@ -96,7 +96,7 @@ public class ApocConfig extends LifecycleAdapter { run_directory, lib_directory, neo4j_home)); - private static final String DEFAULT_PATH = "."; + private static final String CONFIG_DIR = "config-dir="; public static final String EXPORT_NOT_ENABLED_ERROR = "Export to files not enabled, please set apoc.export.file.enabled=true in your apoc.conf."; @@ -169,7 +169,7 @@ private String evaluateIfCommand(String settingName, String entry) { @Override public void init() { log.debug("called init"); - // grab NEO4J_CONF from environment. If not set, calculate it from sun.java.command system property + // grab NEO4J_CONF from environment. If not set, calculate it from sun.java.command system property or Neo4j default String neo4jConfFolder = System.getenv().getOrDefault("NEO4J_CONF", determineNeo4jConfFolder()); System.setProperty("NEO4J_CONF", neo4jConfFolder); log.info("system property NEO4J_CONF set to %s", neo4jConfFolder); @@ -183,21 +183,25 @@ public void init() { } protected String determineNeo4jConfFolder() { + String defaultPath = neo4jConfig + .get(neo4j_home) + .resolve(Config.DEFAULT_CONFIG_DIR_NAME) + .toString(); String command = System.getProperty(SUN_JAVA_COMMAND); if (command == null) { log.warn( - "system property %s is not set, assuming '.' as conf dir. This might cause `apoc.conf` not getting loaded.", - SUN_JAVA_COMMAND); - return DEFAULT_PATH; + "system property %s is not set, assuming %s as conf dir. This might cause `apoc.conf` not getting loaded.", + SUN_JAVA_COMMAND, defaultPath); + return defaultPath; } else { final String neo4jConfFolder = Stream.of(command.split("--")) .map(String::trim) .filter(s -> s.startsWith(CONFIG_DIR)) .map(s -> s.substring(CONFIG_DIR.length())) .findFirst() - .orElse(DEFAULT_PATH); - if (DEFAULT_PATH.equals(neo4jConfFolder)) { - log.info("cannot determine conf folder from sys property %s, assuming '.' ", command); + .orElse(defaultPath); + if (defaultPath.equals(neo4jConfFolder)) { + log.info("cannot determine conf folder from sys property %s, assuming %s", command, defaultPath); } else { log.info("from system properties: NEO4J_CONF=%s", neo4jConfFolder); } diff --git a/common/src/test/java/apoc/ApocConfigCommandExpansionTest.java b/common/src/test/java/apoc/ApocConfigCommandExpansionTest.java index fbfeeb1cf..7d56d80f4 100644 --- a/common/src/test/java/apoc/ApocConfigCommandExpansionTest.java +++ b/common/src/test/java/apoc/ApocConfigCommandExpansionTest.java @@ -41,6 +41,7 @@ import java.io.IOException; import java.io.PrintWriter; import java.nio.file.Files; +import java.nio.file.Path; import java.nio.file.attribute.PosixFilePermission; import java.util.Collections; import java.util.Set; @@ -74,6 +75,7 @@ public void setup() throws Exception { when(neo4jConfig.get(any())).thenReturn(null); when(neo4jConfig.get(GraphDatabaseSettings.allow_file_urls)).thenReturn(false); when(neo4jConfig.expandCommands()).thenReturn(true); + when(neo4jConfig.get(GraphDatabaseSettings.neo4j_home)).thenReturn(Path.of("C:/neo4j/neo4j-enterprise-5.x.0")); apocConfigCommandExpansionFile = new File(getClass() .getClassLoader() @@ -150,6 +152,7 @@ public void testApocConfWithoutExpandCommands() { when(neo4jConfig.getDeclaredSettings()).thenReturn(Collections.emptyMap()); when(neo4jConfig.get(any())).thenReturn(null); when(neo4jConfig.expandCommands()).thenReturn(false); + when(neo4jConfig.get(GraphDatabaseSettings.neo4j_home)).thenReturn(Path.of("C:/neo4j/neo4j-enterprise-5.x.0")); GlobalProceduresRegistry registry = mock(GlobalProceduresRegistry.class); DatabaseManagementService databaseManagementService = mock(DatabaseManagementService.class); @@ -173,6 +176,7 @@ public void testMaxDecompressionRatioValidation() { when(neo4jConfig.getDeclaredSettings()).thenReturn(Collections.emptyMap()); when(neo4jConfig.get(any())).thenReturn(null); when(neo4jConfig.expandCommands()).thenReturn(false); + when(neo4jConfig.get(GraphDatabaseSettings.neo4j_home)).thenReturn(Path.of("C:/neo4j/neo4j-enterprise-5.x.0")); GlobalProceduresRegistry registry = mock(GlobalProceduresRegistry.class); DatabaseManagementService databaseManagementService = mock(DatabaseManagementService.class); diff --git a/common/src/test/java/apoc/ApocConfigTest.java b/common/src/test/java/apoc/ApocConfigTest.java index 277740cee..1f0eb7820 100644 --- a/common/src/test/java/apoc/ApocConfigTest.java +++ b/common/src/test/java/apoc/ApocConfigTest.java @@ -25,6 +25,7 @@ import static org.mockito.Mockito.when; import java.io.File; +import java.nio.file.Path; import java.util.Collections; import org.junit.Before; import org.junit.Test; @@ -49,6 +50,7 @@ public void setup() throws Exception { when(neo4jConfig.getDeclaredSettings()).thenReturn(Collections.emptyMap()); when(neo4jConfig.get(any())).thenReturn(null); when(neo4jConfig.get(GraphDatabaseSettings.allow_file_urls)).thenReturn(false); + when(neo4jConfig.get(GraphDatabaseSettings.neo4j_home)).thenReturn(Path.of("C:/neo4j/neo4j-enterprise-5.x.0")); apocConfigFile = new File(getClass().getClassLoader().getResource("apoc.conf").toURI()); @@ -69,7 +71,7 @@ private void setApocConfigSystemProperty() { @Test public void testDetermineNeo4jConfFolderDefault() { System.setProperty(SUN_JAVA_COMMAND, ""); - assertEquals(".", apocConfig.determineNeo4jConfFolder()); + assertEquals("C:/neo4j/neo4j-enterprise-5.x.0/conf", apocConfig.determineNeo4jConfFolder()); } @Test