KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > logicalcobwebs > dbscript > ScriptFacade


1 /*
2  * This software is released under a licence similar to the Apache Software Licence.
3  * See org.logicalcobwebs.proxool.package.html for details.
4  * The latest version is available at http://proxool.sourceforge.net
5  */

6 package org.logicalcobwebs.dbscript;
7
8 import org.xml.sax.ErrorHandler JavaDoc;
9 import org.xml.sax.SAXParseException JavaDoc;
10 import org.xml.sax.SAXException JavaDoc;
11 import org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
13 import org.logicalcobwebs.proxool.ProxoolException;
14
15 import javax.xml.parsers.SAXParserFactory JavaDoc;
16 import javax.xml.parsers.SAXParser JavaDoc;
17 import javax.xml.parsers.FactoryConfigurationError JavaDoc;
18 import javax.xml.parsers.ParserConfigurationException JavaDoc;
19 import java.io.File JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.sql.SQLException JavaDoc;
22
23 /**
24  * Allows you to run scripts from file.
25  *
26  * @version $Revision: 1.12 $, $Date: 2006/01/18 14:40:05 $
27  * @author Bill Horsman (bill@logicalcobwebs.co.uk)
28  * @author $Author: billhorsman $ (current maintainer)
29  * @since Proxool 0.5
30  */

31 public class ScriptFacade {
32
33     private static final Log LOG = LogFactory.getLog(ScriptFacade.class);
34
35     /**
36      * Run the script using the appropriate handler
37      * @param scriptLocation the path to the file that contains the script XML
38      * @param adapter so we know where to get {@link java.sql.Connection connections} from.
39      */

40     public static void runScript(String JavaDoc scriptLocation, ConnectionAdapterIF adapter) {
41         runScript(scriptLocation, adapter, null);
42     }
43
44     /**
45      * Run the script using the appropriate handler
46      * @param scriptLocation the path to the file that contains the script XML
47      * @param adapter so we know where to get {@link java.sql.Connection connections} from.
48      * @param commandFilter allows you to filter which commands get run and do things to the {@link java.sql.Connection}
49      */

50     public static void runScript(String JavaDoc scriptLocation, ConnectionAdapterIF adapter, CommandFilterIF commandFilter) {
51
52         File JavaDoc scriptFile = new File JavaDoc(scriptLocation);
53         if (!scriptFile.canRead()) {
54             throw new RuntimeException JavaDoc("Can't read from file at " + scriptFile.getAbsolutePath());
55         }
56
57         try {
58             SAXParserFactory JavaDoc saxParserFactory = SAXParserFactory.newInstance();
59             saxParserFactory.setValidating(false);
60             saxParserFactory.setNamespaceAware(true);
61             SAXParser JavaDoc saxParser = saxParserFactory.newSAXParser();
62             saxParser.getXMLReader().setFeature("http://xml.org/sax/features/namespaces", true);
63             saxParser.getXMLReader().setErrorHandler(new ErrorHandler JavaDoc() {
64                 public void warning(SAXParseException JavaDoc exception)
65                         throws SAXException JavaDoc {
66                     LOG.warn(exception.getLineNumber() + ":" + exception.getColumnNumber(), exception);
67                 }
68
69                 public void error(SAXParseException JavaDoc exception)
70                         throws SAXException JavaDoc {
71                     LOG.error(exception.getLineNumber() + ":" + exception.getColumnNumber(), exception);
72                 }
73
74                 public void fatalError(SAXParseException JavaDoc exception)
75                         throws SAXException JavaDoc {
76                     LOG.error(exception.getLineNumber() + ":" + exception.getColumnNumber(), exception);
77                 }
78             });
79
80             ScriptBuilder scriptBuilder = new ScriptBuilder();
81             saxParser.parse(scriptFile, scriptBuilder);
82             Script script = scriptBuilder.getScript();
83
84             ScriptRunner.runScript(script, adapter, commandFilter);
85
86         } catch (FactoryConfigurationError JavaDoc factoryConfigurationError) {
87             LOG.error(factoryConfigurationError);
88         } catch (ParserConfigurationException JavaDoc e) {
89             LOG.error("Problem running script " + scriptLocation, e);
90         } catch (SAXException JavaDoc e) {
91             LOG.error("Problem running script " + scriptLocation, e);
92         } catch (IOException JavaDoc e) {
93             LOG.error("Problem running script " + scriptLocation, e);
94         } catch (SQLException JavaDoc e) {
95             LOG.error("Problem running script " + scriptLocation, e);
96         } catch (ProxoolException e) {
97             LOG.error("Problem running script " + scriptLocation, e);
98         }
99
100     }
101
102 }
103
104 /*
105  Revision history:
106  $Log: ScriptFacade.java,v $
107  Revision 1.12 2006/01/18 14:40:05 billhorsman
108  Unbundled Jakarta's Commons Logging.
109
110  Revision 1.11 2003/03/03 11:12:03 billhorsman
111  fixed licence
112
113  Revision 1.10 2003/02/19 15:14:21 billhorsman
114  fixed copyright (copy and paste error,
115  not copyright change)
116
117  Revision 1.9 2003/02/06 17:41:02 billhorsman
118  now uses imported logging
119
120  Revision 1.8 2003/01/17 00:38:12 billhorsman
121  wide ranging changes to clarify use of alias and url -
122  this has led to some signature changes (new exceptions
123  thrown) on the ProxoolFacade API.
124
125  Revision 1.7 2002/11/13 20:23:35 billhorsman
126  change method name, throw exceptions differently, trivial changes
127
128  Revision 1.6 2002/11/09 16:00:21 billhorsman
129  fix doc
130
131  Revision 1.5 2002/11/07 19:08:54 billhorsman
132  Fixed up tests a bit
133
134  Revision 1.4 2002/11/06 21:06:21 billhorsman
135  Support for CommandFilterIF
136
137  Revision 1.3 2002/11/02 14:22:16 billhorsman
138  Documentation
139
140  Revision 1.2 2002/11/02 13:57:34 billhorsman
141  checkstyle
142
143  Revision 1.1 2002/11/02 11:29:53 billhorsman
144  new script runner for testing
145
146 */

147
Popular Tags