|
75 | 75 | import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.Lists;
|
76 | 76 | import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.io.ByteStreams;
|
77 | 77 | import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.io.Files;
|
| 78 | +import org.apache.commons.dbcp2.BasicDataSource; |
78 | 79 | import org.checkerframework.checker.nullness.qual.Nullable;
|
79 | 80 | import org.joda.time.DateTime;
|
80 | 81 | import org.joda.time.Duration;
|
@@ -712,24 +713,41 @@ void reportLineage(Lineage lineage, @Nullable KV<@Nullable String, String> table
|
712 | 713 | String maybeSqlInstance;
|
713 | 714 | String url;
|
714 | 715 | try {
|
715 |
| - Class<?> hikariClass = Class.forName("com.zaxxer.hikari.HikariDataSource"); |
716 |
| - if (!hikariClass.isInstance(dataSource)) { |
717 |
| - return null; |
718 |
| - } |
719 |
| - Method getProperties = hikariClass.getMethod("getDataSourceProperties"); |
720 |
| - Properties properties = (Properties) getProperties.invoke(dataSource); |
721 |
| - if (properties == null) { |
722 |
| - return null; |
723 |
| - } |
724 |
| - maybeSqlInstance = properties.getProperty("cloudSqlInstance"); |
725 |
| - if (maybeSqlInstance == null) { |
726 |
| - // not a cloudSqlInstance |
727 |
| - return null; |
728 |
| - } |
729 |
| - Method getUrl = hikariClass.getMethod("getJdbcUrl"); |
730 |
| - url = (String) getUrl.invoke(dataSource); |
731 |
| - if (url == null) { |
732 |
| - return null; |
| 716 | + if (dataSource instanceof BasicDataSource) { |
| 717 | + // try default data source implementation |
| 718 | + BasicDataSource source = (BasicDataSource) dataSource; |
| 719 | + Method getProperties = source.getClass().getDeclaredMethod("getConnectionProperties"); |
| 720 | + getProperties.setAccessible(true); |
| 721 | + Properties properties = (Properties) getProperties.invoke(dataSource); |
| 722 | + if (properties == null) { |
| 723 | + return null; |
| 724 | + } |
| 725 | + maybeSqlInstance = properties.getProperty("cloudSqlInstance"); |
| 726 | + if (maybeSqlInstance == null) { |
| 727 | + // not a cloudSqlInstance |
| 728 | + return null; |
| 729 | + } |
| 730 | + url = source.getUrl(); |
| 731 | + } else { // try recommended as per best practice |
| 732 | + Class<?> hikariClass = Class.forName("com.zaxxer.hikari.HikariDataSource"); |
| 733 | + if (!hikariClass.isInstance(dataSource)) { |
| 734 | + return null; |
| 735 | + } |
| 736 | + Method getProperties = hikariClass.getMethod("getDataSourceProperties"); |
| 737 | + Properties properties = (Properties) getProperties.invoke(dataSource); |
| 738 | + if (properties == null) { |
| 739 | + return null; |
| 740 | + } |
| 741 | + maybeSqlInstance = properties.getProperty("cloudSqlInstance"); |
| 742 | + if (maybeSqlInstance == null) { |
| 743 | + // not a cloudSqlInstance |
| 744 | + return null; |
| 745 | + } |
| 746 | + Method getUrl = hikariClass.getMethod("getJdbcUrl"); |
| 747 | + url = (String) getUrl.invoke(dataSource); |
| 748 | + if (url == null) { |
| 749 | + return null; |
| 750 | + } |
733 | 751 | }
|
734 | 752 | } catch (ClassNotFoundException
|
735 | 753 | | InvocationTargetException
|
|
0 commit comments