src/main/java/org/embulk/output/JdbcOutputPlugin.java in embulk-output-jdbc-0.1.2 vs src/main/java/org/embulk/output/JdbcOutputPlugin.java in embulk-output-jdbc-0.1.3
- old
+ new
@@ -1,31 +1,43 @@
package org.embulk.output;
+import java.nio.file.Paths;
+import java.util.Set;
+import java.util.HashSet;
import java.util.Properties;
import java.sql.Driver;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
+import com.google.common.base.Optional;
import com.google.common.base.Throwables;
import org.embulk.spi.Exec;
+import org.embulk.spi.PluginClassLoader;
import org.embulk.config.Config;
+import org.embulk.config.ConfigDefault;
import org.embulk.output.jdbc.AbstractJdbcOutputPlugin;
import org.embulk.output.jdbc.BatchInsert;
import org.embulk.output.jdbc.StandardBatchInsert;
import org.embulk.output.jdbc.JdbcOutputConnector;
import org.embulk.output.jdbc.JdbcOutputConnection;
public class JdbcOutputPlugin
extends AbstractJdbcOutputPlugin
{
+ private final static Set<String> loadedJarGlobs = new HashSet<String>();
+
public interface GenericPluginTask extends PluginTask
{
@Config("driver_name")
public String getDriverName();
@Config("driver_class")
public String getDriverClass();
+
+ @Config("driver_path")
+ @ConfigDefault("null")
+ public Optional<String> getDriverPath();
}
@Override
protected Class<? extends PluginTask> getTaskClass()
{
@@ -35,16 +47,26 @@
@Override
protected GenericOutputConnector getConnector(PluginTask task, boolean retryableMetadataOperation)
{
GenericPluginTask g = (GenericPluginTask) task;
+ if (g.getDriverPath().isPresent()) {
+ synchronized (loadedJarGlobs) {
+ String glob = g.getDriverPath().get();
+ if (!loadedJarGlobs.contains(glob)) {
+ loadDriverJar(glob);
+ loadedJarGlobs.add(glob);
+ }
+ }
+ }
+
String url;
if (g.getPort().isPresent()) {
url = String.format("jdbc:%s://%s:%d/%s",
g.getDriverName(), g.getHost(), g.getPort().get(), g.getDatabase());
} else {
- url = String.format("jdbc:%s://%s:%d/%s",
+ url = String.format("jdbc:%s://%s/%s",
g.getDriverName(), g.getHost(), g.getDatabase());
}
Properties props = new Properties();
props.setProperty("user", g.getUser());
@@ -52,9 +74,16 @@
props.putAll(g.getOptions());
return new GenericOutputConnector(url, props, g.getDriverClass(),
g.getSchema().orNull());
+ }
+
+ private void loadDriverJar(String glob)
+ {
+ // TODO match glob
+ PluginClassLoader loader = (PluginClassLoader) getClass().getClassLoader();
+ loader.addPath(Paths.get(glob));
}
private static class GenericOutputConnector
implements JdbcOutputConnector
{