This is an automated email from the ASF dual-hosted git repository.

eolivelli pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/bookkeeper.git


The following commit(s) were added to refs/heads/master by this push:
     new f34455b2da Improve locating config files (#4560)
f34455b2da is described below

commit f34455b2dafb7ce164b5f81db8b252a7b5cd1a7d
Author: Lari Hotari <[email protected]>
AuthorDate: Wed Apr 2 10:43:00 2025 +0300

    Improve locating config files (#4560)
---
 .../bookie/storage/ldb/KeyValueStorageRocksDB.java |  3 +-
 .../bookkeeper/conf/ServerConfiguration.java       | 35 +++++++++++++---------
 2 files changed, 23 insertions(+), 15 deletions(-)

diff --git 
a/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/storage/ldb/KeyValueStorageRocksDB.java
 
b/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/storage/ldb/KeyValueStorageRocksDB.java
index 22c5bc75db..c9e72847fb 100644
--- 
a/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/storage/ldb/KeyValueStorageRocksDB.java
+++ 
b/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/storage/ldb/KeyValueStorageRocksDB.java
@@ -40,6 +40,7 @@ import java.util.Map.Entry;
 import java.util.concurrent.TimeUnit;
 import 
org.apache.bookkeeper.bookie.storage.ldb.KeyValueStorageFactory.DbConfigType;
 import org.apache.bookkeeper.conf.ServerConfiguration;
+import org.apache.commons.lang3.StringUtils;
 import org.rocksdb.BlockBasedTableConfig;
 import org.rocksdb.BloomFilter;
 import org.rocksdb.Cache;
@@ -131,7 +132,7 @@ public class KeyValueStorageRocksDB implements 
KeyValueStorage {
             dbFilePath = conf.getDefaultRocksDBConf();
         }
         log.info("Searching for a RocksDB configuration file in {}", 
dbFilePath);
-        if (Paths.get(dbFilePath).toFile().exists()) {
+        if (StringUtils.isNotBlank(dbFilePath) && 
Paths.get(dbFilePath).toFile().exists()) {
             log.info("Found a RocksDB configuration file and using it to 
initialize the RocksDB");
             db = initializeRocksDBWithConfFile(basePath, subPath, 
dbConfigType, conf, readOnly, dbFilePath);
         } else {
diff --git 
a/bookkeeper-server/src/main/java/org/apache/bookkeeper/conf/ServerConfiguration.java
 
b/bookkeeper-server/src/main/java/org/apache/bookkeeper/conf/ServerConfiguration.java
index 65cfa2d7aa..e970cd6b09 100644
--- 
a/bookkeeper-server/src/main/java/org/apache/bookkeeper/conf/ServerConfiguration.java
+++ 
b/bookkeeper-server/src/main/java/org/apache/bookkeeper/conf/ServerConfiguration.java
@@ -24,8 +24,8 @@ import com.google.common.base.Strings;
 import com.google.common.collect.Lists;
 import java.io.File;
 import java.net.URL;
-import java.nio.file.Paths;
 import java.util.concurrent.TimeUnit;
+import lombok.SneakyThrows;
 import org.apache.bookkeeper.bookie.FileChannelProvider;
 import org.apache.bookkeeper.bookie.InterleavedLedgerStorage;
 import org.apache.bookkeeper.bookie.LedgerStorage;
@@ -4080,8 +4080,7 @@ public class ServerConfiguration extends 
AbstractConfiguration<ServerConfigurati
      * @return String configured default rocksdb conf.
      */
     public String getDefaultRocksDBConf() {
-        String filePath = getFilePath("conf/default_rocksdb.conf");
-        return getString(DEFAULT_ROCKSDB_CONF, filePath);
+        return getString(DEFAULT_ROCKSDB_CONF, 
getDefaultFilePath("conf/default_rocksdb.conf"));
     }
 
     /**
@@ -4100,8 +4099,7 @@ public class ServerConfiguration extends 
AbstractConfiguration<ServerConfigurati
      * @return String configured entry Location rocksdb conf.
      */
     public String getEntryLocationRocksdbConf() {
-        String filePath = getFilePath("conf/entry_location_rocksdb.conf");
-        return getString(ENTRY_LOCATION_ROCKSDB_CONF, filePath);
+        return getString(ENTRY_LOCATION_ROCKSDB_CONF, 
getDefaultFilePath("conf/entry_location_rocksdb.conf"));
     }
 
     /**
@@ -4120,8 +4118,7 @@ public class ServerConfiguration extends 
AbstractConfiguration<ServerConfigurati
      * @return String configured ledger metadata rocksdb conf.
      */
     public String getLedgerMetadataRocksdbConf() {
-        String filePath = getFilePath("conf/ledger_metadata_rocksdb.conf");
-        return getString(LEDGER_METADATA_ROCKSDB_CONF, filePath);
+        return getString(LEDGER_METADATA_ROCKSDB_CONF, 
getDefaultFilePath("conf/ledger_metadata_rocksdb.conf"));
     }
 
     /**
@@ -4176,16 +4173,26 @@ public class ServerConfiguration extends 
AbstractConfiguration<ServerConfigurati
     }
 
     /**
-     * Get the path of a file from resources.
+     * Retrieves the default file path for the specified file name.
+     * This method prioritizes a file available in the classpath, which is 
often used in testing scenarios.
+     * If the file is not found in the classpath, the original file name is 
returned.
      *
-     * @param fileName the name of the file to get the path for.
-     * @return String the absolute path of the file.
+     * @param fileName the name of the file for which to retrieve the path.
+     * @return the path of the file if found in the classpath, otherwise the 
input file name.
      */
-    private String getFilePath(String fileName) {
+    @SneakyThrows
+    private String getDefaultFilePath(String fileName) {
+        // Attempt to locate the file in the classpath, used mainly for 
testing purposes.
         URL resourceURL = getClass().getClassLoader().getResource(fileName);
-        if (resourceURL != null) {
-            return Paths.get(resourceURL.getPath()).toString();
+        if (resourceURL != null && "file".equals(resourceURL.getProtocol())) {
+            // Convert the URL to a File object using toURI() for proper URL 
decoding
+            // and platform specific file path handling (such as on Windows OS)
+            File file = new File(resourceURL.toURI());
+            if (file.exists()) {
+                return file.getAbsolutePath();
+            }
         }
-        return "";
+        // Return the original file name if no path was found in the classpath
+        return fileName;
     }
 }

Reply via email to