1 37 46 package net.sourceforge.cruisecontrol.sourcecontrols; 47 48 import java.io.IOException ; 49 import java.io.InputStream ; 50 import java.util.Iterator ; 51 import java.util.LinkedList ; 52 import java.util.List ; 53 import net.sourceforge.cruisecontrol.CruiseControlException; 54 import net.sourceforge.cruisecontrol.sourcecontrols.accurev.AccurevInputParser; 55 import net.sourceforge.cruisecontrol.sourcecontrols.accurev.Runner; 56 import org.apache.log4j.Logger; 57 58 class Script { 59 private String path; 60 private int returnCode; 61 public Script(String path, int returnCode) { 62 this.path = path; 63 this.returnCode = returnCode; 64 } 65 public String getPath() { 66 return path; 67 } 68 public void setPath(String path) { 69 this.path = path; 70 } 71 public int getReturnCode() { 72 return returnCode; 73 } 74 public void setReturnCode(int returnCode) { 75 this.returnCode = returnCode; 76 } 77 } 78 79 public class AccurevMockRunner implements Runner { 80 public static final Logger LOG = Logger.getLogger(AccurevMockRunner.class); 81 private int returnCode; 82 private List scriptList; 83 private Iterator scriptIterator; 84 private String scriptRoot; 85 public AccurevMockRunner() { 86 scriptList = new LinkedList (); 87 } 88 public void setScriptRoot(String scriptRoot) { 89 this.scriptRoot = scriptRoot; 90 } 91 public void addScript(String path, int returnCode) { 92 if (scriptRoot != null) { 93 path = scriptRoot + "/" + path; 94 } 95 Script script = new Script(path, returnCode); 96 scriptList.add(script); 97 } 98 private Script nextScript() { 99 if (scriptList.size() == 0) { return null; } 100 if (scriptIterator == null || !scriptIterator.hasNext()) { 101 scriptIterator = scriptList.iterator(); 102 } 103 return (Script) scriptIterator.next(); 104 } 105 public boolean execute(AccurevInputParser inputParser) { 106 boolean syntaxError = false; 107 try { 108 Script script = nextScript(); 109 if (script == null) { throw new IOException ("Script list empty"); } 110 LOG.info("Scripted execution, reading input from resource " + script.getPath()); 111 InputStream fakeFile = getClass().getClassLoader().getResourceAsStream(script.getPath()); 112 if (fakeFile == null) { throw new IOException ("Script not found: " + script.getPath()); } 113 try { 114 if (inputParser != null) { 115 syntaxError = !inputParser.parseStream(fakeFile); 116 } 117 this.returnCode = script.getReturnCode(); 118 } finally { 119 fakeFile.close(); 120 } 121 } catch (IOException e) { 122 LOG.error(e); 123 throw new RuntimeException (e.getMessage()); 124 } catch (CruiseControlException e) { 125 LOG.error(e); 126 throw new RuntimeException (e.getMessage()); 127 } 128 return syntaxError; 129 } 130 public int getReturnCode() { 131 return returnCode; 132 } 133 } 134 | Popular Tags |