2525import java .io .File ;
2626import java .net .URL ;
2727import java .util .concurrent .TimeUnit ;
28+ import lombok .SneakyThrows ;
2829import org .apache .bookkeeper .bookie .FileChannelProvider ;
2930import org .apache .bookkeeper .bookie .InterleavedLedgerStorage ;
3031import org .apache .bookkeeper .bookie .LedgerStorage ;
@@ -4047,12 +4048,7 @@ public boolean isSkipReplayJournalInvalidRecord() {
40474048 * @return String configured default rocksdb conf.
40484049 */
40494050 public String getDefaultRocksDBConf () {
4050- String defaultPath = "conf/default_rocksdb.conf" ;
4051- URL defURL = getClass ().getClassLoader ().getResource (defaultPath );
4052- if (defURL != null ) {
4053- defaultPath = defURL .getPath ();
4054- }
4055- return getString (DEFAULT_ROCKSDB_CONF , defaultPath );
4051+ return getString (DEFAULT_ROCKSDB_CONF , getDefaultFilePath ("conf/default_rocksdb.conf" ));
40564052 }
40574053
40584054 /**
@@ -4071,12 +4067,7 @@ public ServerConfiguration setDefaultRocksDBConf(String defaultRocksdbConf) {
40714067 * @return String configured entry Location rocksdb conf.
40724068 */
40734069 public String getEntryLocationRocksdbConf () {
4074- String defaultPath = "conf/entry_location_rocksdb.conf" ;
4075- URL defURL = getClass ().getClassLoader ().getResource (defaultPath );
4076- if (defURL != null ) {
4077- defaultPath = defURL .getPath ();
4078- }
4079- return getString (ENTRY_LOCATION_ROCKSDB_CONF , defaultPath );
4070+ return getString (ENTRY_LOCATION_ROCKSDB_CONF , getDefaultFilePath ("conf/entry_location_rocksdb.conf" ));
40804071 }
40814072
40824073 /**
@@ -4095,12 +4086,7 @@ public ServerConfiguration setEntryLocationRocksdbConf(String entryLocationRocks
40954086 * @return String configured ledger metadata rocksdb conf.
40964087 */
40974088 public String getLedgerMetadataRocksdbConf () {
4098- String defaultPath = "conf/ledger_metadata_rocksdb.conf" ;
4099- URL defURL = getClass ().getClassLoader ().getResource (defaultPath );
4100- if (defURL != null ) {
4101- defaultPath = defURL .getPath ();
4102- }
4103- return getString (LEDGER_METADATA_ROCKSDB_CONF , defaultPath );
4089+ return getString (LEDGER_METADATA_ROCKSDB_CONF , getDefaultFilePath ("conf/ledger_metadata_rocksdb.conf" ));
41044090 }
41054091
41064092 /**
@@ -4133,4 +4119,28 @@ public ServerConfiguration setOperationMaxNumbersInSingleRocksDBWriteBatch(int m
41334119 public int getMaxOperationNumbersInSingleRocksDBBatch () {
41344120 return getInt (MAX_OPERATION_NUMBERS_IN_SINGLE_ROCKSDB_WRITE_BATCH , 100000 );
41354121 }
4122+
4123+ /**
4124+ * Retrieves the default file path for the specified file name.
4125+ * This method prioritizes a file available in the classpath, which is often used in testing scenarios.
4126+ * If the file is not found in the classpath, the original file name is returned.
4127+ *
4128+ * @param fileName the name of the file for which to retrieve the path.
4129+ * @return the path of the file if found in the classpath, otherwise the input file name.
4130+ */
4131+ @ SneakyThrows
4132+ private String getDefaultFilePath (String fileName ) {
4133+ // Attempt to locate the file in the classpath, used mainly for testing purposes.
4134+ URL resourceURL = getClass ().getClassLoader ().getResource (fileName );
4135+ if (resourceURL != null && "file" .equals (resourceURL .getProtocol ())) {
4136+ // Convert the URL to a File object using toURI() for proper URL decoding
4137+ // and platform specific file path handling (such as on Windows OS)
4138+ File file = new File (resourceURL .toURI ());
4139+ if (file .exists ()) {
4140+ return file .getAbsolutePath ();
4141+ }
4142+ }
4143+ // Return the original file name if no path was found in the classpath
4144+ return fileName ;
4145+ }
41364146}
0 commit comments