KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > sourcecontrols > AccurevMockRunner


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2001, ThoughtWorks, Inc.
4  * 651 W Washington Ave. Suite 600
5  * Chicago, IL 60661 USA
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * + Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * + Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  *
20  * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
21  * names of its contributors may be used to endorse or promote
22  * products derived from this software without specific prior
23  * written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  ********************************************************************************/

37 /*
38  * Created on 04-Aug-2005 by norru
39  *
40  * Copyright (C) Sony Computer Entertainment Europe
41  * Studio Liverpool Server Group
42  *
43  * Authors:
44  * Nicola Orru' <Nicola_Orru@scee.net>
45  */

46 package net.sourceforge.cruisecontrol.sourcecontrols;
47
48 import java.io.IOException JavaDoc;
49 import java.io.InputStream JavaDoc;
50 import java.util.Iterator JavaDoc;
51 import java.util.LinkedList JavaDoc;
52 import java.util.List JavaDoc;
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 JavaDoc path;
60   private int returnCode;
61   public Script(String JavaDoc path, int returnCode) {
62     this.path = path;
63     this.returnCode = returnCode;
64   }
65   public String JavaDoc getPath() {
66     return path;
67   }
68   public void setPath(String JavaDoc 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 JavaDoc scriptList;
83   private Iterator JavaDoc scriptIterator;
84   private String JavaDoc scriptRoot;
85   public AccurevMockRunner() {
86     scriptList = new LinkedList JavaDoc();
87   }
88   public void setScriptRoot(String JavaDoc scriptRoot) {
89     this.scriptRoot = scriptRoot;
90   }
91   public void addScript(String JavaDoc 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 JavaDoc("Script list empty"); }
110       LOG.info("Scripted execution, reading input from resource " + script.getPath());
111       InputStream JavaDoc fakeFile = getClass().getClassLoader().getResourceAsStream(script.getPath());
112       if (fakeFile == null) { throw new IOException JavaDoc("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 JavaDoc e) {
122       LOG.error(e);
123       throw new RuntimeException JavaDoc(e.getMessage());
124     } catch (CruiseControlException e) {
125       LOG.error(e);
126       throw new RuntimeException JavaDoc(e.getMessage());
127     }
128     return syntaxError;
129   }
130   public int getReturnCode() {
131     return returnCode;
132   }
133 }
134
Popular Tags