|
8 | 8 | import java.sql.DriverManager;
|
9 | 9 | import java.sql.SQLException;
|
10 | 10 | import java.time.ZoneId;
|
| 11 | +import java.util.Arrays; |
| 12 | +import java.util.List; |
11 | 13 | import java.util.Optional;
|
12 | 14 | import java.util.Properties;
|
13 | 15 | import java.util.concurrent.atomic.AtomicReference;
|
@@ -159,20 +161,20 @@ private void loadTimeZoneMappings()
|
159 | 161 | // Here implements a workaround as as workaround.
|
160 | 162 | Field f = null;
|
161 | 163 | try {
|
162 |
| - Class<?> timeUtilClass = Class.forName("com.mysql.jdbc.TimeUtil"); |
| 164 | + Class<?> timeUtilClass = getTimeUtilClass(); |
163 | 165 | f = timeUtilClass.getDeclaredField("timeZoneMappings");
|
164 | 166 | f.setAccessible(true);
|
165 | 167 |
|
166 | 168 | Properties timeZoneMappings = (Properties) f.get(null);
|
167 | 169 | if (timeZoneMappings == null) {
|
168 | 170 | timeZoneMappings = new Properties();
|
169 | 171 | synchronized (timeUtilClass) {
|
170 |
| - timeZoneMappings.load(this.getClass().getResourceAsStream("/com/mysql/jdbc/TimeZoneMapping.properties")); |
| 172 | + loadTimezoneMappings(timeZoneMappings); |
171 | 173 | }
|
172 | 174 | f.set(null, timeZoneMappings);
|
173 | 175 | }
|
174 | 176 | }
|
175 |
| - catch (ClassNotFoundException | IllegalAccessException | NoSuchFieldException | IOException e) { |
| 177 | + catch (IllegalAccessException | NoSuchFieldException e) { |
176 | 178 | throw new RuntimeException(e);
|
177 | 179 | }
|
178 | 180 | finally {
|
@@ -252,7 +254,40 @@ private static Class<? extends java.sql.Driver> loadJdbcDriverClassForName(final
|
252 | 254 | {
|
253 | 255 | return (Class<? extends java.sql.Driver>) Class.forName(className);
|
254 | 256 | }
|
| 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 | + ); |
255 | 280 |
|
| 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 | + } |
256 | 291 | private static final AtomicReference<Class<? extends java.sql.Driver>> mysqlJdbcDriver = new AtomicReference<>();
|
257 | 292 |
|
258 | 293 | private static final Logger logger = LoggerFactory.getLogger(MySQLInputPlugin.class);
|
|
0 commit comments