001: /* ====================================================================
002: * Copyright (c) 1998 - 2003 David F. Glasser. All rights
003: * reserved.
004: *
005: * This file is part of the QueryForm Database Tool.
006: *
007: * The QueryForm Database Tool is free software; you can redistribute it
008: * and/or modify it under the terms of the GNU General Public License as
009: * published by the Free Software Foundation; either version 2 of the
010: * License, or (at your option) any later version.
011: *
012: * The QueryForm Database Tool is distributed in the hope that it will
013: * be useful, but WITHOUT ANY WARRANTY; without even the implied
014: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
015: * See the GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with the QueryForm Database Tool; if not, write to:
019: *
020: * The Free Software Foundation, Inc.,
021: * 59 Temple Place, Suite 330
022: * Boston, MA 02111-1307 USA
023: *
024: * or visit http://www.gnu.org.
025: *
026: * ====================================================================
027: *
028: * This product includes software developed by the
029: * Apache Software Foundation (http://www.apache.org/).
030: *
031: * ====================================================================
032: *
033: * $Source: /cvsroot/qform/qform/src/org/glasser/qform/QForm.java,v $
034: * $Revision: 1.9 $
035: * $Author: dglasser $
036: * $Date: 2004/06/03 01:18:01 $
037: *
038: * --------------------------------------------------------------------
039: */
040: package org.glasser.qform;
041:
042: import javax.swing.*;
043: import org.glasser.util.*;
044: import org.glasser.swing.*;
045: import java.io.*;
046: import java.util.*;
047: import org.apache.commons.dbcp.DriverManagerConnectionFactory;
048: import org.apache.commons.dbcp.PoolableConnectionFactory;
049: import org.apache.commons.dbcp.PoolingDataSource;
050: import org.apache.commons.pool.impl.GenericObjectPool;
051: import java.net.*;
052: import java.awt.*;
053:
054: public class QForm {
055:
056: private final static boolean debug = System
057: .getProperty("QForm.debug") != null;
058:
059: private final static String WSDP = "http://java.sun.com as part of the Java Web Services Developer Pack";
060:
061: private final static String COMMONS = "http://jakarta.apache.org as part of the Commons project";
062:
063: private final static String[][] checkClasses = {
064: { "javax.sql.DataSource", "javax.sql",
065: "jdbc2_0-stdext.jar", WSDP, "1.4.0" },
066: { "javax.xml.parsers.DocumentBuilderFactory", "javax.xml",
067: "jaxp-api.jar", WSDP, "1.4.0" },
068: { "org.w3c.dom.Node", "org.w3c.dom", "dom.jar", WSDP,
069: "1.4.0" },
070: { "org.xml.sax.SAXException", "org.xml.sax", "sax.jar",
071: WSDP, "1.4.0" },
072: { "org.apache.commons.dbcp.DriverManagerConnectionFactory",
073: "org.apache.commons.dbcp", "commons-dbcp.jar",
074: COMMONS },
075: { "org.apache.commons.pool.impl.GenericObjectPool",
076: "org.apache.commons.pool", "commons-pool.jar",
077: COMMONS },
078: { "org.apache.commons.collections.Bag",
079: "org.apache.commons.collections",
080: "commons-collections.jar", COMMONS }
081:
082: };
083:
084: private static File getConfigFile(String path) {
085: File f = new File(path);
086: if (f.exists())
087: return f;
088: else
089: return null;
090: }
091:
092: public static void main(String[] args) throws Exception {
093:
094: // this will be the default title for message boxes when one isn't supplied
095: GUIHelper.defaultMessageTitle = "QueryForm";
096:
097: // make sure we're running at least JRE 1.3
098: if (!Util.isCurrentJavaVersionAtLeast("1.3")) {
099: String version = System.getProperty("java.version");
100: GUIHelper
101: .errMsg(
102: null,
103: "This application requires Java 1.3 or later. You are currently running version "
104: + version + ".", null);
105: System.exit(1);
106: }
107:
108: // look in advance for some of the 3rd-party library classes we're going to need, so we
109: // can handle it gracefully if they're not there.
110: for (int j = 0; j < checkClasses.length; j++) {
111: try {
112: Class.forName(checkClasses[j][0]);
113: } catch (ClassNotFoundException ex) {
114: String msg = "Classes from the "
115: + checkClasses[j][1]
116: + " package were not found in your classpath. "
117: + "These classes are available in the jar file \""
118: + checkClasses[j][2]
119: + "\", which is available " + "from "
120: + checkClasses[j][3] + ".";
121: if (checkClasses[j].length > 4) {
122: msg = msg
123: + "\n\nThis package is also a standard part of the Java Runtime Environment "
124: + checkClasses[j][4]
125: + ". You are currently running version "
126: + System.getProperty("java.version") + ".";
127: }
128:
129: GUIHelper.errMsg(null, msg, "Required Classes Missing");
130: System.exit(2);
131: }
132: }
133:
134: try {
135: // if the program was loaded from a jar file,
136: // add the drivers subdirectory (just below the directory
137: // that contains the program jar file) to the classpath
138: // of the ExtensionClassLoader singleton. That is the classloader
139: // through which driver and look-and-feel classes are loaded.
140: ClassLoader cl = QForm.class.getClassLoader();
141: URL url = cl.getResource("org/glasser/qform/QForm.class");
142: String path = url.getPath();
143: if (debug)
144: System.out.println("QForm.class URL is " + path);
145: int i = path.indexOf(".jar!/org/glasser/qform/QForm.class");
146: if (i > 0) {
147:
148: // truncate just after ".jar"
149: // the "decode" is needed in 1.4 but not 1.3
150: String jarpath = java.net.URLDecoder.decode(path
151: .substring(0, i + 4));
152: jarpath = new URL(jarpath).getPath();
153:
154: // if this is a Windows machine, the jarpath will look like
155: // "/C:/dir1/dir2..." with a leading slash. It works on some
156: // systems, but not on others, so remove it.
157: if (jarpath.indexOf(':') == 2)
158: jarpath = jarpath.substring(1);
159: if (debug)
160: System.out.println("Jar path is " + jarpath);
161: File jarFile = new File(jarpath);
162: File jarDir = jarFile.getParentFile();
163: File driverDir = new File(jarDir, "drivers");
164: if (driverDir.exists()) {
165: if (debug)
166: System.out.println(driverDir.getAbsolutePath()
167: + " exists.");
168: ExtensionClassLoader.getSingleton()
169: .addArchivesInDirectory(driverDir);
170: } else {
171: // make sure we translated the jar directory correctly before showing the
172: // error message.
173: if (jarDir.exists()) {
174: if (debug)
175: System.out.println(driverDir
176: .getAbsolutePath()
177: + " not found.");
178:
179: GUIHelper
180: .infoMsg(
181: null,
182: "The JDBC drivers directory, "
183: + driverDir
184: + ", was not found. This is not necessary to run the program, "
185: + "however if you create it, you can place JAR or ZIP files "
186: + "containing JDBC drivers in that directory, and they will "
187: + "be available the next time the program is started.\n\n"
188: + "The drivers directory is a directory named \"drivers\" "
189: + "which is a subdirectory of the one where the "
190: + "application jar file, \""
191: + jarFile.getName()
192: + "\" is located.",
193: "Drivers Directory Missing");
194: } else {
195: System.out
196: .println("WARNING: Invalid jar path: "
197: + jarpath);
198: }
199: }
200: }
201: } catch (Exception ex) {
202: GUIHelper
203: .errMsg(
204: null,
205: "An error occurred while reading your drivers directory:\n\n"
206: + ex.getClass().getName()
207: + "\n"
208: + ex.getMessage()
209: + "\n\n Some JDBC drivers may not be available.",
210: "Application Error");
211: }
212:
213: // if a Look and Feel was specified on the command line, attempt to set it.
214: String lafClassName = System.getProperty("qform.laf");
215: if (lafClassName != null && lafClassName.trim().length() > 0) {
216: try {
217: LookAndFeel laf = (LookAndFeel) ExtensionClassLoader
218: .getSingleton().loadClass(lafClassName)
219: .newInstance();
220: UIManager.setLookAndFeel(laf);
221: UIManager.getDefaults().put("ClassLoader",
222: laf.getClass().getClassLoader());
223: UIManager.getLookAndFeelDefaults().put("ClassLoader",
224: laf.getClass().getClassLoader());
225: System.out.println("Look and Feel set to "
226: + laf.getName());
227:
228: } catch (Exception ex) {
229: GUIHelper.errMsg(null,
230: "There was an error setting the application's look-and-feel to "
231: + lafClassName + ".\n\n"
232: + ex.getClass().getName() + "\n"
233: + Util.trimToString(ex.getMessage()),
234: "Application Error");
235: }
236: }
237:
238: // now read the configuration file.
239: try {
240: // the configuration file is determined by the following
241: // priority:
242: //
243: // 1. The command line argument
244: // 2. The qform.config system property
245: // 3. The user's home directory.
246:
247: File configFile = null;
248: String configProp = System.getProperty("qform.config");
249: String configFilePath = null;
250: if (args.length > 0) {
251: configFile = getConfigFile(args[0]);
252: if (configFile == null) {
253: GUIHelper.errMsg(null,
254: "The configuration file specified with a command line argument, \""
255: + args[0] + "\", was not found.",
256: "Application Error");
257: System.exit(1);
258: }
259: } else if (configProp != null) {
260: configFile = getConfigFile(configProp);
261: if (configFile == null) {
262: GUIHelper
263: .errMsg(
264: null,
265: "The configuration file specified with the \"qform.config\" system property, \""
266: + configProp
267: + "\", was not found.",
268: "Application Error");
269: System.exit(1);
270: }
271: } else {
272: // the default config file is called qform.xml and is located in
273: // the user's home directory. If it doesn't exist, it will be
274: // created when the program exits.
275: String userHome = System.getProperty("user.home");
276: if (userHome == null)
277: userHome = "";
278: if (!userHome.endsWith("/") && !userHome.endsWith("\\")) {
279: userHome += "/";
280: }
281: configFile = new File(userHome + "qform.xml");
282: }
283:
284: JFrame frame = new JFrame();
285: frame.setContentPane(new org.glasser.qform.MainPanel(frame,
286: configFile));
287:
288: try {
289: java.net.URL imageUrl = QForm.class
290: .getClassLoader()
291: .getResource(
292: "org/glasser/qform/images/LogoIcon32.gif");
293: frame.setIconImage(Toolkit.getDefaultToolkit()
294: .getImage(imageUrl));
295: } catch (Throwable t) {
296: t.printStackTrace();
297: }
298:
299: frame.pack();
300: frame.setTitle("QueryForm");
301:
302: GUIHelper.centerWindowOnScreen(frame);
303:
304: frame.setVisible(true);
305: } catch (Throwable ex) {
306: ex.printStackTrace();
307: String msg = "An error occurred during program startup, which was probably due to missing libraries or class files:\n\n"
308: + ex.getClass().getName()
309: + "\n\n"
310: + ex.getMessage()
311: + "\n\nPlease see the console output for more information.";
312: GUIHelper.errMsg(null, msg,
313: "Unrecoverable Application Error");
314: System.exit(13);
315: }
316:
317: }
318:
319: }
|