2424import com .google .common .collect .Lists ;
2525import java .io .File ;
2626import java .net .URL ;
27- import java .nio .file .Paths ;
2827import java .util .concurrent .TimeUnit ;
28+ import lombok .SneakyThrows ;
2929import org .apache .bookkeeper .bookie .FileChannelProvider ;
3030import org .apache .bookkeeper .bookie .InterleavedLedgerStorage ;
3131import org .apache .bookkeeper .bookie .LedgerStorage ;
@@ -4080,8 +4080,7 @@ public boolean isSkipReplayJournalInvalidRecord() {
40804080 * @return String configured default rocksdb conf.
40814081 */
40824082 public String getDefaultRocksDBConf () {
4083- String filePath = getFilePath ("conf/default_rocksdb.conf" );
4084- return getString (DEFAULT_ROCKSDB_CONF , filePath );
4083+ return getString (DEFAULT_ROCKSDB_CONF , getDefaultFilePath ("conf/default_rocksdb.conf" ));
40854084 }
40864085
40874086 /**
@@ -4100,8 +4099,7 @@ public ServerConfiguration setDefaultRocksDBConf(String defaultRocksdbConf) {
41004099 * @return String configured entry Location rocksdb conf.
41014100 */
41024101 public String getEntryLocationRocksdbConf () {
4103- String filePath = getFilePath ("conf/entry_location_rocksdb.conf" );
4104- return getString (ENTRY_LOCATION_ROCKSDB_CONF , filePath );
4102+ return getString (ENTRY_LOCATION_ROCKSDB_CONF , getDefaultFilePath ("conf/entry_location_rocksdb.conf" ));
41054103 }
41064104
41074105 /**
@@ -4120,8 +4118,7 @@ public ServerConfiguration setEntryLocationRocksdbConf(String entryLocationRocks
41204118 * @return String configured ledger metadata rocksdb conf.
41214119 */
41224120 public String getLedgerMetadataRocksdbConf () {
4123- String filePath = getFilePath ("conf/ledger_metadata_rocksdb.conf" );
4124- return getString (LEDGER_METADATA_ROCKSDB_CONF , filePath );
4121+ return getString (LEDGER_METADATA_ROCKSDB_CONF , getDefaultFilePath ("conf/ledger_metadata_rocksdb.conf" ));
41254122 }
41264123
41274124 /**
@@ -4176,16 +4173,26 @@ public long getMaxBatchReadSize() {
41764173 }
41774174
41784175 /**
4179- * Get the path of a file from resources.
4176+ * Retrieves the default file path for the specified file name.
4177+ * This method prioritizes a file available in the classpath, which is often used in testing scenarios.
4178+ * If the file is not found in the classpath, the original file name is returned.
41804179 *
4181- * @param fileName the name of the file to get the path for .
4182- * @return String the absolute path of the file.
4180+ * @param fileName the name of the file for which to retrieve the path.
4181+ * @return the path of the file if found in the classpath, otherwise the input file name .
41834182 */
4184- private String getFilePath (String fileName ) {
4183+ @ SneakyThrows
4184+ private String getDefaultFilePath (String fileName ) {
4185+ // Attempt to locate the file in the classpath, used mainly for testing purposes.
41854186 URL resourceURL = getClass ().getClassLoader ().getResource (fileName );
4186- if (resourceURL != null ) {
4187- return Paths .get (resourceURL .getPath ()).toString ();
4187+ if (resourceURL != null && "file" .equals (resourceURL .getProtocol ())) {
4188+ // Convert the URL to a File object using toURI() for proper URL decoding
4189+ // and platform specific file path handling (such as on Windows OS)
4190+ File file = new File (resourceURL .toURI ());
4191+ if (file .exists ()) {
4192+ return file .getAbsolutePath ();
4193+ }
41884194 }
4189- return "" ;
4195+ // Return the original file name if no path was found in the classpath
4196+ return fileName ;
41904197 }
41914198}
0 commit comments