1 6 package org.logicalcobwebs.dbscript; 7 8 import org.xml.sax.ErrorHandler ; 9 import org.xml.sax.SAXParseException ; 10 import org.xml.sax.SAXException ; 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 ; 16 import javax.xml.parsers.SAXParser ; 17 import javax.xml.parsers.FactoryConfigurationError ; 18 import javax.xml.parsers.ParserConfigurationException ; 19 import java.io.File ; 20 import java.io.IOException ; 21 import java.sql.SQLException ; 22 23 31 public class ScriptFacade { 32 33 private static final Log LOG = LogFactory.getLog(ScriptFacade.class); 34 35 40 public static void runScript(String scriptLocation, ConnectionAdapterIF adapter) { 41 runScript(scriptLocation, adapter, null); 42 } 43 44 50 public static void runScript(String scriptLocation, ConnectionAdapterIF adapter, CommandFilterIF commandFilter) { 51 52 File scriptFile = new File (scriptLocation); 53 if (!scriptFile.canRead()) { 54 throw new RuntimeException ("Can't read from file at " + scriptFile.getAbsolutePath()); 55 } 56 57 try { 58 SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); 59 saxParserFactory.setValidating(false); 60 saxParserFactory.setNamespaceAware(true); 61 SAXParser saxParser = saxParserFactory.newSAXParser(); 62 saxParser.getXMLReader().setFeature("http://xml.org/sax/features/namespaces", true); 63 saxParser.getXMLReader().setErrorHandler(new ErrorHandler () { 64 public void warning(SAXParseException exception) 65 throws SAXException { 66 LOG.warn(exception.getLineNumber() + ":" + exception.getColumnNumber(), exception); 67 } 68 69 public void error(SAXParseException exception) 70 throws SAXException { 71 LOG.error(exception.getLineNumber() + ":" + exception.getColumnNumber(), exception); 72 } 73 74 public void fatalError(SAXParseException exception) 75 throws SAXException { 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 factoryConfigurationError) { 87 LOG.error(factoryConfigurationError); 88 } catch (ParserConfigurationException e) { 89 LOG.error("Problem running script " + scriptLocation, e); 90 } catch (SAXException e) { 91 LOG.error("Problem running script " + scriptLocation, e); 92 } catch (IOException e) { 93 LOG.error("Problem running script " + scriptLocation, e); 94 } catch (SQLException 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 147 | Popular Tags |