001: /********************************************************************************
002: * CruiseControl, a Continuous Integration Toolkit
003: * Copyright (c) 2001, ThoughtWorks, Inc.
004: * 200 E. Randolph, 25th Floor
005: * Chicago, IL 60601 USA
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * + Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * + Redistributions in binary form must reproduce the above
016: * copyright notice, this list of conditions and the following
017: * disclaimer in the documentation and/or other materials provided
018: * with the distribution.
019: *
020: * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
021: * names of its contributors may be used to endorse or promote
022: * products derived from this software without specific prior
023: * written permission.
024: *
025: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
026: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
027: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
028: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
029: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
030: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
031: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
032: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
033: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
034: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
035: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
036: ********************************************************************************/package net.sourceforge.cruisecontrol.sourcecontrols;
037:
038: import java.io.IOException;
039: import java.io.InputStream;
040: import java.util.Iterator;
041: import java.util.LinkedList;
042: import java.util.List;
043: import net.sourceforge.cruisecontrol.CruiseControlException;
044: import net.sourceforge.cruisecontrol.sourcecontrols.accurev.AccurevInputParser;
045: import net.sourceforge.cruisecontrol.sourcecontrols.accurev.Runner;
046: import org.apache.log4j.Logger;
047:
048: class Script {
049: private String path;
050: private int returnCode;
051:
052: public Script(String path, int returnCode) {
053: this .path = path;
054: this .returnCode = returnCode;
055: }
056:
057: public String getPath() {
058: return path;
059: }
060:
061: public void setPath(String path) {
062: this .path = path;
063: }
064:
065: public int getReturnCode() {
066: return returnCode;
067: }
068:
069: public void setReturnCode(int returnCode) {
070: this .returnCode = returnCode;
071: }
072: }
073:
074: public class AccurevMockRunner implements Runner {
075: public static final Logger LOG = Logger
076: .getLogger(AccurevMockRunner.class);
077: private int returnCode;
078: private List scriptList;
079: private Iterator scriptIterator;
080: private String scriptRoot;
081:
082: public AccurevMockRunner() {
083: scriptList = new LinkedList();
084: }
085:
086: public void setScriptRoot(String scriptRoot) {
087: this .scriptRoot = scriptRoot;
088: }
089:
090: public void addScript(String path, int rCode) {
091: if (scriptRoot != null) {
092: path = scriptRoot + "/" + path;
093: }
094: Script script = new Script(path, rCode);
095: scriptList.add(script);
096: }
097:
098: private Script nextScript() {
099: if (scriptList.size() == 0) {
100: return null;
101: }
102: if (scriptIterator == null || !scriptIterator.hasNext()) {
103: scriptIterator = scriptList.iterator();
104: }
105: return (Script) scriptIterator.next();
106: }
107:
108: public boolean execute(AccurevInputParser inputParser) {
109: boolean syntaxError = false;
110: try {
111: Script script = nextScript();
112: if (script == null) {
113: throw new IOException("Script list empty");
114: }
115: LOG.info("Scripted execution, reading input from resource "
116: + script.getPath());
117: InputStream fakeFile = getClass().getClassLoader()
118: .getResourceAsStream(script.getPath());
119: if (fakeFile == null) {
120: throw new IOException("Script not found: "
121: + script.getPath());
122: }
123: try {
124: if (inputParser != null) {
125: syntaxError = !inputParser.parseStream(fakeFile);
126: }
127: this .returnCode = script.getReturnCode();
128: } finally {
129: fakeFile.close();
130: }
131: } catch (IOException e) {
132: LOG.error(e);
133: throw new RuntimeException(e.getMessage());
134: } catch (CruiseControlException e) {
135: LOG.error(e);
136: throw new RuntimeException(e.getMessage());
137: }
138: return syntaxError;
139: }
140:
141: public int getReturnCode() {
142: return returnCode;
143: }
144: }
|