diff --git a/databricks-sdk-java/pom.xml b/databricks-sdk-java/pom.xml index cba1f785..a49f2205 100644 --- a/databricks-sdk-java/pom.xml +++ b/databricks-sdk-java/pom.xml @@ -49,9 +49,9 @@ provided - org.ini4j - ini4j - 0.5.4 + org.apache.commons + commons-configuration2 + 2.11.0 compile @@ -67,7 +67,7 @@ commons-io commons-io - 2.13.0 + 2.14.0 org.junit.jupiter diff --git a/databricks-sdk-java/src/main/java/com/databricks/sdk/core/ConfigLoader.java b/databricks-sdk-java/src/main/java/com/databricks/sdk/core/ConfigLoader.java index 933fa50a..47779d0e 100644 --- a/databricks-sdk-java/src/main/java/com/databricks/sdk/core/ConfigLoader.java +++ b/databricks-sdk-java/src/main/java/com/databricks/sdk/core/ConfigLoader.java @@ -1,16 +1,17 @@ package com.databricks.sdk.core; import com.databricks.sdk.core.utils.Environment; -import java.io.File; import java.io.FileNotFoundException; +import java.io.FileReader; import java.io.IOException; import java.lang.reflect.Field; import java.net.MalformedURLException; import java.net.URL; import java.nio.file.Paths; import java.util.*; -import org.ini4j.Ini; -import org.ini4j.Profile; +import org.apache.commons.configuration2.INIConfiguration; +import org.apache.commons.configuration2.SubnodeConfiguration; +import org.apache.commons.configuration2.ex.ConfigurationException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -40,7 +41,7 @@ public static DatabricksConfig resolve(DatabricksConfig cfg) throws DatabricksEx } } - static void loadFromEnvironmentVariables(DatabricksConfig cfg) throws IllegalAccessException { + static void loadFromEnvironmentVariables(DatabricksConfig cfg) { if (cfg.getEnv() == null) { return; } @@ -57,7 +58,7 @@ static void loadFromEnvironmentVariables(DatabricksConfig cfg) throws IllegalAcc } accessor.setValueOnConfig(cfg, env); } - } catch (DatabricksException e) { + } catch (DatabricksException | IllegalAccessException e) { String msg = String.format("%s auth: %s", cfg.getCredentialsProvider().authType(), e.getMessage()); throw new DatabricksException(msg, e); @@ -86,27 +87,27 @@ static void loadFromConfig(DatabricksConfig cfg) throws IllegalAccessException { configFile = configFile.replaceFirst("^~", userHome); } - Ini ini = parseDatabricksCfg(configFile, isDefaultConfig); + INIConfiguration ini = parseDatabricksCfg(configFile, isDefaultConfig); if (ini == null) return; + String profile = cfg.getProfile(); boolean hasExplicitProfile = !isNullOrEmpty(profile); if (!hasExplicitProfile) { profile = "DEFAULT"; } - - Profile.Section section = ini.get(profile); - if (section == null && !hasExplicitProfile) { + SubnodeConfiguration section = ini.getSection(profile); + boolean sectionNotPresent = section == null || section.isEmpty(); + if (sectionNotPresent && !hasExplicitProfile) { LOG.info("{} has no {} profile configured", configFile, profile); return; } - - if (section == null) { + if (sectionNotPresent) { String msg = String.format("resolve: %s has no %s profile configured", configFile, profile); throw new DatabricksException(msg); } for (ConfigAttributeAccessor accessor : accessors) { - String value = section.get(accessor.getName()); + String value = section.getString(accessor.getName()); if (!isNullOrEmpty(accessor.getValueFromConfig(cfg))) { continue; } @@ -114,18 +115,18 @@ static void loadFromConfig(DatabricksConfig cfg) throws IllegalAccessException { } } - private static Ini parseDatabricksCfg(String configFile, boolean isDefaultConfig) { - Ini ini = new Ini(); - try { - ini.load(new File(configFile)); + private static INIConfiguration parseDatabricksCfg(String configFile, boolean isDefaultConfig) { + INIConfiguration iniConfig = new INIConfiguration(); + try (FileReader reader = new FileReader(configFile)) { + iniConfig.read(reader); } catch (FileNotFoundException e) { if (isDefaultConfig) { return null; } - } catch (IOException e) { + } catch (IOException | ConfigurationException e) { throw new DatabricksException("Cannot load " + configFile, e); } - return ini; + return iniConfig; } public static void fixHostIfNeeded(DatabricksConfig cfg) { @@ -230,12 +231,12 @@ public static String debugString(DatabricksConfig cfg) { if (!attrsUsed.isEmpty()) { buf.add(String.format("Config: %s", String.join(", ", attrsUsed))); } else { - buf.add(String.format("Config: ")); + buf.add("Config: "); } if (!envsUsed.isEmpty()) { buf.add(String.format("Env: %s", String.join(", ", envsUsed))); } else { - buf.add(String.format("Env: ")); + buf.add("Env: "); } return String.join(". ", buf); } catch (IllegalAccessException e) {