View Javadoc

1   //
2   // Copyright (c) 2004, International Decision Systems
3   // all rights reserved
4   /*
5    * Copyright (c) 2004 International Decision Systems, Inc.  All Rights Reserved.
6    *
7    * By using this Software, You acknowledge that the Software is a valuable asset
8    * and trade secret of either International Decision Systems, Inc. ("IDSI") or a
9    * third party supplier of IDSI and constitutes confidential and proprietary
10   * information.
11   *
12   * NEITHER IDSI NOR ANY AGENT OR PERSON ACTING FOR OR WITH IDSI HAS MADE OR DOES
13   * MAKE ANY STATEMENTS, AFFIRMATIONS, REPRESENTATIONS OR WARRANTIES WHATSOEVER
14   * TO YOU, WHETHER EXPRESS OR IMPLIED, AS TO THE SOFTWARE, THE QUALITY OR
15   * CONDITION OF THE SOFTWARE, OR THE OPERATING CHARACTERISTICS OR RELIABILITY OF
16   * THE SOFTWARE, OR ITS SUITABILITY FOR ANY GENERAL OR PARTICULAR PURPOSE, OR AS
17   * TO ANY OTHER MATTER WHATSOEVER; ANY AND ALL OTHER WARRANTIES INCLUDING
18   * WITHOUT LIMITATION ANY WARRANTIES IMPLIED BY LAW, SUCH AS THE IMPLIED
19   * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND TITLE,
20   * USE AND NON-INFRINGEMENT; ARE HEREBY EXPRESSLY DISCLAIMED AND EXCLUDED.
21  */
22  package net.sourceforge.addam.ddlrun.runners;
23  
24  
25  import java.io.IOException;
26  import java.sql.SQLException;
27  import java.util.*;
28  
29  import net.sourceforge.addam.ddlrun.filters.Filter;
30  import net.sourceforge.addam.ddlrun.utils.ResourceReader;
31  import net.sourceforge.addam.ddlrun.utils.ResourceReaderFactory;
32  import net.sourceforge.addam.ddlrun.utils.RunLogger;
33  
34  /**
35   * Used to run a group (folder) of scripts.
36   *
37   * @author TIM3
38   * @since Mar 5, 2005
39   */
40  public class ScriptGroupRunner implements Runner {
41      private static final String MASTER_DRIVER = "master.drv";
42      private static final String SEPARATOR_CHAR = "/";
43  
44      /**
45       * Constructs a ScriptGroupRunner
46       *
47       * @param readerFactory   used to read the resources
48       * @param scriptFilter    used to determine which scripts need to run
49       * @param scriptRunnerMap registry of runners by type
50       */
51      public ScriptGroupRunner(ResourceReaderFactory readerFactory,
52                               Filter scriptFilter, Map scriptRunnerMap) {
53          this.readerFactory = readerFactory;
54          this.scriptFilter = scriptFilter;
55          this.scriptRunnerMap = scriptRunnerMap;
56          this.runLoggers = new ArrayList();
57      }
58  
59      public void addRunLogger(RunLogger logger) {
60          runLoggers.add(logger);
61      }
62  
63      private void logRunComplete(String group, long time) throws Exception {
64          for (Iterator it = runLoggers.iterator(); it.hasNext();) {
65              RunLogger logger = (RunLogger) it.next();
66              logger.logRunComplete(group, time);
67          }
68      }
69  
70      private void logRunScript(String script, long time) throws Exception {
71          for (Iterator it = runLoggers.iterator(); it.hasNext();) {
72              RunLogger logger = (RunLogger) it.next();
73              logger.logRunScript(script, time);
74          }
75      }
76  
77      private void logRunFailure(String group, String script, Exception e) throws Exception {
78          for (Iterator it = runLoggers.iterator(); it.hasNext();) {
79              RunLogger logger = (RunLogger) it.next();
80              logger.logRunFailure(group, script, e);
81          }
82      }
83  
84      /**
85       * @param group the group that contains the list of scripts to run
86       * @throws IOException
87       */
88      public void run(String group) throws Exception {
89          ResourceReader reader = readerFactory.getReader(group + SEPARATOR_CHAR + MASTER_DRIVER);
90          List allScripts = new ArrayList();
91          for (String line = reader.readLine(); line != null; line = reader.readLine()) {
92              allScripts.add(line);
93          }
94          Date groupStart = new Date();
95          for (Iterator it = scriptFilter.getIterator(allScripts); it.hasNext();) {
96              String script = it.next().toString();
97              int i = script.lastIndexOf(".");
98              if (i >= 0) {
99                  String type = getExtension(script);
100                 Runner scriptRunner = (Runner) scriptRunnerMap.get(type);
101                 if (scriptRunner == null) {
102                     Exception e = new RuntimeException("unknown type " + type + " for script " + script);
103                     logRunFailure(group, script, e);
104                     throw e;
105                 }
106                 try {
107                     // make sure to include the group so the script can be located
108                     Date scriptStart = new Date();
109                     scriptRunner.run(group + SEPARATOR_CHAR + script);
110                     Date scriptStop = new Date();
111                     logRunScript(script, scriptStop.getTime() - scriptStart.getTime());
112                 } catch (SQLException e) {
113                     logRunFailure(group, script, e);
114                     e.printStackTrace();
115                     throw e;
116                 } catch (Exception e) {
117                     logRunFailure(group, script, e);
118                     throw e;
119                 }
120             } else {
121                 Exception e = new RuntimeException("no type found for script " + script);
122                 logRunFailure(group, script, e);
123                 throw e;
124             }
125         }
126         Date groupStop = new Date();
127         logRunComplete(group, groupStop.getTime() - groupStart.getTime());
128     }
129 
130     // nonprivate to facilitate testing
131     static String getExtension(String resource) {
132         String[] parts = resource.split("\\.");
133         return (parts.length > 1) ? parts[parts.length - 1] : null;
134     }
135 
136     private final ResourceReaderFactory readerFactory;
137     private final Filter scriptFilter;
138     private final Map scriptRunnerMap;
139     private final ArrayList runLoggers;
140 }