Skip to content

Commit

Permalink
[4Bece3wd] Use Neo4j home to determine default config location. (#586)
Browse files Browse the repository at this point in the history
This will be used in Windows service, where sun.java.command does not work.
  • Loading branch information
Lojjs authored Feb 26, 2024
1 parent fe271e2 commit dc9369f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
20 changes: 12 additions & 8 deletions common/src/main/java/apoc/ApocConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.";
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down
4 changes: 4 additions & 0 deletions common/src/test/java/apoc/ApocConfigCommandExpansionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion common/src/test/java/apoc/ApocConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
Expand All @@ -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
Expand Down

0 comments on commit dc9369f

Please sign in to comment.