Debian Codegraph @

src/main/java/net/sf/antcontrib/antserver/client/ClientTask.java

raw symbols SCIP
1 /*
2 * Copyright (c) 2001-2004 Ant-Contrib project. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package net.sf.antcontrib.antserver.client;
17
18 import java.io.ByteArrayInputStream;
19 import java.io.IOException;
20 import java.util.Enumeration;
21 import java.util.Vector;
22 import javax.xml.parsers.DocumentBuilder;
23 import javax.xml.parsers.DocumentBuilderFactory;
24 import javax.xml.parsers.ParserConfigurationException;
25
26 import org.apache.tools.ant.BuildException;
27 import org.apache.tools.ant.Project;
28 import org.apache.tools.ant.Task;
29 import org.w3c.dom.Document;
30 import org.w3c.dom.Element;
31 import org.w3c.dom.NodeList;
32 import org.xml.sax.SAXException;
33
34 import net.sf.antcontrib.antserver.Command;
35 import net.sf.antcontrib.antserver.Response;
36 import net.sf.antcontrib.antserver.commands.RunAntCommand;
37 import net.sf.antcontrib.antserver.commands.RunTargetCommand;
38 import net.sf.antcontrib.antserver.commands.SendFileCommand;
39 import net.sf.antcontrib.antserver.commands.ShutdownCommand;
40
41 /****************************************************************************
42 * Place class description here.
43 *
44 * @author <a href='mailto:[email protected]'>Matthew Inger</a>
45 * @author <additional author>
46 *
47 * @since
48 *
49 ****************************************************************************/
50
51
52 public class ClientTask
53 extends Task
54 {
55 private String machine = "localhost";
56 private int port = 17000;
57 private Vector commands;
58 private boolean persistant = false;
59 private boolean failOnError = true;
60
61 public ClientTask()
62 {
63 super();
64 this.commands = new Vector();
65 }
66
67
68 public void setMachine(String machine)
69 {
70 this.machine = machine;
71 }
72
73
74 public void setPort(int port)
75 {
76 this.port = port;
77 }
78
79
80 public void setPersistant(boolean persistant)
81 {
82 this.persistant = persistant;
83 }
84
85
86 public void setFailOnError(boolean failOnError)
87 {
88 this.failOnError = failOnError;
89 }
90
91
92 public void addConfiguredShutdown(ShutdownCommand cmd)
93 {
94 commands.add(cmd);
95 }
96
97 public void addConfiguredRunTarget(RunTargetCommand cmd)
98 {
99 commands.add(cmd);
100 }
101
102 public void addConfiguredRunAnt(RunAntCommand cmd)
103 {
104 commands.add(cmd);
105 }
106
107 public void addConfiguredSendFile(SendFileCommand cmd)
108 {
109 commands.add(cmd);
110 }
111
112
113 public void execute()
114 {
115 Enumeration e = commands.elements();
116 Command c = null;
117 while (e.hasMoreElements())
118 {
119 c = (Command)e.nextElement();
120 c.validate(getProject());
121 }
122
123 Client client = new Client(getProject(), machine, port);
124
125 try
126 {
127 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
128 DocumentBuilder db = dbf.newDocumentBuilder();
129
130 try
131 {
132 int failCount = 0;
133
134 client.connect();
135
136 e = commands.elements();
137 c = null;
138 Response r = null;
139 Document d = null;
140 boolean keepGoing = true;
141 while (e.hasMoreElements() && keepGoing)
142 {
143 c = (Command)e.nextElement();
144 r = client.sendCommand(c);
145 if (! r.isSucceeded())
146 {
147 failCount++;
148 log("Command caused a build failure:" + c,
149 Project.MSG_ERR);
150 log(r.getErrorMessage(),
151 Project.MSG_ERR);
152 log(r.getErrorStackTrace(),
153 Project.MSG_DEBUG);
154 if (! persistant)
155 keepGoing = false;
156 }
157
158 try
159 {
160 ByteArrayInputStream bais =
161 new ByteArrayInputStream(r.getResultsXml().getBytes());
162 d = db.parse(bais);
163 NodeList nl = d.getElementsByTagName("target");
164 int len = nl.getLength();
165 Element element = null;
166 for (int i=0;i<len;i++)
167 {
168 element = (Element)nl.item(i);
169 getProject().log("[" + element.getAttribute("name") + "]",
170 Project.MSG_INFO);
171 }
172 }
173 catch (SAXException se)
174 {
175
176 }
177
178 if (c instanceof ShutdownCommand)
179 {
180 keepGoing = false;
181 client.shutdown();
182 }
183 }
184
185 if (failCount > 0 && failOnError)
186 throw new BuildException("One or more commands failed.");
187 }
188 finally
189 {
190 if (client != null)
191 client.disconnect();
192 }
193 }
194 catch (ParserConfigurationException ex)
195 {
196 throw new BuildException(ex);
197 }
198 catch (IOException ex)
199 {
200 throw new BuildException(ex);
201 }
202 }
203 }