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.
@@ -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
- 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
- }
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
+ }