Commit a09aed07 authored by Matija Obreza's avatar Matija Obreza

Load JDBC drivers from {workspace}/jdbc folder

parent 89884aa7
......@@ -175,11 +175,6 @@
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.32</version>
</dependency>
</dependencies>
<build>
......
......@@ -53,7 +53,14 @@ import org.eclipse.swt.dnd.DragSource;
import org.eclipse.swt.dnd.DragSourceAdapter;
import org.eclipse.swt.dnd.DragSourceEvent;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.events.*;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.MenuAdapter;
import org.eclipse.swt.events.MenuEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Control;
......
package org.genesys2.anno.gui;
import java.sql.Connection;
import java.sql.SQLException;
import org.apache.log4j.Logger;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.beans.BeanProperties;
......@@ -13,13 +16,21 @@ import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.genesys2.anno.model.DatabaseSettings;
import org.genesys2.anno.util.ConnectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import swing2swt.layout.BorderLayout;
import java.sql.Connection;
import swing2swt.layout.BorderLayout;
public class DatabaseDialog extends Dialog {
private static final Logger _log = Logger.getLogger(DatabaseDialog.class);
......@@ -185,25 +196,33 @@ public class DatabaseDialog extends Dialog {
}
private void doConnect() {
Connection connection = ConnectionUtils.getConnection(this.txtDbUrl.getText(), this.txtDbUserName.getText(), this.txtDbPassword.getText());
MessageBox dialog = null;
Connection connection = null;
String message = "N/A";
try {
connection = ConnectionUtils.getConnection(this.txtDbUrl.getText(), this.txtDbUserName.getText(), this.txtDbPassword.getText());
} catch (ClassNotFoundException e) {
message = "Could not load JDBC driver " + e.getMessage();
} catch (SQLException e) {
message = e.getMessage();
}
MessageBox dialog = null;
if (connection == null) {
dialog = new MessageBox(shell, SWT.ICON_ERROR | SWT.CANCEL);
dialog.setText("Connection");
dialog.setMessage("Connection failed");
dialog.setMessage(message);
dialog.open();
} else {
dialog = new MessageBox(shell, SWT.ICON_WORKING | SWT.OK | SWT.CANCEL);
dialog.setText("Connection");
dialog.setMessage("Connection successful!\nDo you want to add connection to workspace?");
}
int button = dialog.open();
if (button == SWT.OK) {
doConnection(connection);
if (dialog.open() == SWT.OK) {
doConnection(connection);
}
}
}
......
package org.genesys2.anno.gui;
import java.io.File;
import java.io.FilenameFilter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import org.apache.log4j.Logger;
public class ExtraClassLoader extends URLClassLoader {
private static final Logger _log = Logger.getLogger(AppWindow.class);
public ExtraClassLoader(ClassLoader parentClassLoader) {
super(new URL[] {}, parentClassLoader);
}
public void addJars(File baseDir) throws MalformedURLException {
if (!baseDir.isDirectory()) {
_log.warn("Not a directory: " + baseDir);
return;
}
if (!baseDir.exists() || !baseDir.canRead()) {
_log.warn("Cannot read directory: " + baseDir);
return;
}
File[] jarFiles = baseDir.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.endsWith(".jar");
}
});
for (File jarFile : jarFiles) {
addJar(jarFile);
}
}
private void addJar(File jarFile) throws MalformedURLException {
URL url = new URL("jar:file://" + jarFile.getAbsolutePath() + "!/");
_log.info("Adding JAR file " + url);
addURL(url);
}
}
\ No newline at end of file
......@@ -525,7 +525,7 @@ public class SheetDisplay extends Composite {
if (rows != null) {
currentSheet.updateData(rows);
}
} catch (SQLException e) {
} catch (Throwable e) {
showMessageBox("SQL error", e);
_log.error(e.getMessage());
}
......@@ -547,7 +547,7 @@ public class SheetDisplay extends Composite {
}
}
private void showMessageBox(String title, Exception e) {
private void showMessageBox(String title, Throwable e) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw, true);
pw.println(e.getMessage());
......
package org.genesys2.anno.gui;
import java.io.File;
import java.net.MalformedURLException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.apache.log4j.Logger;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell;
......@@ -12,6 +15,7 @@ import org.genesys2.anno.model.Settings;
import org.genesys2.anno.parser.CsvDataSourceParser;
import org.genesys2.anno.parser.XlsxDataSourceParser;
import org.genesys2.anno.reader.JDBCRowReader;
import org.genesys2.anno.util.ConnectionUtils;
import org.genesys2.client.oauth.GenesysClient;
import org.genesys2.client.oauth.api.GenesysApi;
import org.springframework.beans.factory.annotation.Value;
......@@ -33,6 +37,7 @@ import org.springframework.core.io.Resource;
@EnableAspectJAutoProxy
@ComponentScan(basePackages = { "org.genesys2.anno.gui" })
public class SpringConfig {
private static final Logger _log = Logger.getLogger(SpringConfig.class);
@Value("${genesys.magic.workspace}")
private String workspacePath;
......@@ -102,6 +107,45 @@ public class SpringConfig {
return new DatabaseDialog(shell, treeViewer, style);
}
@Bean
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
public ConnectionUtils connectionUtils() {
System.err.println("Loading connectionUtils");
return new ConnectionUtils();
}
/**
* Extra classLoader for JDBC drivers
*
* @return
*/
@Bean
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
public ExtraClassLoader jdbcDriverClassLoader() {
_log.info("Updating classloader.");
ClassLoader currentThreadClassLoader = Thread.currentThread().getContextClassLoader();
ExtraClassLoader extraClassLoader = new ExtraClassLoader(currentThreadClassLoader);
File baseDir = new File(workspacePath, "jdbc");
if (baseDir.exists()) {
try {
extraClassLoader.addJars(baseDir);
} catch (MalformedURLException e) {
_log.warn(e, e);
}
} else {
try {
baseDir.mkdir();
_log.info("Extensions directory created at " + baseDir.getAbsolutePath());
} catch (Throwable e) {
_log.error("Could not create extensions directory: " + e, e);
}
}
return extraClassLoader;
}
@Bean
public static PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() {
final PropertyPlaceholderConfigurer propertyPlaceholderConfigurer = new PropertyPlaceholderConfigurer();
......
......@@ -21,7 +21,11 @@ public class JdbcRowReader implements RowReader {
public JdbcRowReader(String query, String url, String user, String password) {
this.query = query;
this.conn = ConnectionUtils.getConnection(url, user, password);
try {
this.conn = ConnectionUtils.getConnection(url, user, password);
} catch (ClassNotFoundException e) {
} catch (SQLException e) {
}
this.preparedStatement = null;
}
......@@ -49,10 +53,10 @@ public class JdbcRowReader implements RowReader {
}
return rows;
} catch (SQLException e) {
} catch (Throwable e) {
try {
this.preparedStatement.close();
} catch (SQLException e1) {
} catch (Throwable e1) {
// NOOP
}
ConnectionUtils.close(this.conn);
......
......@@ -16,7 +16,7 @@ import org.genesys2.anno.util.ConnectionUtils;
public class JDBCRowReader {
private static final Logger _log = Logger.getLogger(JDBCRowReader.class);
public List<Object[]> getRows(String query, String url, String user, String password, int limit) throws SQLException {
public List<Object[]> getRows(String query, String url, String user, String password, int limit) throws SQLException, ClassNotFoundException {
List<Object[]> rows = new ArrayList<Object[]>();
......
package org.genesys2.anno.util;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.DriverPropertyInfo;
import java.sql.SQLException;
import java.util.Properties;
import org.genesys2.anno.gui.ExtraClassLoader;
import org.springframework.beans.factory.annotation.Autowired;
public class ConnectionUtils {
public static Connection getConnection(String url, String user, String password) {
private static ConnectionUtils _instance;
@Autowired
ExtraClassLoader jdbcDriverClassLoader;
public ConnectionUtils() {
_instance = this;
}
public static Connection getConnection(String url, String user, String password) throws ClassNotFoundException, SQLException {
Connection con = null;
try {
String driver = "com.mysql.jdbc.Driver";
Class.forName(driver);
Driver d = (Driver) Class.forName(driver, true, _instance.jdbcDriverClassLoader).newInstance();
DriverManager.registerDriver(new DriverShim(d));
con = DriverManager.getConnection(url, user, password);
} catch (ClassNotFoundException cnfe) {
System.out.println(cnfe);
throw cnfe;
} catch (SQLException sqe) {
System.out.println(sqe);
throw sqe;
} catch (InstantiationException e) {
throw new ClassNotFoundException(e.getMessage(), e);
} catch (IllegalAccessException e) {
throw new ClassNotFoundException(e.getMessage(), e);
}
return con;
......@@ -31,4 +52,43 @@ public class ConnectionUtils {
}
}
private static class DriverShim implements Driver {
private Driver driver;
public DriverShim(Driver d) {
this.driver = d;
}
@Override
public boolean acceptsURL(String url) throws SQLException {
return driver.acceptsURL(url);
}
@Override
public Connection connect(String url, Properties info) throws SQLException {
return driver.connect(url, info);
}
@Override
public int getMajorVersion() {
return driver.getMajorVersion();
}
@Override
public int getMinorVersion() {
return driver.getMinorVersion();
}
@Override
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException {
return driver.getPropertyInfo(url, info);
}
@Override
public boolean jdbcCompliant() {
return driver.jdbcCompliant();
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment