Skip to content

Commit 4a17d05

Browse files
committed
Accept com.mysql.cj.util.TimeUtil in addition to com.mysql.jdbc.TimeUtil
1 parent d5bb81c commit 4a17d05

File tree

1 file changed

+38
-3
lines changed

1 file changed

+38
-3
lines changed

embulk-input-mysql/src/main/java/org/embulk/input/mysql/MySQLInputPlugin.java

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import java.sql.DriverManager;
99
import java.sql.SQLException;
1010
import java.time.ZoneId;
11+
import java.util.Arrays;
12+
import java.util.List;
1113
import java.util.Optional;
1214
import java.util.Properties;
1315
import java.util.concurrent.atomic.AtomicReference;
@@ -159,20 +161,20 @@ private void loadTimeZoneMappings()
159161
// Here implements a workaround as as workaround.
160162
Field f = null;
161163
try {
162-
Class<?> timeUtilClass = Class.forName("com.mysql.jdbc.TimeUtil");
164+
Class<?> timeUtilClass = getTimeUtilClass();
163165
f = timeUtilClass.getDeclaredField("timeZoneMappings");
164166
f.setAccessible(true);
165167

166168
Properties timeZoneMappings = (Properties) f.get(null);
167169
if (timeZoneMappings == null) {
168170
timeZoneMappings = new Properties();
169171
synchronized (timeUtilClass) {
170-
timeZoneMappings.load(this.getClass().getResourceAsStream("/com/mysql/jdbc/TimeZoneMapping.properties"));
172+
loadTimezoneMappings(timeZoneMappings);
171173
}
172174
f.set(null, timeZoneMappings);
173175
}
174176
}
175-
catch (ClassNotFoundException | IllegalAccessException | NoSuchFieldException | IOException e) {
177+
catch (IllegalAccessException | NoSuchFieldException e) {
176178
throw new RuntimeException(e);
177179
}
178180
finally {
@@ -252,7 +254,40 @@ private static Class<? extends java.sql.Driver> loadJdbcDriverClassForName(final
252254
{
253255
return (Class<? extends java.sql.Driver>) Class.forName(className);
254256
}
257+
private Class<?> getTimeUtilClass() {
258+
Class<?> timeUtilClass;
259+
List<String> timeUtilClassNames = Arrays.asList(
260+
"com.mysql.jdbc.TimeUtil", // Connector/J v5.X
261+
"com.mysql.cj.util.TimeUtil" // Connector/J v8.X
262+
);
263+
264+
for (String name : timeUtilClassNames) {
265+
try {
266+
timeUtilClass = Class.forName(name);
267+
return timeUtilClass;
268+
} catch (ClassNotFoundException e) {
269+
// do nothing.
270+
}
271+
}
272+
throw new ConfigException("Can't find TimeUtil Class");
273+
}
274+
275+
private void loadTimezoneMappings(Properties timeZoneMappings) {
276+
List<String> timeZoneResources = Arrays.asList(
277+
"/com/mysql/jdbc/TimeZoneMapping.properties", // Connector/J v5.X
278+
"/com/mysql/cj/util/TimeZoneMapping.properties" // Connector/J v8.X
279+
);
255280

281+
for (String resource : timeZoneResources) {
282+
try {
283+
timeZoneMappings.load(this.getClass().getResourceAsStream(resource));
284+
return;
285+
} catch (IOException | NullPointerException e) {
286+
// do nothing.
287+
}
288+
}
289+
throw new ConfigException("Can't find Timezone mapping property file.");
290+
}
256291
private static final AtomicReference<Class<? extends java.sql.Driver>> mysqlJdbcDriver = new AtomicReference<>();
257292

258293
private static final Logger logger = LoggerFactory.getLogger(MySQLInputPlugin.class);

0 commit comments

Comments
 (0)