embulk-filter-mysql_lookup 0.1.5 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +12 -12
- data/LICENSE +20 -20
- data/README.md +161 -147
- data/build.gradle +98 -98
- data/classpath/embulk-filter-mysql_lookup-0.1.7.jar +0 -0
- data/config/checkstyle/checkstyle.xml +128 -128
- data/config/checkstyle/default.xml +108 -108
- data/example/mysql-filter-lookup_testing.yml.liquid +37 -0
- data/example/mysql-filter-lookup_testing_csv.yml.liquid +54 -0
- data/example/mysql-filter-lookup_testing_mssql.yml.liquid +39 -0
- data/example/mysql-filter-lookup_testing_oracle.yml.liquid +42 -0
- data/example/mysql-filter-lookup_testing_postgres.yml.liquid +42 -0
- data/gradle/wrapper/gradle-wrapper.properties +5 -5
- data/gradlew +172 -172
- data/gradlew.bat +84 -84
- data/lib/embulk/filter/mysql_lookup.rb +3 -3
- data/src/main/java/org/embulk/filter/mysql_lookup/DatabaseConnection.java +199 -195
- data/src/main/java/org/embulk/filter/mysql_lookup/MysqlLookupFilterPlugin.java +380 -362
- data/src/test/java/org/embulk/filter/mysql_lookup/TestMysqlLookupFilterPlugin.java +5 -5
- metadata +13 -11
- data/classpath/embulk-filter-mysql_lookup-0.1.5.jar +0 -0
- data/config1.yml +0 -47
- data/embulk-filter-mysql_lookup-0.1.3.gem +0 -0
- data/embulk-filter-mysql_lookup-0.1.4.gem +0 -0
- data/example/config1.yml +0 -47
@@ -1,195 +1,199 @@
|
|
1
|
-
package org.embulk.filter.mysql_lookup;
|
2
|
-
|
3
|
-
|
4
|
-
import org.embulk.config.ConfigException;
|
5
|
-
import org.slf4j.Logger;
|
6
|
-
import org.slf4j.LoggerFactory;
|
7
|
-
|
8
|
-
import java.io.File;
|
9
|
-
import java.io.FileFilter;
|
10
|
-
import java.lang.reflect.InvocationTargetException;
|
11
|
-
import java.lang.reflect.Method;
|
12
|
-
import java.net.MalformedURLException;
|
13
|
-
import java.net.URISyntaxException;
|
14
|
-
import java.net.URL;
|
15
|
-
import java.net.URLClassLoader;
|
16
|
-
import java.nio.file.Path;
|
17
|
-
import java.nio.file.Paths;
|
18
|
-
import java.sql.Connection;
|
19
|
-
import java.sql.DriverManager;
|
20
|
-
import java.util.Optional;
|
21
|
-
import java.util.concurrent.atomic.AtomicReference;
|
22
|
-
|
23
|
-
public class DatabaseConnection {
|
24
|
-
private static Connection connection=null;
|
25
|
-
|
26
|
-
private DatabaseConnection(MysqlLookupFilterPlugin.PluginTask task) throws Exception {
|
27
|
-
try{
|
28
|
-
if(task.getDriverClass().isPresent()){
|
29
|
-
this.loadMySqlJdbcDriver(task.getDriverClass().get(),task.getDriverPath());
|
30
|
-
}else{
|
31
|
-
this.loadMySqlJdbcDriver("com.mysql.cj.jdbc.Driver",task.getDriverPath());
|
32
|
-
}
|
33
|
-
String url = "jdbc:mysql://" + task.getHost() + ":"+task.getPort()+"/"+task.getDatabase();
|
34
|
-
connection= DriverManager.getConnection(url, task.getUserName(), task.getPassword());
|
35
|
-
}catch (Exception e){
|
36
|
-
e.printStackTrace();
|
37
|
-
throw new Exception(e);
|
38
|
-
}
|
39
|
-
|
40
|
-
}
|
41
|
-
|
42
|
-
public static Connection getConnection(MysqlLookupFilterPlugin.PluginTask task){
|
43
|
-
try {
|
44
|
-
if(connection==null || connection.isClosed()){
|
45
|
-
try {
|
46
|
-
new DatabaseConnection(task);
|
47
|
-
return connection;
|
48
|
-
} catch (Exception e) {
|
49
|
-
e.printStackTrace();
|
50
|
-
throw new RuntimeException();
|
51
|
-
}
|
52
|
-
}
|
53
|
-
}catch (Exception e){
|
54
|
-
throw new RuntimeException(e);
|
55
|
-
}
|
56
|
-
|
57
|
-
return connection;
|
58
|
-
}
|
59
|
-
|
60
|
-
private Class<? extends java.sql.Driver> loadMySqlJdbcDriver(
|
61
|
-
final String className,
|
62
|
-
final Optional<String> driverPath)
|
63
|
-
{
|
64
|
-
synchronized (mysqlJdbcDriver) {
|
65
|
-
if (mysqlJdbcDriver.get() != null) {
|
66
|
-
return mysqlJdbcDriver.get();
|
67
|
-
}
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
final File
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
}
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
if (!
|
147
|
-
throw new
|
148
|
-
}
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
}
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
throw new
|
167
|
-
}
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
if (folder
|
186
|
-
|
187
|
-
}
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
}
|
1
|
+
package org.embulk.filter.mysql_lookup;
|
2
|
+
|
3
|
+
|
4
|
+
import org.embulk.config.ConfigException;
|
5
|
+
import org.slf4j.Logger;
|
6
|
+
import org.slf4j.LoggerFactory;
|
7
|
+
|
8
|
+
import java.io.File;
|
9
|
+
import java.io.FileFilter;
|
10
|
+
import java.lang.reflect.InvocationTargetException;
|
11
|
+
import java.lang.reflect.Method;
|
12
|
+
import java.net.MalformedURLException;
|
13
|
+
import java.net.URISyntaxException;
|
14
|
+
import java.net.URL;
|
15
|
+
import java.net.URLClassLoader;
|
16
|
+
import java.nio.file.Path;
|
17
|
+
import java.nio.file.Paths;
|
18
|
+
import java.sql.Connection;
|
19
|
+
import java.sql.DriverManager;
|
20
|
+
import java.util.Optional;
|
21
|
+
import java.util.concurrent.atomic.AtomicReference;
|
22
|
+
|
23
|
+
public class DatabaseConnection {
|
24
|
+
private static Connection connection=null;
|
25
|
+
|
26
|
+
private DatabaseConnection(MysqlLookupFilterPlugin.PluginTask task) throws Exception {
|
27
|
+
try{
|
28
|
+
if(task.getDriverClass().isPresent()){
|
29
|
+
this.loadMySqlJdbcDriver(task.getDriverClass().get(),task.getDriverPath());
|
30
|
+
}else{
|
31
|
+
this.loadMySqlJdbcDriver("com.mysql.cj.jdbc.Driver",task.getDriverPath());
|
32
|
+
}
|
33
|
+
String url = "jdbc:mysql://" + task.getHost() + ":"+task.getPort()+"/"+task.getDatabase();
|
34
|
+
connection= DriverManager.getConnection(url, task.getUserName(), task.getPassword());
|
35
|
+
}catch (Exception e){
|
36
|
+
e.printStackTrace();
|
37
|
+
throw new Exception(e);
|
38
|
+
}
|
39
|
+
|
40
|
+
}
|
41
|
+
|
42
|
+
public static Connection getConnection(MysqlLookupFilterPlugin.PluginTask task){
|
43
|
+
try {
|
44
|
+
if(connection==null || connection.isClosed()){
|
45
|
+
try {
|
46
|
+
new DatabaseConnection(task);
|
47
|
+
return connection;
|
48
|
+
} catch (Exception e) {
|
49
|
+
e.printStackTrace();
|
50
|
+
throw new RuntimeException();
|
51
|
+
}
|
52
|
+
}
|
53
|
+
}catch (Exception e){
|
54
|
+
throw new RuntimeException(e);
|
55
|
+
}
|
56
|
+
|
57
|
+
return connection;
|
58
|
+
}
|
59
|
+
|
60
|
+
private Class<? extends java.sql.Driver> loadMySqlJdbcDriver(
|
61
|
+
final String className,
|
62
|
+
final Optional<String> driverPath)
|
63
|
+
{
|
64
|
+
synchronized (mysqlJdbcDriver) {
|
65
|
+
if (mysqlJdbcDriver.get() != null) {
|
66
|
+
return mysqlJdbcDriver.get();
|
67
|
+
}
|
68
|
+
|
69
|
+
if (driverPath.isPresent()) {
|
70
|
+
logger.info(
|
71
|
+
"\"driver_path\" is set to load the MySQL JDBC driver class \"{}\". Adding it to classpath.", className);
|
72
|
+
this.addDriverJarToClasspath(driverPath.get());
|
73
|
+
}
|
74
|
+
try {
|
75
|
+
// If the class is found from the ClassLoader of the plugin, that is prioritized the highest.
|
76
|
+
final Class<? extends java.sql.Driver> found = loadJdbcDriverClassForName(className);
|
77
|
+
mysqlJdbcDriver.compareAndSet(null, found);
|
78
|
+
|
79
|
+
if (driverPath.isPresent()) {
|
80
|
+
logger.warn(
|
81
|
+
"\"driver_path\" is set while the MySQL JDBC driver class \"{}\" is found from the PluginClassLoader."
|
82
|
+
+ " \"driver_path\" is ignored.", className);
|
83
|
+
}
|
84
|
+
return found;
|
85
|
+
}
|
86
|
+
catch (final ClassNotFoundException ex) {
|
87
|
+
//throw new ConfigException("The MySQL JDBC driver for the class \"" + className + "\" is not found.", ex);
|
88
|
+
}
|
89
|
+
final File root = this.findPluginRoot();
|
90
|
+
final File driverLib = new File(root, "default_jdbc_driver");
|
91
|
+
final File[] files = driverLib.listFiles(new FileFilter() {
|
92
|
+
@Override
|
93
|
+
public boolean accept(final File file)
|
94
|
+
{
|
95
|
+
return file.isFile() && file.getName().endsWith(".jar");
|
96
|
+
}
|
97
|
+
});
|
98
|
+
if (files == null || files.length == 0) {
|
99
|
+
throw new ConfigException(new ClassNotFoundException(
|
100
|
+
"The MySQL JDBC driver for the class \"" + className + "\" is not found"
|
101
|
+
+ " in \"default_jdbc_driver\" (" + root.getAbsolutePath() + ")."));
|
102
|
+
}
|
103
|
+
for (final File file : files) {
|
104
|
+
logger.info(
|
105
|
+
"The MySQL JDBC driver for the class \"{}\" is expected to be found"
|
106
|
+
+ " in \"default_jdbc_driver\" at {}.", className, file.getAbsolutePath());
|
107
|
+
this.addDriverJarToClasspath(file.getAbsolutePath());
|
108
|
+
}
|
109
|
+
|
110
|
+
try {
|
111
|
+
// If the class is found from the ClassLoader of the plugin, that is prioritized the highest.
|
112
|
+
final Class<? extends java.sql.Driver> found = loadJdbcDriverClassForName(className);
|
113
|
+
mysqlJdbcDriver.compareAndSet(null, found);
|
114
|
+
|
115
|
+
if (driverPath.isPresent()) {
|
116
|
+
logger.warn(
|
117
|
+
"\"driver_path\" is set while the MySQL JDBC driver class \"{}\" is found from the PluginClassLoader."
|
118
|
+
+ " \"driver_path\" is ignored.", className);
|
119
|
+
}
|
120
|
+
return found;
|
121
|
+
}
|
122
|
+
catch (final ClassNotFoundException ex) {
|
123
|
+
throw new ConfigException("The MySQL JDBC driver for the class \"" + className + "\" is not found.", ex);
|
124
|
+
}
|
125
|
+
|
126
|
+
}
|
127
|
+
}
|
128
|
+
|
129
|
+
@SuppressWarnings("unchecked")
|
130
|
+
private static Class<? extends java.sql.Driver> loadJdbcDriverClassForName(final String className) throws ClassNotFoundException
|
131
|
+
{
|
132
|
+
return (Class<? extends java.sql.Driver>) Class.forName(className);
|
133
|
+
}
|
134
|
+
|
135
|
+
private static final AtomicReference<Class<? extends java.sql.Driver>> mysqlJdbcDriver = new AtomicReference<>();
|
136
|
+
|
137
|
+
private static final Logger logger = LoggerFactory.getLogger(DatabaseConnection.class);
|
138
|
+
|
139
|
+
protected void addDriverJarToClasspath(String glob)
|
140
|
+
{
|
141
|
+
// TODO match glob
|
142
|
+
final ClassLoader loader = getClass().getClassLoader();
|
143
|
+
if (!(loader instanceof URLClassLoader)) {
|
144
|
+
throw new RuntimeException("Plugin is not loaded by URLClassLoader unexpectedly.");
|
145
|
+
}
|
146
|
+
if (!"org.embulk.plugin.PluginClassLoader".equals(loader.getClass().getName())) {
|
147
|
+
throw new RuntimeException("Plugin is not loaded by PluginClassLoader unexpectedly.");
|
148
|
+
}
|
149
|
+
Path path = Paths.get(glob);
|
150
|
+
if (!path.toFile().exists()) {
|
151
|
+
throw new ConfigException("The specified driver jar doesn't exist: " + glob);
|
152
|
+
}
|
153
|
+
final Method addPathMethod;
|
154
|
+
try {
|
155
|
+
addPathMethod = loader.getClass().getMethod("addPath", Path.class);
|
156
|
+
} catch (final NoSuchMethodException ex) {
|
157
|
+
throw new RuntimeException("Plugin is not loaded a ClassLoader which has addPath(Path), unexpectedly.");
|
158
|
+
}
|
159
|
+
try {
|
160
|
+
addPathMethod.invoke(loader, Paths.get(glob));
|
161
|
+
} catch (final IllegalAccessException ex) {
|
162
|
+
throw new RuntimeException(ex);
|
163
|
+
} catch (final InvocationTargetException ex) {
|
164
|
+
final Throwable targetException = ex.getTargetException();
|
165
|
+
if (targetException instanceof MalformedURLException) {
|
166
|
+
throw new IllegalArgumentException(targetException);
|
167
|
+
} else if (targetException instanceof RuntimeException) {
|
168
|
+
throw (RuntimeException) targetException;
|
169
|
+
} else {
|
170
|
+
throw new RuntimeException(targetException);
|
171
|
+
}
|
172
|
+
}
|
173
|
+
}
|
174
|
+
|
175
|
+
protected File findPluginRoot()
|
176
|
+
{
|
177
|
+
try {
|
178
|
+
URL url = getClass().getResource("/" + getClass().getName().replace('.', '/') + ".class");
|
179
|
+
if (url.toString().startsWith("jar:")) {
|
180
|
+
url = new URL(url.toString().replaceAll("^jar:", "").replaceAll("![^!]*$", ""));
|
181
|
+
}
|
182
|
+
|
183
|
+
File folder = new File(url.toURI()).getParentFile();
|
184
|
+
for (;; folder = folder.getParentFile()) {
|
185
|
+
if (folder == null) {
|
186
|
+
throw new RuntimeException("Cannot find 'embulk-filter-xxx' folder.");
|
187
|
+
}
|
188
|
+
|
189
|
+
if (folder.getName().startsWith("embulk-input-")) {
|
190
|
+
return folder;
|
191
|
+
}
|
192
|
+
}
|
193
|
+
} catch (MalformedURLException | URISyntaxException e) {
|
194
|
+
throw new RuntimeException(e);
|
195
|
+
}
|
196
|
+
}
|
197
|
+
|
198
|
+
|
199
|
+
}
|