001: /*
002: $Header: /cvsroot/xorm/xorm/tools/src/org/xorm/tools/JdbcDriverFinder.java,v 1.3 2003/04/19 20:01:05 dcheckoway Exp $
003:
004: This file is part of XORM.
005:
006: XORM is free software; you can redistribute it and/or modify
007: it under the terms of the GNU General Public License as published by
008: the Free Software Foundation; either version 2 of the License, or
009: (at your option) any later version.
010:
011: XORM is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: GNU General Public License for more details.
015:
016: You should have received a copy of the GNU General Public License
017: along with Foobar; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020: package org.xorm.tools;
021:
022: import java.io.File;
023: import java.io.FileInputStream;
024: import java.io.InputStream;
025: import java.io.IOException;
026: import java.util.ArrayList;
027: import java.util.Collection;
028: import java.util.Iterator;
029: import javax.swing.JFileChooser;
030: import javax.swing.UIManager;
031: import javax.swing.filechooser.FileFilter;
032: import org.xorm.tools.jar.JarClassFinder;
033:
034: /** Utility for finding JDBC driver classes in JAR files
035: @author Dan Checkoway
036: */
037: public class JdbcDriverFinder {
038: JarClassFinder finder = new JarClassFinder();
039:
040: /** Create a JdbcDriverFinder */
041: public JdbcDriverFinder() {
042: }
043:
044: /** Find JDBC driver classes by browsing for JAR files
045: @return a Collection of matching class objects, or an empty
046: Collection if no driver classes are found
047: */
048: public Collection findJdbcDrivers() throws IOException {
049: File jarFile = chooseJarFile();
050: if (jarFile == null) {
051: return new ArrayList();
052: } else {
053: return finder.findClassesOfType(java.sql.Driver.class,
054: jarFile);
055: }
056: }
057:
058: /** Find JDBC driver classes within a JAR file
059: @param jarFileName the name/path of the JAR file to examine
060: @return a Collection of matching class objects, or an empty
061: Collection if no driver classes are found
062: */
063: public Collection findJdbcDrivers(String jarFileName)
064: throws IOException {
065: return finder.findClassesOfType(java.sql.Driver.class,
066: jarFileName);
067: }
068:
069: /** Find JDBC driver classes within a JAR file
070: @param jarFile the JAR file to examine
071: @return a Collection of matching class objects, or an empty
072: Collection if no driver classes are found
073: */
074: public Collection findJdbcDrivers(File jarFile) throws IOException {
075: return finder.findClassesOfType(java.sql.Driver.class, jarFile);
076: }
077:
078: /** Find JDBC driver classes within a JAR file
079: @param inputStream an input stream with the JAR content to examine
080: @return a Collection of matching class objects, or an empty
081: Collection if no driver classes are found
082: */
083: public Collection findJdbcDrivers(InputStream inputStream)
084: throws IOException {
085: return finder.findClassesOfType(java.sql.Driver.class,
086: inputStream);
087: }
088:
089: /** Test stub */
090: public static void main(String[] args) {
091: int exitCode = 0;
092: try {
093: // I *hate* the stock swing look and feel...use native if able
094: String nativeLNF = UIManager
095: .getSystemLookAndFeelClassName();
096: if (nativeLNF != null) {
097: UIManager.setLookAndFeel(nativeLNF);
098: }
099:
100: if (args.length <= 0) {
101: File jarFile = chooseJarFile();
102: if (jarFile != null) {
103: displayJdbcDriversInFile(jarFile);
104: }
105: } else {
106: for (int k = 0; k < args.length; ++k) {
107: displayJdbcDriversInFile(new File(args[k]));
108: }
109: }
110: } catch (Throwable t) {
111: t.printStackTrace(System.out);
112: exitCode = 1;
113: } finally {
114: Runtime.getRuntime().exit(exitCode);
115: }
116: }
117:
118: public static File chooseJarFile() {
119: return chooseJarFile(".");
120: }
121:
122: public static File chooseJarFile(String startingDir) {
123: JFileChooser fc = new JFileChooser(startingDir);
124: fc.setFileFilter(new FileFilter() {
125: public boolean accept(File file) {
126: return file.isDirectory()
127: || file.getName().endsWith(".jar")
128: || file.getName().endsWith(".zip");
129: }
130:
131: public String getDescription() {
132: return "JAR Files";
133: }
134: });
135: fc.setFileSelectionMode(fc.FILES_ONLY);
136: fc.setDialogTitle("Please Select Your JDBC Driver JAR File");
137: if (fc.showOpenDialog(null) == fc.APPROVE_OPTION) {
138: return fc.getSelectedFile();
139: } else {
140: return null;
141: }
142: }
143:
144: public static void displayJdbcDriversInFile(File jarFile)
145: throws IOException {
146: System.out.println("Looking for drivers in: "
147: + jarFile.getCanonicalPath());
148: JdbcDriverFinder jdf = new JdbcDriverFinder();
149: Collection classes = jdf.findJdbcDrivers(jarFile);
150: if (classes.isEmpty()) {
151: System.out.println("No matching driver classes found.");
152: } else {
153: System.out.println("Driver classes found:");
154: for (Iterator iter = classes.iterator(); iter.hasNext();) {
155: Class clazz = (Class) iter.next();
156: System.out.println(clazz.getName());
157: }
158: }
159: }
160: }
|