001: /*
002: * This software is released under a licence similar to the Apache Software Licence.
003: * See org.logicalcobwebs.proxool.package.html for details.
004: * The latest version is available at http://proxool.sourceforge.net
005: */
006: package org.logicalcobwebs.dbscript;
007:
008: import org.xml.sax.ErrorHandler;
009: import org.xml.sax.SAXParseException;
010: import org.xml.sax.SAXException;
011: import org.apache.commons.logging.Log;
012: import org.apache.commons.logging.LogFactory;
013: import org.logicalcobwebs.proxool.ProxoolException;
014:
015: import javax.xml.parsers.SAXParserFactory;
016: import javax.xml.parsers.SAXParser;
017: import javax.xml.parsers.FactoryConfigurationError;
018: import javax.xml.parsers.ParserConfigurationException;
019: import java.io.File;
020: import java.io.IOException;
021: import java.sql.SQLException;
022:
023: /**
024: * Allows you to run scripts from file.
025: *
026: * @version $Revision: 1.12 $, $Date: 2006/01/18 14:40:05 $
027: * @author Bill Horsman (bill@logicalcobwebs.co.uk)
028: * @author $Author: billhorsman $ (current maintainer)
029: * @since Proxool 0.5
030: */
031: public class ScriptFacade {
032:
033: private static final Log LOG = LogFactory
034: .getLog(ScriptFacade.class);
035:
036: /**
037: * Run the script using the appropriate handler
038: * @param scriptLocation the path to the file that contains the script XML
039: * @param adapter so we know where to get {@link java.sql.Connection connections} from.
040: */
041: public static void runScript(String scriptLocation,
042: ConnectionAdapterIF adapter) {
043: runScript(scriptLocation, adapter, null);
044: }
045:
046: /**
047: * Run the script using the appropriate handler
048: * @param scriptLocation the path to the file that contains the script XML
049: * @param adapter so we know where to get {@link java.sql.Connection connections} from.
050: * @param commandFilter allows you to filter which commands get run and do things to the {@link java.sql.Connection}
051: */
052: public static void runScript(String scriptLocation,
053: ConnectionAdapterIF adapter, CommandFilterIF commandFilter) {
054:
055: File scriptFile = new File(scriptLocation);
056: if (!scriptFile.canRead()) {
057: throw new RuntimeException("Can't read from file at "
058: + scriptFile.getAbsolutePath());
059: }
060:
061: try {
062: SAXParserFactory saxParserFactory = SAXParserFactory
063: .newInstance();
064: saxParserFactory.setValidating(false);
065: saxParserFactory.setNamespaceAware(true);
066: SAXParser saxParser = saxParserFactory.newSAXParser();
067: saxParser.getXMLReader().setFeature(
068: "http://xml.org/sax/features/namespaces", true);
069: saxParser.getXMLReader().setErrorHandler(
070: new ErrorHandler() {
071: public void warning(SAXParseException exception)
072: throws SAXException {
073: LOG.warn(exception.getLineNumber() + ":"
074: + exception.getColumnNumber(),
075: exception);
076: }
077:
078: public void error(SAXParseException exception)
079: throws SAXException {
080: LOG.error(exception.getLineNumber() + ":"
081: + exception.getColumnNumber(),
082: exception);
083: }
084:
085: public void fatalError(
086: SAXParseException exception)
087: throws SAXException {
088: LOG.error(exception.getLineNumber() + ":"
089: + exception.getColumnNumber(),
090: exception);
091: }
092: });
093:
094: ScriptBuilder scriptBuilder = new ScriptBuilder();
095: saxParser.parse(scriptFile, scriptBuilder);
096: Script script = scriptBuilder.getScript();
097:
098: ScriptRunner.runScript(script, adapter, commandFilter);
099:
100: } catch (FactoryConfigurationError factoryConfigurationError) {
101: LOG.error(factoryConfigurationError);
102: } catch (ParserConfigurationException e) {
103: LOG.error("Problem running script " + scriptLocation, e);
104: } catch (SAXException e) {
105: LOG.error("Problem running script " + scriptLocation, e);
106: } catch (IOException e) {
107: LOG.error("Problem running script " + scriptLocation, e);
108: } catch (SQLException e) {
109: LOG.error("Problem running script " + scriptLocation, e);
110: } catch (ProxoolException e) {
111: LOG.error("Problem running script " + scriptLocation, e);
112: }
113:
114: }
115:
116: }
117:
118: /*
119: Revision history:
120: $Log: ScriptFacade.java,v $
121: Revision 1.12 2006/01/18 14:40:05 billhorsman
122: Unbundled Jakarta's Commons Logging.
123:
124: Revision 1.11 2003/03/03 11:12:03 billhorsman
125: fixed licence
126:
127: Revision 1.10 2003/02/19 15:14:21 billhorsman
128: fixed copyright (copy and paste error,
129: not copyright change)
130:
131: Revision 1.9 2003/02/06 17:41:02 billhorsman
132: now uses imported logging
133:
134: Revision 1.8 2003/01/17 00:38:12 billhorsman
135: wide ranging changes to clarify use of alias and url -
136: this has led to some signature changes (new exceptions
137: thrown) on the ProxoolFacade API.
138:
139: Revision 1.7 2002/11/13 20:23:35 billhorsman
140: change method name, throw exceptions differently, trivial changes
141:
142: Revision 1.6 2002/11/09 16:00:21 billhorsman
143: fix doc
144:
145: Revision 1.5 2002/11/07 19:08:54 billhorsman
146: Fixed up tests a bit
147:
148: Revision 1.4 2002/11/06 21:06:21 billhorsman
149: Support for CommandFilterIF
150:
151: Revision 1.3 2002/11/02 14:22:16 billhorsman
152: Documentation
153:
154: Revision 1.2 2002/11/02 13:57:34 billhorsman
155: checkstyle
156:
157: Revision 1.1 2002/11/02 11:29:53 billhorsman
158: new script runner for testing
159:
160: */
|