From 2400b50f680eec9d8a3a7b918dd4cfba7eace1cf Mon Sep 17 00:00:00 2001 From: Andrew Outzen Date: Wed, 19 Jan 2022 15:05:34 -0600 Subject: [PATCH] Update population of driver properties in DriverDataSource to support non-String Object values. --- .../java/com/zaxxer/hikari/HikariConfig.java | 3 +++ .../com/zaxxer/hikari/HikariJNDIFactory.java | 2 +- .../zaxxer/hikari/util/DriverDataSource.java | 2 +- .../com/zaxxer/hikari/util/PropertyElf.java | 2 +- .../java/com/zaxxer/hikari/pool/TestJNDI.java | 6 +++-- .../zaxxer/hikari/util/PropertyElfTest.java | 22 +++++++++++++++++++ 6 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/zaxxer/hikari/HikariConfig.java b/src/main/java/com/zaxxer/hikari/HikariConfig.java index e2b34dc7d..bcecd5ad3 100644 --- a/src/main/java/com/zaxxer/hikari/HikariConfig.java +++ b/src/main/java/com/zaxxer/hikari/HikariConfig.java @@ -1105,6 +1105,9 @@ private void logConfiguration() if ("dataSourceProperties".equals(prop)) { var dsProps = PropertyElf.copyProperties(dataSourceProperties); dsProps.setProperty("password", ""); + if (dsProps.containsKey("privateKey")) { + dsProps.setProperty("privateKey", ""); + } value = dsProps; } diff --git a/src/main/java/com/zaxxer/hikari/HikariJNDIFactory.java b/src/main/java/com/zaxxer/hikari/HikariJNDIFactory.java index c4d93afb9..c5a5b2a25 100644 --- a/src/main/java/com/zaxxer/hikari/HikariJNDIFactory.java +++ b/src/main/java/com/zaxxer/hikari/HikariJNDIFactory.java @@ -45,7 +45,7 @@ synchronized public Object getObjectInstance(Object obj, Name name, Context name var element = enumeration.nextElement(); var type = element.getType(); if (type.startsWith("dataSource.") || hikariPropSet.contains(type)) { - properties.setProperty(type, element.getContent().toString()); + properties.put(type, element.getContent()); } } return createDataSource(properties, nameCtx); diff --git a/src/main/java/com/zaxxer/hikari/util/DriverDataSource.java b/src/main/java/com/zaxxer/hikari/util/DriverDataSource.java index 90d042b8d..7b6ad2787 100644 --- a/src/main/java/com/zaxxer/hikari/util/DriverDataSource.java +++ b/src/main/java/com/zaxxer/hikari/util/DriverDataSource.java @@ -46,7 +46,7 @@ public DriverDataSource(String jdbcUrl, String driverClassName, Properties prope this.driverProperties = new Properties(); for (var entry : properties.entrySet()) { - driverProperties.setProperty(entry.getKey().toString(), entry.getValue().toString()); + driverProperties.put(entry.getKey().toString(), entry.getValue()); } if (username != null) { diff --git a/src/main/java/com/zaxxer/hikari/util/PropertyElf.java b/src/main/java/com/zaxxer/hikari/util/PropertyElf.java index 9cebdfbab..c4c62387a 100644 --- a/src/main/java/com/zaxxer/hikari/util/PropertyElf.java +++ b/src/main/java/com/zaxxer/hikari/util/PropertyElf.java @@ -105,7 +105,7 @@ public static Object getProperty(final String propName, final Object target) public static Properties copyProperties(final Properties props) { var copy = new Properties(); - props.forEach((key, value) -> copy.setProperty(key.toString(), value.toString())); + props.forEach((key, value) -> copy.put(key.toString(), value)); return copy; } diff --git a/src/test/java/com/zaxxer/hikari/pool/TestJNDI.java b/src/test/java/com/zaxxer/hikari/pool/TestJNDI.java index 4f2dd0efb..be62a638c 100644 --- a/src/test/java/com/zaxxer/hikari/pool/TestJNDI.java +++ b/src/test/java/com/zaxxer/hikari/pool/TestJNDI.java @@ -44,11 +44,13 @@ public void testJndiLookup1() throws Exception ref.add(new BogusRef("maxLifetime", "30000")); ref.add(new BogusRef("maximumPoolSize", "10")); ref.add(new BogusRef("dataSource.loginTimeout", "10")); + ref.add(new BogusRef("dataSource.nonStringObj", Integer.valueOf(10))); Context nameCtx = new BogusContext(); try (HikariDataSource ds = (HikariDataSource) jndi.getObjectInstance(ref, null, nameCtx, null)) { assertNotNull(ds); assertEquals("foo", getUnsealedConfig(ds).getUsername()); + assertEquals(ds.getDataSourceProperties().get("nonStringObj"), Integer.valueOf(10)); } } @@ -149,8 +151,8 @@ private class BogusRef extends RefAddr { private static final long serialVersionUID = 1L; - private String content; - BogusRef(String type, String content) + private Object content; + BogusRef(String type, Object content) { super(type); this.content = content; diff --git a/src/test/java/com/zaxxer/hikari/util/PropertyElfTest.java b/src/test/java/com/zaxxer/hikari/util/PropertyElfTest.java index 983ef0bf9..262cf67e6 100644 --- a/src/test/java/com/zaxxer/hikari/util/PropertyElfTest.java +++ b/src/test/java/com/zaxxer/hikari/util/PropertyElfTest.java @@ -41,4 +41,26 @@ public void setTargetFromPropertiesNotAClass() throws Exception assertEquals("argument type mismatch", e.getCause().getMessage()); } } + + @Test + public void copyPropertiesPropValueIsString() { + Properties propertiesOrig = new Properties(); + String stringPropName = "stringProp"; + String stringPropValue = "aString"; + propertiesOrig.setProperty(stringPropName, stringPropValue); + + Properties propertiesCopy = PropertyElf.copyProperties(propertiesOrig); + assertEquals(stringPropValue, propertiesCopy.getProperty(stringPropName)); + } + + @Test + public void copyPropertiesPropValueIsNonStringObject() { + Properties propertiesOrig = new Properties(); + String propName = "nonStringProp"; + Integer propValue = 10; + propertiesOrig.put(propName, propValue); + + Properties propertiesCopy = PropertyElf.copyProperties(propertiesOrig); + assertEquals(propValue, propertiesCopy.get(propName)); + } }