Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NOID] Exclude OpenCSV bean-dependencies #606

Merged
merged 3 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion LICENSES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ Apache-2.0
commons-cli-1.2.jar
commons-codec-1.16.0.jar
commons-collections-3.2.2.jar
commons-collections4-4.4.jar
commons-compress-1.25.0.jar
commons-configuration2-2.9.0.jar
commons-csv-1.9.0.jar
Expand Down
1 change: 0 additions & 1 deletion NOTICE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ Apache-2.0
commons-cli-1.2.jar
commons-codec-1.16.0.jar
commons-collections-3.2.2.jar
commons-collections4-4.4.jar
commons-compress-1.25.0.jar
commons-configuration2-2.9.0.jar
commons-csv-1.9.0.jar
Expand Down
56 changes: 36 additions & 20 deletions common/src/main/java/apoc/ApocConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,28 @@
import java.io.IOException;
import java.lang.reflect.Field;
import java.net.URL;
import java.nio.file.Path;
import java.time.Duration;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
import org.apache.commons.configuration2.CombinedConfiguration;
import org.apache.commons.configuration2.Configuration;
import org.apache.commons.configuration2.EnvironmentConfiguration;
import org.apache.commons.configuration2.PropertiesConfiguration;
import org.apache.commons.configuration2.builder.combined.CombinedConfigurationBuilder;
import org.apache.commons.configuration2.builder.fluent.Parameters;
import org.apache.commons.configuration2.SystemConfiguration;
import org.apache.commons.configuration2.ex.ConfigurationException;
import org.apache.commons.configuration2.ex.ConversionException;
import org.apache.commons.configuration2.io.FileHandler;
import org.apache.commons.configuration2.tree.OverrideCombiner;
import org.neo4j.configuration.Config;
import org.neo4j.configuration.GraphDatabaseInternalSettings;
import org.neo4j.configuration.GraphDatabaseSettings;
import org.neo4j.dbms.api.DatabaseManagementService;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Path;
import org.neo4j.graphdb.config.Setting;
import org.neo4j.graphdb.security.URLAccessChecker;
import org.neo4j.kernel.api.procedure.GlobalProcedures;
Expand Down Expand Up @@ -87,7 +90,7 @@ public class ApocConfig extends LifecycleAdapter {
APOC_IMPORT_FILE_ENABLED, false,
APOC_IMPORT_FILE_USE_NEO4J_CONFIG, true,
APOC_TRIGGER_ENABLED, false);
private static final List<Setting> NEO4J_DIRECTORY_CONFIGURATION_SETTING_NAMES = new ArrayList<>(Arrays.asList(
private static final List<Setting<?>> NEO4J_DIRECTORY_CONFIGURATION_SETTING_NAMES = new ArrayList<>(Arrays.asList(
data_directory,
load_csv_file_url_root,
logs_directory,
Expand Down Expand Up @@ -115,6 +118,7 @@ public class ApocConfig extends LifecycleAdapter {
private boolean expandCommands;

private Duration commandEvaluationTimeout;
private File apocConfFile;

public ApocConfig(
Config neo4jConfig,
Expand All @@ -134,7 +138,7 @@ public ApocConfig(
theInstance = this;

// expose this config instance via `@Context ApocConfig config`
globalProceduresRegistry.registerComponent((Class<ApocConfig>) getClass(), ctx -> this, true);
globalProceduresRegistry.registerComponent(ApocConfig.class, ctx -> this, true);
this.log.info("successfully registered ApocConfig for @Context");
}

Expand Down Expand Up @@ -169,11 +173,12 @@ 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 or Neo4j default
// 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);
File apocConfFile = new File(neo4jConfFolder + "/apoc.conf");
apocConfFile = new File(neo4jConfFolder + "/apoc.conf");
// Command Expansion required check from Neo4j
if (apocConfFile.exists() && this.expandCommands) {
Config.Builder.validateFilePermissionForCommandExpansion(List.of(apocConfFile.toPath()));
Expand Down Expand Up @@ -211,15 +216,10 @@ protected String determineNeo4jConfFolder() {

/**
* use apache commons to load configuration
* classpath:/apoc-config.xml contains a description where to load configuration from
*/
protected void loadConfiguration() {
try {
URL resource = getClass().getClassLoader().getResource("apoc-config.xml");
log.info("loading apoc meta config from %s", resource.toString());
CombinedConfigurationBuilder builder = new CombinedConfigurationBuilder()
.configure(new Parameters().fileBased().setURL(resource));
config = builder.getConfiguration();
config = setupConfigurations(apocConfFile);

// Command Expansion if needed
config.getKeys()
Expand All @@ -237,7 +237,7 @@ protected void loadConfiguration() {
});

addDbmsDirectoriesMetricsSettings();
for (Setting s : NEO4J_DIRECTORY_CONFIGURATION_SETTING_NAMES) {
for (Setting<?> s : NEO4J_DIRECTORY_CONFIGURATION_SETTING_NAMES) {
Object value = neo4jConfig.get(s);
if (value != null) {
config.setProperty(s.name(), value.toString());
Expand All @@ -264,11 +264,29 @@ protected void loadConfiguration() {
}
}

private static Configuration setupConfigurations(File propertyFile) throws ConfigurationException {
PropertiesConfiguration configFile = new PropertiesConfiguration();
if (propertyFile.exists()) {
final FileHandler handler = new FileHandler(configFile);
handler.setFile(propertyFile);
handler.load();
}

// OverrideCombiner will evaluate keys in order, i.e. env before sys etc.
CombinedConfiguration combined = new CombinedConfiguration();
combined.setNodeCombiner(new OverrideCombiner());
combined.addConfiguration(new EnvironmentConfiguration());
combined.addConfiguration(new SystemConfiguration());
combined.addConfiguration(configFile);

return combined;
}

@SuppressWarnings("unchecked")
private void addDbmsDirectoriesMetricsSettings() {
try {
Class<?> metricsSettingsClass =
Class.forName("com.neo4j.kernel.impl.enterprise.configuration.MetricsSettings");
Field csvPathField = metricsSettingsClass.getDeclaredField("csvPath");
Class<?> metricsSettingsClass = Class.forName("com.neo4j.configuration.MetricsSettings");
Field csvPathField = metricsSettingsClass.getDeclaredField("csv_path");
Setting<Path> dbms_directories_metrics = (Setting<Path>) csvPathField.get(null);
NEO4J_DIRECTORY_CONFIGURATION_SETTING_NAMES.add(dbms_directories_metrics);
} catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException e) {
Expand Down Expand Up @@ -313,9 +331,7 @@ public void checkReadAllowed(String url, URLAccessChecker urlAccessChecker) thro

public void checkWriteAllowed(ExportConfig exportConfig, String fileName) {
if (!config.getBoolean(APOC_EXPORT_FILE_ENABLED)) {
if (exportConfig == null
|| (fileName != null && !fileName.equals(""))
|| !exportConfig.streamStatements()) {
if (exportConfig == null || (fileName != null && !fileName.isEmpty()) || !exportConfig.streamStatements()) {
throw new RuntimeException(EXPORT_TO_FILE_ERROR);
}
}
Expand Down
14 changes: 0 additions & 14 deletions common/src/main/resources/apoc-config.xml

This file was deleted.

5 changes: 4 additions & 1 deletion core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ dependencies {

// These will be dependencies packaged with the .jar
implementation project(":common")
implementation group: 'com.opencsv', name: 'opencsv', version: '5.7.1'
implementation group: 'com.opencsv', name: 'opencsv', version: '5.7.1', {
exclude group: 'commons-beanutils', module: 'commons-beanutils'
exclude group: 'org.apache.commons', module: 'commons-collections4'
}
implementation group: 'org.roaringbitmap', name: 'RoaringBitmap', version: '0.7.17'

def arrowExclusions = {
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/apoc/refactor/GraphRefactoring.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public Stream<NodeRefactorResult> cloneNodes(
// If there was an error, the procedure still passes, but this node + its rels should not
// be created. Instead, an error is returned to the user in the output.
if (withRelationships) {
for (Relationship rel: newNode.getRelationships()) {
for (Relationship rel : newNode.getRelationships()) {
rel.delete();
}
}
Expand Down
16 changes: 6 additions & 10 deletions core/src/test/java/apoc/refactor/GraphRefactoringTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import junit.framework.TestCase;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.hamcrest.Matchers;
Expand Down Expand Up @@ -1677,8 +1676,9 @@ public void testMergeRelsTrueAndProduceSelfRelFalse() {

@Test
public void issue3960WithCloneNodes() {
// Any node created in cloneNodes should not be committed if the entire query fails.
String query = """
// Any node created in cloneNodes should not be committed if the entire query fails.
String query =
"""
CREATE (original:Person {uid: "original"}), (original2:Person {uid: "original"})
WITH original, original2
CALL apoc.refactor.cloneNodes([original, original2], false, ["uid"])
Expand All @@ -1687,12 +1687,7 @@ public void issue3960WithCloneNodes() {
RETURN 1/0
""";

QueryExecutionException e = assertThrows(
QueryExecutionException.class,
() -> testCall(
db,
query,
(r) -> {}));
QueryExecutionException e = assertThrows(QueryExecutionException.class, () -> testCall(db, query, (r) -> {}));
Throwable except = ExceptionUtils.getRootCause(e);
TestCase.assertTrue(except instanceof RuntimeException);
TestCase.assertEquals("/ by zero", except.getMessage());
Expand All @@ -1704,7 +1699,8 @@ public void issue3960WithCloneNodes() {
public void ShouldErrorOnConstraintsFailedCommon() {
db.executeTransactionally(("CREATE CONSTRAINT unique_id FOR ()-[r:HAS_PET]-() REQUIRE r.id IS UNIQUE"));

String query = """
String query =
"""
CREATE (a:Person {name: 'Mark', city: 'London'})-[:HAS_PET {id: 1}]->(:Cat {name: "Mittens"})
WITH a
CALL apoc.refactor.cloneNodes([a], true, ["city"])
Expand Down
Loading