embulk-filter-mysql_lookup 0.1.4 → 0.1.6
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 +147 -143
- data/build.gradle +98 -98
- data/classpath/embulk-filter-mysql_lookup-0.1.6.jar +0 -0
- data/config/checkstyle/checkstyle.xml +128 -128
- data/config/checkstyle/default.xml +108 -108
- data/example/{config.yml → config1.yml} +46 -44
- 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 +195 -38
- data/src/main/java/org/embulk/filter/mysql_lookup/MysqlLookupFilterPlugin.java +363 -360
- data/src/test/java/org/embulk/filter/mysql_lookup/TestMysqlLookupFilterPlugin.java +5 -5
- metadata +9 -9
- data/classpath/embulk-filter-mysql_lookup-0.1.4.jar +0 -0
- data/embulk-filter-mysql_lookup-0.1.3.gem +0 -0
@@ -1,38 +1,195 @@
|
|
1
|
-
package org.embulk.filter.mysql_lookup;
|
2
|
-
|
3
|
-
|
4
|
-
import
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
}
|
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
|
+
try {
|
70
|
+
// If the class is found from the ClassLoader of the plugin, that is prioritized the highest.
|
71
|
+
final Class<? extends java.sql.Driver> found = loadJdbcDriverClassForName(className);
|
72
|
+
mysqlJdbcDriver.compareAndSet(null, found);
|
73
|
+
|
74
|
+
if (driverPath.isPresent()) {
|
75
|
+
logger.warn(
|
76
|
+
"\"driver_path\" is set while the MySQL JDBC driver class \"{}\" is found from the PluginClassLoader."
|
77
|
+
+ " \"driver_path\" is ignored.", className);
|
78
|
+
}
|
79
|
+
return found;
|
80
|
+
}
|
81
|
+
catch (final ClassNotFoundException ex) {
|
82
|
+
// Pass-through once.
|
83
|
+
}
|
84
|
+
|
85
|
+
if (driverPath.isPresent()) {
|
86
|
+
logger.info(
|
87
|
+
"\"driver_path\" is set to load the MySQL JDBC driver class \"{}\". Adding it to classpath.", className);
|
88
|
+
this.addDriverJarToClasspath(driverPath.get());
|
89
|
+
}
|
90
|
+
else {
|
91
|
+
final File root = this.findPluginRoot();
|
92
|
+
final File driverLib = new File(root, "default_jdbc_driver");
|
93
|
+
final File[] files = driverLib.listFiles(new FileFilter() {
|
94
|
+
@Override
|
95
|
+
public boolean accept(final File file)
|
96
|
+
{
|
97
|
+
return file.isFile() && file.getName().endsWith(".jar");
|
98
|
+
}
|
99
|
+
});
|
100
|
+
if (files == null || files.length == 0) {
|
101
|
+
throw new ConfigException(new ClassNotFoundException(
|
102
|
+
"The MySQL JDBC driver for the class \"" + className + "\" is not found"
|
103
|
+
+ " in \"default_jdbc_driver\" (" + root.getAbsolutePath() + ")."));
|
104
|
+
}
|
105
|
+
for (final File file : files) {
|
106
|
+
logger.info(
|
107
|
+
"The MySQL JDBC driver for the class \"{}\" is expected to be found"
|
108
|
+
+ " in \"default_jdbc_driver\" at {}.", className, file.getAbsolutePath());
|
109
|
+
this.addDriverJarToClasspath(file.getAbsolutePath());
|
110
|
+
}
|
111
|
+
}
|
112
|
+
|
113
|
+
try {
|
114
|
+
// Retrying to find the class from the ClassLoader of the plugin.
|
115
|
+
final Class<? extends java.sql.Driver> found = loadJdbcDriverClassForName(className);
|
116
|
+
mysqlJdbcDriver.compareAndSet(null, found);
|
117
|
+
return found;
|
118
|
+
}
|
119
|
+
catch (final ClassNotFoundException ex) {
|
120
|
+
throw new ConfigException("The MySQL JDBC driver for the class \"" + className + "\" is not found.", ex);
|
121
|
+
}
|
122
|
+
}
|
123
|
+
}
|
124
|
+
|
125
|
+
@SuppressWarnings("unchecked")
|
126
|
+
private static Class<? extends java.sql.Driver> loadJdbcDriverClassForName(final String className) throws ClassNotFoundException
|
127
|
+
{
|
128
|
+
return (Class<? extends java.sql.Driver>) Class.forName(className);
|
129
|
+
}
|
130
|
+
|
131
|
+
private static final AtomicReference<Class<? extends java.sql.Driver>> mysqlJdbcDriver = new AtomicReference<>();
|
132
|
+
|
133
|
+
private static final Logger logger = LoggerFactory.getLogger(DatabaseConnection.class);
|
134
|
+
|
135
|
+
protected void addDriverJarToClasspath(String glob)
|
136
|
+
{
|
137
|
+
// TODO match glob
|
138
|
+
final ClassLoader loader = getClass().getClassLoader();
|
139
|
+
if (!(loader instanceof URLClassLoader)) {
|
140
|
+
throw new RuntimeException("Plugin is not loaded by URLClassLoader unexpectedly.");
|
141
|
+
}
|
142
|
+
if (!"org.embulk.plugin.PluginClassLoader".equals(loader.getClass().getName())) {
|
143
|
+
throw new RuntimeException("Plugin is not loaded by PluginClassLoader unexpectedly.");
|
144
|
+
}
|
145
|
+
Path path = Paths.get(glob);
|
146
|
+
if (!path.toFile().exists()) {
|
147
|
+
throw new ConfigException("The specified driver jar doesn't exist: " + glob);
|
148
|
+
}
|
149
|
+
final Method addPathMethod;
|
150
|
+
try {
|
151
|
+
addPathMethod = loader.getClass().getMethod("addPath", Path.class);
|
152
|
+
} catch (final NoSuchMethodException ex) {
|
153
|
+
throw new RuntimeException("Plugin is not loaded a ClassLoader which has addPath(Path), unexpectedly.");
|
154
|
+
}
|
155
|
+
try {
|
156
|
+
addPathMethod.invoke(loader, Paths.get(glob));
|
157
|
+
} catch (final IllegalAccessException ex) {
|
158
|
+
throw new RuntimeException(ex);
|
159
|
+
} catch (final InvocationTargetException ex) {
|
160
|
+
final Throwable targetException = ex.getTargetException();
|
161
|
+
if (targetException instanceof MalformedURLException) {
|
162
|
+
throw new IllegalArgumentException(targetException);
|
163
|
+
} else if (targetException instanceof RuntimeException) {
|
164
|
+
throw (RuntimeException) targetException;
|
165
|
+
} else {
|
166
|
+
throw new RuntimeException(targetException);
|
167
|
+
}
|
168
|
+
}
|
169
|
+
}
|
170
|
+
|
171
|
+
protected File findPluginRoot()
|
172
|
+
{
|
173
|
+
try {
|
174
|
+
URL url = getClass().getResource("/" + getClass().getName().replace('.', '/') + ".class");
|
175
|
+
if (url.toString().startsWith("jar:")) {
|
176
|
+
url = new URL(url.toString().replaceAll("^jar:", "").replaceAll("![^!]*$", ""));
|
177
|
+
}
|
178
|
+
|
179
|
+
File folder = new File(url.toURI()).getParentFile();
|
180
|
+
for (;; folder = folder.getParentFile()) {
|
181
|
+
if (folder == null) {
|
182
|
+
throw new RuntimeException("Cannot find 'embulk-filter-xxx' folder.");
|
183
|
+
}
|
184
|
+
|
185
|
+
if (folder.getName().startsWith("embulk-input-")) {
|
186
|
+
return folder;
|
187
|
+
}
|
188
|
+
}
|
189
|
+
} catch (MalformedURLException | URISyntaxException e) {
|
190
|
+
throw new RuntimeException(e);
|
191
|
+
}
|
192
|
+
}
|
193
|
+
|
194
|
+
|
195
|
+
}
|