001: /* ====================================================================
002: * The QueryForm License, Version 1.1
003: *
004: * Copyright (c) 1998 - 2003 David F. Glasser. All rights
005: * reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The end-user documentation included with the redistribution,
020: * if any, must include the following acknowledgment:
021: * "This product includes software developed by
022: * David F. Glasser."
023: * Alternately, this acknowledgment may appear in the software itself,
024: * if and wherever such third-party acknowledgments normally appear.
025: *
026: * 4. The names "QueryForm" and "David F. Glasser" must
027: * not be used to endorse or promote products derived from this
028: * software without prior written permission. For written
029: * permission, please contact dglasser@pobox.com.
030: *
031: * 5. Products derived from this software may not be called "QueryForm",
032: * nor may "QueryForm" appear in their name, without prior written
033: * permission of David F. Glasser.
034: *
035: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL DAVID F. GLASSER, THE APACHE SOFTWARE
039: * FOUNDATION OR ITS CONTRIBUTORS, OR ANY AUTHORS OR DISTRIBUTORS
040: * OF THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: * ====================================================================
049: *
050: * This product includes software developed by the
051: * Apache Software Foundation (http://www.apache.org/).
052: *
053: * ====================================================================
054: *
055: * $Source: /cvsroot/qform/qform/src/org/glasser/qform/Config.java,v $
056: * $Revision: 1.5 $
057: * $Author: dglasser $
058: * $Date: 2003/07/24 01:53:12 $
059: *
060: * --------------------------------------------------------------------
061: */
062: package org.glasser.qform;
063:
064: import javax.xml.parsers.*;
065: import org.w3c.dom.*;
066: import org.xml.sax.*;
067: import java.util.*;
068: import java.io.*;
069: import javax.xml.transform.*;
070: import javax.xml.transform.dom.*;
071: import javax.xml.transform.stream.*;
072: import java.sql.*;
073: import javax.sql.*;
074: import org.glasser.sql.*;
075: import org.glasser.util.comparators.MethodComparator;
076: import javax.swing.*;
077:
078: public class Config {
079:
080: private final static boolean debug = System
081: .getProperty("Config.debug") != null;
082:
083: protected final static String DOCUMENT_TAG = "qform-config";
084:
085: protected final static String LOCAL_DATASOURCES_TAG = "local-datasources";
086:
087: protected final static String LOCAL_DATASOURCE_TAG = "local-datasource";
088:
089: protected final static String THIRD_PARTY_LAFS_TAG = "third-party-lafs";
090:
091: protected final static String THIRD_PARTY_LAF_TAG = "third-party-laf";
092:
093: protected final static String ENDL = "\n";
094:
095: protected final static String TAB = " ";
096:
097: protected final static String TAB2 = TAB + TAB;
098:
099: protected final static String TAB3 = TAB + TAB + TAB;
100:
101: ArrayList localDataSourceConfigs = new ArrayList();
102:
103: Hashtable thirdPartyLafs = new Hashtable();
104:
105: public void setLocalDataSourceConfigs(
106: ArrayList localDataSourceConfigs) {
107: this .localDataSourceConfigs = localDataSourceConfigs;
108: }
109:
110: public LocalDataSourceConfig[] getLocalDataSourceConfigs() {
111: return (LocalDataSourceConfig[]) localDataSourceConfigs
112: .toArray(new LocalDataSourceConfig[localDataSourceConfigs
113: .size()]);
114: }
115:
116: public void deleteLocalDataSourceConfig(LocalDataSourceConfig config) {
117: for (int j = 0; j < localDataSourceConfigs.size(); j++) {
118: if (localDataSourceConfigs.get(j) == config) {
119: localDataSourceConfigs.remove(j);
120: }
121: }
122: }
123:
124: public void addlocalDataSourceConfig(LocalDataSourceConfig config) {
125: localDataSourceConfigs.add(config);
126: }
127:
128: public void clearLocalDataSourceConfigs() {
129: localDataSourceConfigs.clear();
130: }
131:
132: /**
133: * Sorts UIManager.LookAndFeelInfo objects by their name fields.
134: */
135: private MethodComparator lafComparator = new MethodComparator(
136: UIManager.LookAndFeelInfo.class, "getName", false, false,
137: null, false);
138:
139: /**
140: * Returns all of the third-party LAF's listed in the config file.
141: */
142: public UIManager.LookAndFeelInfo[] getThirdPartyLafs() {
143:
144: UIManager.LookAndFeelInfo[] lafs = new UIManager.LookAndFeelInfo[thirdPartyLafs
145: .size()];
146: int j = 0;
147: for (Iterator i = thirdPartyLafs.values().iterator(); i
148: .hasNext();) {
149: lafs[j++] = (UIManager.LookAndFeelInfo) i.next();
150: }
151:
152: Arrays.sort(lafs, lafComparator);
153:
154: return lafs;
155:
156: }
157:
158: public void deleteThirdPartyLaf(String lafClassName) {
159: thirdPartyLafs.remove(lafClassName);
160: }
161:
162: public void addOrUpdateThirdPartyLaf(String lafName,
163: String lafClassName) {
164:
165: // get the existing one if it's there.
166: UIManager.LookAndFeelInfo info = (UIManager.LookAndFeelInfo) thirdPartyLafs
167: .get(lafClassName);
168:
169: // only create the object if we need to.
170: if (info == null || lafName.equals(info.getName()) == false) {
171: thirdPartyLafs
172: .put(lafClassName, new UIManager.LookAndFeelInfo(
173: lafName, lafClassName));
174: }
175: }
176:
177: /**
178: * If there is currently a UIManager.LookAndFeelInfo object stored for the given lafClassName,
179: * this method will update it so its name field matches lafName, otherwise it does nothing.
180: */
181: public void maybeUpdateThirdPartyLaf(String lafName,
182: String lafClassName) {
183:
184: // get the existing one if it's there.
185: UIManager.LookAndFeelInfo info = (UIManager.LookAndFeelInfo) thirdPartyLafs
186: .get(lafClassName);
187:
188: // only create the object if we need to.
189: if (info != null && lafName.equals(info.getName()) == false) {
190: thirdPartyLafs
191: .put(lafClassName, new UIManager.LookAndFeelInfo(
192: lafName, lafClassName));
193: }
194: }
195:
196: public void clearThirdPartyLafs() {
197: thirdPartyLafs.clear();
198: }
199:
200: public Config() {
201: }
202:
203: public Config(File configFile) throws SAXException, IOException,
204: ParserConfigurationException {
205: DocumentBuilder db = DocumentBuilderFactory.newInstance()
206: .newDocumentBuilder();
207: Document doc = db.parse(configFile);
208: init(doc);
209:
210: }
211:
212: public Config(InputStream is) throws SAXException, IOException,
213: ParserConfigurationException {
214: DocumentBuilder db = DocumentBuilderFactory.newInstance()
215: .newDocumentBuilder();
216: Document doc = db.parse(is);
217: init(doc);
218:
219: }
220:
221: private void init(Document doc) throws SAXException, IOException,
222: ParserConfigurationException {
223:
224: Element root = doc.getDocumentElement();
225: NodeList conns = root
226: .getElementsByTagName(LOCAL_DATASOURCES_TAG);
227: if (conns.getLength() < 1) {
228: throw new SAXException(LOCAL_DATASOURCES_TAG
229: + " element not found in configuration file.");
230: } else if (conns.getLength() > 1) {
231: throw new SAXException("More than one "
232: + LOCAL_DATASOURCES_TAG
233: + " element was found in the configuration file.");
234: }
235:
236: Element e = (Element) conns.item(0);
237:
238: conns = e.getElementsByTagName(LOCAL_DATASOURCE_TAG);
239:
240: for (int j = 0; j < conns.getLength(); j++) {
241: Element con = (Element) conns.item(j);
242: LocalDataSourceConfig config = new LocalDataSourceConfig();
243: config.setDisplayName(trimToNull(con
244: .getAttribute("display-name")));
245: config.setDriverClassName(trimToNull(con
246: .getAttribute("driver-class")));
247: config.setUrl(trimToNull(con.getAttribute("url")));
248: String s = trimToNull(con.getAttribute("login-required"));
249: if (s != null) {
250: try {
251: config.setLoginRequired(new Boolean(s)
252: .booleanValue());
253: } catch (Exception ex) {
254: ex.printStackTrace();
255: }
256: }
257: config.setUser(trimToNull(con.getAttribute("user")));
258: config
259: .setPassword(trimToNull(con
260: .getAttribute("password")));
261:
262: s = trimToNull(con.getAttribute("max-connections"));
263: if (s != null) {
264: try {
265: config.setMaxConnections(new Integer(s));
266: } catch (Exception ex) {
267: ex.printStackTrace();
268: }
269: }
270:
271: s = trimToNull(con.getAttribute("login-timeout"));
272: if (s != null) {
273: try {
274: config.setLoginTimeout(new Integer(s));
275: } catch (Exception ex) {
276: ex.printStackTrace();
277: }
278: }
279:
280: localDataSourceConfigs.add(config);
281:
282: if (debug)
283: System.out.println("Connection: " + config);
284: }
285:
286: NodeList lafs = root.getElementsByTagName(THIRD_PARTY_LAFS_TAG);
287: if (lafs != null && lafs.getLength() > 0) {
288: e = (Element) lafs.item(0);
289: lafs = e.getElementsByTagName(THIRD_PARTY_LAF_TAG);
290: for (int j = 0; j < lafs.getLength(); j++) {
291: Element laf = (Element) lafs.item(j);
292: String name = laf.getAttribute("laf-name");
293: String lafClassName = laf.getAttribute("laf-class");
294: UIManager.LookAndFeelInfo lafInfo = new UIManager.LookAndFeelInfo(
295: name, lafClassName);
296: this .thirdPartyLafs
297: .put(lafInfo.getClassName(), lafInfo);
298: }
299: }
300:
301: }
302:
303: public void writeConfig(String fileName) throws Exception {
304: writeConfig(new File(fileName));
305: }
306:
307: public void writeConfig(File file) throws Exception {
308: FileOutputStream fos = new FileOutputStream(file);
309: writeConfig(fos);
310: }
311:
312: public void writeConfig(OutputStream os) throws IOException,
313: ParserConfigurationException, TransformerException {
314:
315: DocumentBuilderFactory dbf = DocumentBuilderFactory
316: .newInstance();
317: if (debug)
318: System.out.println("DocumentBuilderFactory is "
319: + dbf.getClass().getName());
320:
321: DocumentBuilder db = dbf.newDocumentBuilder();
322:
323: if (debug)
324: System.out.println("DocumentBuilder is "
325: + db.getClass().getName());
326:
327: Document doc = db.newDocument();
328:
329: if (debug)
330: System.out.println("Document is "
331: + doc.getClass().getName());
332:
333: Element root = doc.createElement(DOCUMENT_TAG);
334:
335: doc.appendChild(root);
336:
337: root.appendChild(doc.createTextNode(ENDL + TAB));
338:
339: Element localConns = doc.createElement(LOCAL_DATASOURCES_TAG);
340:
341: root.appendChild(localConns);
342:
343: LocalDataSourceConfig[] conns = this
344: .getLocalDataSourceConfigs();
345: for (int j = 0; j < conns.length; j++) {
346: LocalDataSourceConfig config = conns[j];
347: Element con = doc.createElement(LOCAL_DATASOURCE_TAG);
348: con.setAttribute("display-name", trim(config
349: .getDisplayName()));
350: if (debug)
351: System.out.println("DISPLAY NAME = "
352: + config.getDisplayName());
353: con.setAttribute("driver-class", trim(config
354: .getDriverClassName()));
355: con.setAttribute("url", trim(config.getUrl()));
356: con.setAttribute("login-required", String.valueOf(config
357: .isLoginRequired()));
358: con.setAttribute("user", trim(config.getUser()));
359: con.setAttribute("password", trim(config.getPassword()));
360: con.setAttribute("max-connections", trim(config
361: .getMaxConnections()));
362: con.setAttribute("login-timeout", trim(config
363: .getLoginTimeout()));
364:
365: localConns.appendChild(doc.createTextNode(ENDL + TAB2));
366: localConns.appendChild(con);
367: if (j == conns.length - 1) {
368: localConns.appendChild(doc.createTextNode(ENDL + TAB));
369: }
370: }
371:
372: root.appendChild(doc.createTextNode(ENDL + TAB));
373:
374: Element lafs = doc.createElement(THIRD_PARTY_LAFS_TAG);
375: root.appendChild(lafs);
376:
377: UIManager.LookAndFeelInfo[] lafInfos = this .getThirdPartyLafs();
378: for (int j = 0; j < lafInfos.length; j++) {
379: UIManager.LookAndFeelInfo info = lafInfos[j];
380: Element lafElement = doc.createElement(THIRD_PARTY_LAF_TAG);
381: lafElement.setAttribute("laf-name", trim(info.getName()));
382: lafElement.setAttribute("laf-class", trim(info
383: .getClassName()));
384: lafs.appendChild(doc.createTextNode(ENDL + TAB2));
385: lafs.appendChild(lafElement);
386: if (j == lafInfos.length - 1) {
387: lafs.appendChild(doc.createTextNode(ENDL + TAB));
388: }
389: }
390:
391: root.appendChild(doc.createTextNode(ENDL));
392:
393: javax.xml.transform.Transformer transformer = javax.xml.transform.TransformerFactory
394: .newInstance().newTransformer();
395:
396: StreamResult sr = new StreamResult(os);
397:
398: transformer.transform(new DOMSource(doc), sr);
399:
400: OutputStream ostream = sr.getOutputStream();
401: if (ostream != os) {
402: // System.out.println("Closing StreamResult's output stream.");
403: ostream.close();
404: }
405:
406: os.close();
407:
408: }
409:
410: private static final String trimToNull(String s) {
411: if (s == null || (s = s.trim()).length() == 0)
412: return null;
413: return s;
414: }
415:
416: private static final String trim(Object o) {
417: if (o == null)
418: return "";
419: return o.toString().trim();
420: }
421:
422: public static void main(String[] args) throws Exception {
423: Config c = new Config(new File("C:/0/qform.xml"));
424: LocalDataSourceConfig[] configs = c.getLocalDataSourceConfigs();
425: for (int j = 0; j < configs.length; j++) {
426: System.out.println("----" + configs[j]);
427: DataSource ds = DataSourceManager.getLocalDataSource(
428: configs[j], null, null);
429: for (int k = 0; k < 7; k++) {
430: Connection conn = ds.getConnection();
431: System.out.println("DONE.");
432: // conn.close();
433: }
434: }
435:
436: c.writeConfig("C:/0/qf2.xml");
437: }
438:
439: }
|