Coverage Report - net.sourceforge.addam.ddlrun.runners.ScriptGroupRunner
 
Classes in this File Line Coverage Branch Coverage Complexity
ScriptGroupRunner
79%
48/61
100%
8/8
2.857
 
 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  14
     public ScriptGroupRunner(ResourceReaderFactory readerFactory,
 52  14
                              Filter scriptFilter, Map scriptRunnerMap) {
 53  28
         this.readerFactory = readerFactory;
 54  28
         this.scriptFilter = scriptFilter;
 55  28
         this.scriptRunnerMap = scriptRunnerMap;
 56  28
         this.runLoggers = new ArrayList();
 57  28
     }
 58  
 
 59  
     public void addRunLogger(RunLogger logger) {
 60  24
         runLoggers.add(logger);
 61  24
     }
 62  
 
 63  
     private void logRunComplete(String group, long time) throws Exception {
 64  48
         for (Iterator it = runLoggers.iterator(); it.hasNext();) {
 65  24
             RunLogger logger = (RunLogger) it.next();
 66  24
             logger.logRunComplete(group, time);
 67  12
         }
 68  24
     }
 69  
 
 70  
     private void logRunScript(String script, long time) throws Exception {
 71  96
         for (Iterator it = runLoggers.iterator(); it.hasNext();) {
 72  48
             RunLogger logger = (RunLogger) it.next();
 73  48
             logger.logRunScript(script, time);
 74  24
         }
 75  48
     }
 76  
 
 77  
     private void logRunFailure(String group, String script, Exception e) throws Exception {
 78  6
         for (Iterator it = runLoggers.iterator(); it.hasNext();) {
 79  0
             RunLogger logger = (RunLogger) it.next();
 80  0
             logger.logRunFailure(group, script, e);
 81  0
         }
 82  4
     }
 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  28
         ResourceReader reader = readerFactory.getReader(group + SEPARATOR_CHAR + MASTER_DRIVER);
 90  28
         List allScripts = new ArrayList();
 91  88
         for (String line = reader.readLine(); line != null; line = reader.readLine()) {
 92  60
             allScripts.add(line);
 93  
         }
 94  28
         Date groupStart = new Date();
 95  66
         for (Iterator it = scriptFilter.getIterator(allScripts); it.hasNext();) {
 96  52
             String script = it.next().toString();
 97  52
             int i = script.lastIndexOf(".");
 98  52
             if (i >= 0) {
 99  52
                 String type = getExtension(script);
 100  52
                 Runner scriptRunner = (Runner) scriptRunnerMap.get(type);
 101  52
                 if (scriptRunner == null) {
 102  4
                     Exception e = new RuntimeException("unknown type " + type + " for script " + script);
 103  4
                     logRunFailure(group, script, e);
 104  4
                     throw e;
 105  
                 }
 106  
                 try {
 107  
                     // make sure to include the group so the script can be located
 108  48
                     Date scriptStart = new Date();
 109  48
                     scriptRunner.run(group + SEPARATOR_CHAR + script);
 110  48
                     Date scriptStop = new Date();
 111  48
                     logRunScript(script, scriptStop.getTime() - scriptStart.getTime());
 112  0
                 } catch (SQLException e) {
 113  0
                     logRunFailure(group, script, e);
 114  0
                     e.printStackTrace();
 115  0
                     throw e;
 116  0
                 } catch (Exception e) {
 117  0
                     logRunFailure(group, script, e);
 118  0
                     throw e;
 119  24
                 }
 120  24
             } else {
 121  0
                 Exception e = new RuntimeException("no type found for script " + script);
 122  0
                 logRunFailure(group, script, e);
 123  0
                 throw e;
 124  
             }
 125  24
         }
 126  24
         Date groupStop = new Date();
 127  24
         logRunComplete(group, groupStop.getTime() - groupStart.getTime());
 128  24
     }
 129  
 
 130  
     // nonprivate to facilitate testing
 131  
     static String getExtension(String resource) {
 132  56
         String[] parts = resource.split("\\.");
 133  56
         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  
 }