001: package org.dbbrowser.drivermanager;
002:
003: import infrastructure.logging.Log;
004: import java.io.File;
005: import java.io.IOException;
006: import java.net.URL;
007: import java.net.URLClassLoader;
008: import java.sql.Driver;
009: import java.util.ArrayList;
010: import java.util.Enumeration;
011: import java.util.List;
012: import java.util.jar.JarEntry;
013: import java.util.jar.JarFile;
014: import javax.swing.event.ChangeEvent;
015: import javax.swing.event.ChangeListener;
016:
017: /**
018: * A utility class used to search for JDBC driver classes in a jar file.
019: * JDBC driver classes are classes which implement the 'java.sql.Driver' interface. This search utility extends Thread and can
020: * run in a seperate thread while informing the change listeners of the progress.
021: */
022: public class JDBCDriverSearchUtility extends Thread {
023: private File jdbcDriverJarFileLocation = null;
024: private ChangeListener changeListener = null;
025: private ChangeListener processCompletedListener = null;
026: private List listOfDrivers = new ArrayList();;
027: private IOException exc = null;
028:
029: /**
030: * Constructer
031: * @param jdbcDriverJarFileLocation
032: * @param changeListener
033: * @param processCompletedListener
034: */
035: public JDBCDriverSearchUtility(File jdbcDriverJarFileLocation,
036: ChangeListener changeListener,
037: ChangeListener processCompletedListener) {
038: this .jdbcDriverJarFileLocation = jdbcDriverJarFileLocation;
039: this .changeListener = changeListener;
040: this .processCompletedListener = processCompletedListener;
041: }
042:
043: /**
044: * Run this method in a seperate thread. This method: <br/>
045: * <ol>
046: * <li>Informs the changeListener when it starts and during the progress of the search</li>
047: * <li>Searches the jar file for classes which implement the java.sql.Driver interface using 'Driver.class.isAssignableFrom(aClass)' statement</li>
048: * <li>Informs the changeListener when it is finished</li>
049: * </ol>
050: */
051: public void run() {
052: try {
053: URL jdbcDriverURL = jdbcDriverJarFileLocation.toURL();
054: URL[] urls = new URL[1];
055: urls[0] = jdbcDriverURL;
056: URLClassLoader urlclassLoader = new URLClassLoader(urls);
057: int countOfFilesProcessed = 0;
058:
059: //List all the classes from the jar file
060: JarFile jarJDBCDriverFile = new JarFile(
061: jdbcDriverJarFileLocation);
062: Enumeration filesInJarfile = jarJDBCDriverFile.entries();
063: while (filesInJarfile.hasMoreElements()) {
064: Object o = filesInJarfile.nextElement();
065: JarEntry jarEntry = (JarEntry) o;
066:
067: //Raise an event so listener knows of the change
068: if (changeListener != null) {
069: ChangeEvent event = new ChangeEvent(new Integer(
070: countOfFilesProcessed));
071: changeListener.stateChanged(event);
072: countOfFilesProcessed++;
073: }
074:
075: try {
076: String jarEntryName = jarEntry.getName();
077: Log.getInstance().debugMessage(
078: "Jar entry name is: " + jarEntryName,
079: DBBrowserDriverManager.class.getName());
080: int g = jarEntryName.lastIndexOf(".class");
081:
082: if (g != -1) {
083: jarEntryName = jarEntryName.substring(0, g);
084: jarEntryName = jarEntryName.replace('/', '.');
085: try {
086: Class c = Class.forName(jarEntryName,
087: false, urlclassLoader);
088: Log.getInstance().debugMessage(
089: "Class is: " + c.toString(),
090: DBBrowserDriverManager.class
091: .getName());
092:
093: if (Driver.class.isAssignableFrom(c)) {
094: Log.getInstance().infoMessage(
095: "Driver found, class is "
096: + c.toString(),
097: DBBrowserDriverManager.class
098: .getName());
099: listOfDrivers.add(jarEntryName);
100: }
101: } catch (NoClassDefFoundError exc) {
102: Log.getInstance().debugMessage(
103: "Class " + jarEntryName
104: + "not tested",
105: DBBrowserDriverManager.class
106: .getName());
107: }
108: }
109: } catch (ClassNotFoundException exc) {
110: Log.getInstance().debugMessage(
111: exc.getClass().getName() + " - "
112: + exc.getMessage(),
113: DBBrowserDriverManager.class.getName());
114: }
115: }
116:
117: jarJDBCDriverFile.close();
118: } catch (IOException exc) {
119: this .exc = exc;
120: }
121:
122: ChangeEvent event = new ChangeEvent("Process completed");
123: processCompletedListener.stateChanged(event);
124: }
125:
126: /**
127: * Call this method when the process is completed to get the list of JDBC driver classes
128: * @return - a list of Strings
129: * @throws IOException
130: */
131: public List getListOfJDBCDriverClasses() throws IOException {
132: if (exc != null) {
133: throw exc;
134: }
135: return this.listOfDrivers;
136: }
137: }
|