From a5bf9e5ff3094fccc9d68d7943e5c9648af004fb Mon Sep 17 00:00:00 2001 From: khartlage Date: Tue, 8 Jun 2004 19:57:30 +0000 Subject: [PATCH 1/1] synchronized from quantum plugin --- .../plugin.xml | 13 +- .../src/com/quantum/QuantumResources.properties | 8 +- .../src/com/quantum/model/Bookmark.java | 19 ++- .../src/com/quantum/model/BookmarkCollection.java | 103 +++++++++++-- .../src/com/quantum/model/JDBCDriver.java | 173 ++++++++++++++++++++ .../quantum/properties/BookmarkPropertyPage.java | 11 +- .../src/com/quantum/sql/MultiSQLServer.java | 59 +++----- .../src/com/quantum/util/JarUtil.java | 93 +++++++++++ 8 files changed, 406 insertions(+), 73 deletions(-) create mode 100644 archive/net.sourceforge.phpeclipse.quantum.sql/src/com/quantum/model/JDBCDriver.java create mode 100644 archive/net.sourceforge.phpeclipse.quantum.sql/src/com/quantum/util/JarUtil.java diff --git a/archive/net.sourceforge.phpeclipse.quantum.sql/plugin.xml b/archive/net.sourceforge.phpeclipse.quantum.sql/plugin.xml index 9d27d0d..9bddeab 100644 --- a/archive/net.sourceforge.phpeclipse.quantum.sql/plugin.xml +++ b/archive/net.sourceforge.phpeclipse.quantum.sql/plugin.xml @@ -8,19 +8,12 @@ class="com.quantum.QuantumPlugin"> - - @@ -128,7 +121,7 @@ @@ -172,12 +165,12 @@ class="com.quantum.editors.SQLEditor" id="com.quantum.editors.SQLEditor"> - - + --> 0) { importSchemas((Element) children.item(0), bookmark); } @@ -175,11 +225,10 @@ public class BookmarkCollection { importQuickList(bookmark, column); importQueryList(bookmark, column); } - this.bookmarks.addAll(newBookmarks); - this.support.firePropertyChange("bookmarks", null, null); - } + return newBookmarks; + } - private void importSchemas(Element otherSchemas, Bookmark bookmark) { + private void importSchemas(Element otherSchemas, Bookmark bookmark) { Vector vector = MetaDataXMLInterface.getVectorText(otherSchemas, Messages.getString("ExportXMLAction.SchemaName")); List list = new ArrayList(); for (Iterator i = vector.iterator(); i.hasNext();) { @@ -254,6 +303,10 @@ public class BookmarkCollection { public Bookmark[] getBookmarks() { return (Bookmark[]) this.bookmarks.toArray(new Bookmark[this.bookmarks.size()]); } + + public JDBCDriver[] getJDBCDrivers() { + return (JDBCDriver[]) this.drivers.toArray(new JDBCDriver[this.drivers.size()]); + } /** * @return */ @@ -308,5 +361,33 @@ public class BookmarkCollection { return copyName; } + /** + * @param driver + * @param driverFile + */ + public JDBCDriver findDriver(String driverClassName, String driverFile) { + JDBCDriver temp = new JDBCDriver(driverClassName, driverFile); + return findDriver(temp); + } + + /** + * @param temp + * @return + */ + public JDBCDriver findDriver(JDBCDriver temp) { + JDBCDriver result = null; + for (Iterator i = this.drivers.iterator(); result == null && i.hasNext();) { + JDBCDriver driver = (JDBCDriver) i.next(); + if (temp.equals(driver)) { + result = driver; + } + } + if (result == null) { + addDriver(temp); + result = temp; + } + return result; + } + } diff --git a/archive/net.sourceforge.phpeclipse.quantum.sql/src/com/quantum/model/JDBCDriver.java b/archive/net.sourceforge.phpeclipse.quantum.sql/src/com/quantum/model/JDBCDriver.java new file mode 100644 index 0000000..aff0f3c --- /dev/null +++ b/archive/net.sourceforge.phpeclipse.quantum.sql/src/com/quantum/model/JDBCDriver.java @@ -0,0 +1,173 @@ +package com.quantum.model; + +import java.beans.PropertyChangeListener; +import java.beans.PropertyChangeSupport; +import java.sql.Driver; + +import com.quantum.util.JarUtil; + + +/** + * @author BC + */ +public class JDBCDriver { + private String name; + private String version; + private String className; + private String jarFileName; + private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this); + /** + * @param className + * @param jarFileName + */ + public JDBCDriver(String className, String jarFileName) { + super(); + this.className = className; + this.jarFileName = jarFileName; + } + /** + * + */ + public JDBCDriver() { + } + /** + * @param className + * @param jarFileName + * @param name + * @param version + */ + public JDBCDriver(String className, String jarFileName, String name, String version) { + this.name = name; + this.version = version; + this.className = className; + this.jarFileName = jarFileName; + } + /** + * @return Returns the className. + */ + public String getClassName() { + return this.className; + } + /** + * @param className The className to set. + */ + public void setClassName(String className) { + if (className != null && !className.equals(this.className)) { + String original = this.className; + this.className = className; + this.propertyChangeSupport.firePropertyChange("className", original, className); + } + } + /** + * @return Returns the jarFileName. + */ + public String getJarFileName() { + return this.jarFileName; + } + /** + * @param jarFileName The jarFileName to set. + */ + public void setJarFileName(String jarFileName) { + if (jarFileName != null && !jarFileName.equals(this.jarFileName)) { + String original = this.jarFileName; + this.jarFileName = jarFileName; + this.propertyChangeSupport.firePropertyChange("jarFileName", original, jarFileName); + } + } + /** + * @return Returns the name. + */ + public String getName() { + return this.name == null || this.name.trim().length() == 0 ? getClassName() : this.name; + } + /** + * @param name The name to set. + */ + public void setName(String name) { + if (name != null && !name.equals(this.name)) { + String original = this.name; + this.name = name; + this.propertyChangeSupport.firePropertyChange("name", original, name); + } + } + /** + * @return Returns the version. + */ + public String getVersion() { + return this.version; + } + /** + * @param version The version to set. + */ + public void setVersion(String version) { + if (version != null && !version.equals(this.version)) { + String original = this.version; + this.version = version; + this.propertyChangeSupport.firePropertyChange("version", original, version); + } + } + + public boolean equals(Object object) { + if (super.equals(object)) { + return true; + } else if (object == null) { + return false; + } else if (getClass() != object.getClass()) { + return false; + } else { + JDBCDriver that = (JDBCDriver) object; + + if (this.className == null && that.className != null) { + return false; + } else if (this.className != null && !this.className.equals(that.className)) { + return false; + } else if (this.jarFileName == null && that.jarFileName != null) { + return false; + } else if (this.jarFileName != null && !this.jarFileName.equals(that.jarFileName)) { + return false; + } else { + return true; + } + } + } + public int hashCode() { + int hashCode = 31; + if (this.className != null) { + hashCode ^= this.className.hashCode(); + } + if (this.jarFileName != null) { + hashCode ^= this.jarFileName.hashCode(); + } + return hashCode; + } + + public Driver getDriver() { + return JarUtil.loadDriver(getJarFileName(), getClassName()); + } + /** + * @param listener + */ + public void addPropertyChangeListener(PropertyChangeListener listener) { + this.propertyChangeSupport.addPropertyChangeListener(listener); + } + /** + * @param propertyName + * @param listener + */ + public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) { + this.propertyChangeSupport.addPropertyChangeListener(propertyName, listener); + } + /** + * @param arg0 + */ + public void removePropertyChangeListener(PropertyChangeListener arg0) { + this.propertyChangeSupport.removePropertyChangeListener(arg0); + } + /** + * @param arg0 + * @param arg1 + */ + public void removePropertyChangeListener(String arg0, PropertyChangeListener arg1) { + this.propertyChangeSupport.removePropertyChangeListener(arg0, arg1); + } +} diff --git a/archive/net.sourceforge.phpeclipse.quantum.sql/src/com/quantum/properties/BookmarkPropertyPage.java b/archive/net.sourceforge.phpeclipse.quantum.sql/src/com/quantum/properties/BookmarkPropertyPage.java index 42c4065..2463376 100644 --- a/archive/net.sourceforge.phpeclipse.quantum.sql/src/com/quantum/properties/BookmarkPropertyPage.java +++ b/archive/net.sourceforge.phpeclipse.quantum.sql/src/com/quantum/properties/BookmarkPropertyPage.java @@ -4,6 +4,8 @@ import com.quantum.IQuantumConstants; import com.quantum.adapters.AdapterFactory; import com.quantum.adapters.DriverInfo; import com.quantum.model.Bookmark; +import com.quantum.model.BookmarkCollection; +import com.quantum.model.JDBCDriver; import com.quantum.view.bookmark.TreeNode; import org.eclipse.swt.SWT; @@ -240,8 +242,9 @@ public class BookmarkPropertyPage extends PropertyPage { int index = this.type.getSelectionIndex(); bookmark.setType(this.adapters[index].getDriverType()); bookmark.setConnect(this.jdbcURL.getText()); - bookmark.setDriver(this.driverName.getText()); - bookmark.setDriverFile(this.driverPath.getText()); + JDBCDriver jdbcDriver = BookmarkCollection.getInstance().findDriver( + this.driverName.getText(), this.driverPath.getText()); + bookmark.setJDBCDriver(jdbcDriver); if (this.autoCommit.getSelectionIndex() >= 0) bookmark.setAutoCommitPreference(this.autoCommit.getItem(this.autoCommit.getSelectionIndex())); return super.performOk(); @@ -277,8 +280,8 @@ public class BookmarkPropertyPage extends PropertyPage { else if (bookmark.getAutoCommitPreference().equals(IQuantumConstants.autoCommitSaved)) this.autoCommit.select(2); - this.driverName.setText(bookmark.getDriver()); + this.driverName.setText(bookmark.getJDBCDriver().getClassName()); this.jdbcURL.setText(bookmark.getConnect()); - this.driverPath.setText(bookmark.getDriverFile()); + this.driverPath.setText(bookmark.getJDBCDriver().getJarFileName()); } } diff --git a/archive/net.sourceforge.phpeclipse.quantum.sql/src/com/quantum/sql/MultiSQLServer.java b/archive/net.sourceforge.phpeclipse.quantum.sql/src/com/quantum/sql/MultiSQLServer.java index 843d57f..4946bf6 100644 --- a/archive/net.sourceforge.phpeclipse.quantum.sql/src/com/quantum/sql/MultiSQLServer.java +++ b/archive/net.sourceforge.phpeclipse.quantum.sql/src/com/quantum/sql/MultiSQLServer.java @@ -1,13 +1,9 @@ package com.quantum.sql; import java.io.ByteArrayOutputStream; -import java.io.File; import java.io.InputStream; import java.io.Reader; import java.io.UnsupportedEncodingException; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLClassLoader; import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.Driver; @@ -15,13 +11,13 @@ import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Statement; -import java.util.Hashtable; import java.util.Properties; import java.util.Vector; import com.quantum.model.Bookmark; import com.quantum.model.ConnectionException; import com.quantum.model.Entity; +import com.quantum.model.JDBCDriver; import com.quantum.model.PasswordFinder; import com.quantum.sql.metadata.MetaDataJDBCInterface; import com.quantum.sql.metadata.ObjectMetaData; @@ -39,11 +35,9 @@ public class MultiSQLServer implements ConnectionEstablisher { public static final String USERNAME = "user"; //$NON-NLS-1$ public static final String PASSWORD = "password"; //$NON-NLS-1$ private static MultiSQLServer instance = null; - private Hashtable classLoaderCache = new Hashtable(); boolean running = true; - public MultiSQLServer() { - //start(); + private MultiSQLServer() { } public synchronized static MultiSQLServer getInstance() { if (instance == null) { @@ -145,42 +139,29 @@ public class MultiSQLServer implements ConnectionEstablisher { throws ConnectionException { LogProxy log = LogProxy.getInstance(); log.addText(LogProxy.QUERY, "Connecting to: " + bookmark.getName()); //$NON-NLS-1$ - URL urls[] = new URL[1]; try { - String driverFile = bookmark.getDriverFile(); - URLClassLoader loader = - (URLClassLoader) classLoaderCache.get(driverFile); - if (loader == null) { - urls[0] = new File(driverFile).toURL(); - loader = new URLClassLoader(urls); - classLoaderCache.put(driverFile, loader); - System.out.println("Creating new classloader"); //$NON-NLS-1$ - } else { - System.out.println("Using classloader in cache"); //$NON-NLS-1$ - } - Class driverClass = loader.loadClass(bookmark.getDriver()); - Driver driver = (Driver) driverClass.newInstance(); - Properties props = new Properties(); - props.put(USERNAME, bookmark.getUsername()); - props.put(PASSWORD, password); - Connection connection = - driver.connect(bookmark.getConnect(), props); - if (connection == null) { - throw new ConnectionException("Error: Driver returned a null connection: " + bookmark.toString()); //$NON-NLS-1$ + JDBCDriver jdbcDriver = bookmark.getJDBCDriver(); + Driver driver = jdbcDriver.getDriver(); + Connection connection = null; + if (driver != null) { + Properties props = new Properties(); + props.put(USERNAME, bookmark.getUsername()); + props.put(PASSWORD, password); + connection = + driver.connect(bookmark.getConnect(), props); + if (connection == null) { + throw new ConnectionException("Error: Driver returned a null connection: " + bookmark.toString()); //$NON-NLS-1$ + } + + DatabaseMetaData metaData = connection.getMetaData(); + jdbcDriver.setName(metaData.getDriverName()); + jdbcDriver.setVersion(metaData.getDriverVersion()); + log.addText(LogProxy.RESULTS, "Connected to: " + bookmark.getName()); //$NON-NLS-1$ + System.out.println("Connected"); //$NON-NLS-1$ } - log.addText(LogProxy.RESULTS, "Connected to: " + bookmark.getName()); //$NON-NLS-1$ - System.out.println("Connected"); //$NON-NLS-1$ return connection; } catch (SQLException e) { throw new ConnectionException(e); - } catch (MalformedURLException e) { - throw new ConnectionException(e); - } catch (ClassNotFoundException e) { - throw new ConnectionException(e); - } catch (InstantiationException e) { - throw new ConnectionException(e); - } catch (IllegalAccessException e) { - throw new ConnectionException(e); } } public SQLResults execute(Connection con, String s) throws SQLException { diff --git a/archive/net.sourceforge.phpeclipse.quantum.sql/src/com/quantum/util/JarUtil.java b/archive/net.sourceforge.phpeclipse.quantum.sql/src/com/quantum/util/JarUtil.java new file mode 100644 index 0000000..c30bdad --- /dev/null +++ b/archive/net.sourceforge.phpeclipse.quantum.sql/src/com/quantum/util/JarUtil.java @@ -0,0 +1,93 @@ +package com.quantum.util; + +import java.io.File; +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLClassLoader; +import java.sql.Driver; +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.Hashtable; +import java.util.List; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; + + +/** + * @author BC Holmes + */ +public class JarUtil { + + private static Hashtable classLoaderCache = new Hashtable(); + + public static Driver loadDriver(String driverFile, String className) { + Driver result = null; + try { + File file = new File(driverFile); + if (file.exists() && file.isFile()) { + URLClassLoader loader = getURLClassLoader(file); + Class driverClass = loader.loadClass(className); + try { + result = (Driver) driverClass.newInstance(); + } catch (ClassCastException e) { + } + } + } catch (MalformedURLException e) { + } catch (ClassNotFoundException e) { + } catch (InstantiationException e) { + } catch (IllegalAccessException e) { + } + return result; + } + + public static String[] getAllDriverNames(String driverFile) { + List list = new ArrayList(); + try { + File file = new File(driverFile); + URLClassLoader loader = getURLClassLoader(file); + JarFile jar = new JarFile(file); + for (Enumeration e = jar.entries(); e.hasMoreElements(); ) { + JarEntry entry = (JarEntry) e.nextElement(); + String className = getClassNameFromFileName(entry.getName()); + if (className != null) { + try { + Class driverClass = loader.loadClass(className); + if (Driver.class.isAssignableFrom(driverClass)) { + list.add(className); + } + } catch (ClassNotFoundException ex) { + } + } + } + } catch (IOException e) { + } + return (String[]) list.toArray(new String[list.size()]); + } + + private static String getClassNameFromFileName(String name) { + String result = null; + if (name.endsWith(".class")) { + result = name.substring(0, name.length()-6).replace('/', '.').replace('\\', '.' ); + } + return result; + } + + /** + * @param file + * @return + * @throws MalformedURLException + */ + private static URLClassLoader getURLClassLoader(File file) throws MalformedURLException { + String driverFile = file.getAbsolutePath(); + URLClassLoader loader = + (URLClassLoader) classLoaderCache.get(driverFile); + if (loader == null) { + URL urls[] = new URL[1]; + urls[0] = file.toURL(); + loader = new URLClassLoader(urls); + classLoaderCache.put(driverFile, loader); + } + return loader; + } +} -- 1.7.1