From 662847bd76db2186b7a12ba470ae75356bb8cc51 Mon Sep 17 00:00:00 2001 From: Zhou Yang Date: Sat, 2 Nov 2024 10:09:49 +0800 Subject: [PATCH] support loading properties file in unnamed resources module. (#1827) --- .../java/com/zaxxer/hikari/HikariConfig.java | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/zaxxer/hikari/HikariConfig.java b/src/main/java/com/zaxxer/hikari/HikariConfig.java index 4f280d349..289232839 100644 --- a/src/main/java/com/zaxxer/hikari/HikariConfig.java +++ b/src/main/java/com/zaxxer/hikari/HikariConfig.java @@ -26,9 +26,7 @@ import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.DataSource; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; +import java.io.*; import java.lang.reflect.Modifier; import java.security.AccessControlException; import java.sql.Connection; @@ -1199,8 +1197,7 @@ else if (value == null) { private void loadProperties(String propertyFileName) { - final var propFile = new File(propertyFileName); - try (final var is = propFile.isFile() ? new FileInputStream(propFile) : this.getClass().getResourceAsStream(propertyFileName)) { + try (final var is = openPropertiesInputStream(propertyFileName)) { if (is != null) { var props = new Properties(); props.load(is); @@ -1215,6 +1212,18 @@ private void loadProperties(String propertyFileName) } } + private InputStream openPropertiesInputStream(String propertyFileName) throws FileNotFoundException { + final var propFile = new File(propertyFileName); + if (propFile.isFile()) { + return new FileInputStream(propFile); + } + var propertiesInputStream = this.getClass().getResourceAsStream(propertyFileName); + if (propertiesInputStream == null) { + propertiesInputStream = this.getClass().getClassLoader().getResourceAsStream(propertyFileName); + } + return propertiesInputStream; + } + private String generatePoolName() { final var prefix = "HikariPool-";