From 4a17d0505e8a1b395f74003832574381ef3a7f9e Mon Sep 17 00:00:00 2001 From: Hiroyuki Sato Date: Sat, 10 Aug 2024 22:16:37 +0900 Subject: [PATCH] Accept com.mysql.cj.util.TimeUtil in addition to com.mysql.jdbc.TimeUtil --- .../embulk/input/mysql/MySQLInputPlugin.java | 41 +++++++++++++++++-- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/embulk-input-mysql/src/main/java/org/embulk/input/mysql/MySQLInputPlugin.java b/embulk-input-mysql/src/main/java/org/embulk/input/mysql/MySQLInputPlugin.java index 7e18efa0..fac2c007 100644 --- a/embulk-input-mysql/src/main/java/org/embulk/input/mysql/MySQLInputPlugin.java +++ b/embulk-input-mysql/src/main/java/org/embulk/input/mysql/MySQLInputPlugin.java @@ -8,6 +8,8 @@ import java.sql.DriverManager; import java.sql.SQLException; import java.time.ZoneId; +import java.util.Arrays; +import java.util.List; import java.util.Optional; import java.util.Properties; import java.util.concurrent.atomic.AtomicReference; @@ -159,7 +161,7 @@ private void loadTimeZoneMappings() // Here implements a workaround as as workaround. Field f = null; try { - Class timeUtilClass = Class.forName("com.mysql.jdbc.TimeUtil"); + Class timeUtilClass = getTimeUtilClass(); f = timeUtilClass.getDeclaredField("timeZoneMappings"); f.setAccessible(true); @@ -167,12 +169,12 @@ private void loadTimeZoneMappings() if (timeZoneMappings == null) { timeZoneMappings = new Properties(); synchronized (timeUtilClass) { - timeZoneMappings.load(this.getClass().getResourceAsStream("/com/mysql/jdbc/TimeZoneMapping.properties")); + loadTimezoneMappings(timeZoneMappings); } f.set(null, timeZoneMappings); } } - catch (ClassNotFoundException | IllegalAccessException | NoSuchFieldException | IOException e) { + catch (IllegalAccessException | NoSuchFieldException e) { throw new RuntimeException(e); } finally { @@ -252,7 +254,40 @@ private static Class loadJdbcDriverClassForName(final { return (Class) Class.forName(className); } + private Class getTimeUtilClass() { + Class timeUtilClass; + List timeUtilClassNames = Arrays.asList( + "com.mysql.jdbc.TimeUtil", // Connector/J v5.X + "com.mysql.cj.util.TimeUtil" // Connector/J v8.X + ); + + for (String name : timeUtilClassNames) { + try { + timeUtilClass = Class.forName(name); + return timeUtilClass; + } catch (ClassNotFoundException e) { + // do nothing. + } + } + throw new ConfigException("Can't find TimeUtil Class"); + } + + private void loadTimezoneMappings(Properties timeZoneMappings) { + List timeZoneResources = Arrays.asList( + "/com/mysql/jdbc/TimeZoneMapping.properties", // Connector/J v5.X + "/com/mysql/cj/util/TimeZoneMapping.properties" // Connector/J v8.X + ); + for (String resource : timeZoneResources) { + try { + timeZoneMappings.load(this.getClass().getResourceAsStream(resource)); + return; + } catch (IOException | NullPointerException e) { + // do nothing. + } + } + throw new ConfigException("Can't find Timezone mapping property file."); + } private static final AtomicReference> mysqlJdbcDriver = new AtomicReference<>(); private static final Logger logger = LoggerFactory.getLogger(MySQLInputPlugin.class);