View Javadoc

1   /*
2    * Copyright (c) 2004 International Decision Systems, Inc.  All Rights Reserved.
3    *
4    * By using this Software, You acknowledge that the Software is a valuable asset
5    * and trade secret of either International Decision Systems, Inc. ("IDSI") or a
6    * third party supplier of IDSI and constitutes confidential and proprietary
7    * information.
8    *
9    * NEITHER IDSI NOR ANY AGENT OR PERSON ACTING FOR OR WITH IDSI HAS MADE OR DOES
10   * MAKE ANY STATEMENTS, AFFIRMATIONS, REPRESENTATIONS OR WARRANTIES WHATSOEVER
11   * TO YOU, WHETHER EXPRESS OR IMPLIED, AS TO THE SOFTWARE, THE QUALITY OR
12   * CONDITION OF THE SOFTWARE, OR THE OPERATING CHARACTERISTICS OR RELIABILITY OF
13   * THE SOFTWARE, OR ITS SUITABILITY FOR ANY GENERAL OR PARTICULAR PURPOSE, OR AS
14   * TO ANY OTHER MATTER WHATSOEVER; ANY AND ALL OTHER WARRANTIES INCLUDING
15   * WITHOUT LIMITATION ANY WARRANTIES IMPLIED BY LAW, SUCH AS THE IMPLIED
16   * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND TITLE,
17   * USE AND NON-INFRINGEMENT; ARE HEREBY EXPRESSLY DISCLAIMED AND EXCLUDED.
18  */
19  package net.sourceforge.addam.ddlrun;
20  
21  import net.sourceforge.addam.ddlrun.custom.IDSCompletedScriptRetriever;
22  import net.sourceforge.addam.ddlrun.custom.IDSRunLogger;
23  import net.sourceforge.addam.ddlrun.custom.IDSStartFolderRetriever;
24  import net.sourceforge.addam.ddlrun.grammars.ScriptGrammar;
25  import net.sourceforge.addam.ddlrun.grammars.ScriptGrammarManager;
26  import net.sourceforge.addam.ddlrun.runners.DeploymentRunner;
27  import net.sourceforge.addam.ddlrun.runners.DeploymentRunnerFactory;
28  import net.sourceforge.addam.ddlrun.utils.FileSystemReaderFactory;
29  import net.sourceforge.addam.ddlrun.utils.ResourceReaderFactory;
30  import net.sourceforge.addam.ddlrun.utils.RunLogger;
31  
32  import org.apache.tools.ant.BuildException;
33  import org.apache.tools.ant.taskdefs.JDBCTask;
34  
35  import java.io.File;
36  import java.sql.Connection;
37  
38  /**
39   * Implementation of the ant deployment task.
40   * This is NOT generic - it assumes specific database structure for tracking the scripts already run.
41   */
42  public class UpgradeTask extends JDBCTask implements RunLogger {
43  
44      public UpgradeTask() {
45      }
46  
47      public void setScript(File script) {
48          this.script = script;
49      }
50  
51      public void setVersion(String version) {
52          this.dbversion = version;
53      }
54  
55      public void execute() throws BuildException {
56          Connection connection = getConnection();
57          ResourceReaderFactory factory = new FileSystemReaderFactory(script.getParentFile());
58          ScriptGrammar grammar = new ScriptGrammarManager().getGrammar(connection);
59          if (grammar == null) {
60              throw new BuildException("" + connection + " does not use a supported database driver");
61          }
62          try {
63              DeploymentRunner runner = DeploymentRunnerFactory.getInstance().getUpgradeRunner(factory,
64                      connection,
65                      grammar,
66                      new IDSStartFolderRetriever(connection),
67                      new IDSCompletedScriptRetriever(connection));
68              runner.addRunLogger(new IDSRunLogger(connection, dbversion));
69              runner.addRunLogger(this);
70              runner.run(script.getName());
71          } catch (Exception e) {
72              throw new BuildException(e);
73          }
74      }
75  
76      public void logRunScript(String script, long time) throws Exception {
77          this.log("executed " + script + " (" + time + "ms)");
78      }
79  
80      public void logRunComplete(String group, long time) throws Exception {
81          this.log("completed execution of available scripts in " + group + " (" + time + "ms)");
82      }
83  
84      public void logRunFailure(String group, String script, Exception e) throws Exception {
85          this.log(group + "/" + script + " failed!");
86      }
87  
88      private File script = null;
89      private String dbversion = null;
90  }