| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package net.sf.antcontrib.antserver.server; |
| 17 |
|
| 18 |
import java.io.*; |
| 19 |
import java.net.Socket; |
| 20 |
|
| 21 |
import org.apache.tools.ant.Project; |
| 22 |
import org.apache.xml.serialize.OutputFormat; |
| 23 |
import org.apache.xml.serialize.XMLSerializer; |
| 24 |
|
| 25 |
import net.sf.antcontrib.antserver.Command; |
| 26 |
import net.sf.antcontrib.antserver.Response; |
| 27 |
import net.sf.antcontrib.antserver.Util; |
| 28 |
import net.sf.antcontrib.antserver.commands.DisconnectCommand; |
| 29 |
import net.sf.antcontrib.antserver.commands.ShutdownCommand; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public class ConnectionHandler |
| 43 |
implements Runnable |
| 44 |
{ |
| 45 |
private static long nextGroupId = 0; |
| 46 |
private ServerTask task; |
| 47 |
private Socket socket; |
| 48 |
private Thread thread; |
| 49 |
private Throwable thrown; |
| 50 |
|
| 51 |
public ConnectionHandler(ServerTask task, Socket socket) |
| 52 |
{ |
| 53 |
super(); |
| 54 |
this.socket = socket; |
| 55 |
this.task = task; |
| 56 |
} |
| 57 |
|
| 58 |
public void start() |
| 59 |
{ |
| 60 |
long gid = nextGroupId; |
| 61 |
if (nextGroupId == Long.MAX_VALUE) |
| 62 |
nextGroupId = 0; |
| 63 |
else |
| 64 |
nextGroupId++; |
| 65 |
|
| 66 |
ThreadGroup group = new ThreadGroup("server-tg-" + gid); |
| 67 |
thread = new Thread(group, this); |
| 68 |
thread.start(); |
| 69 |
} |
| 70 |
|
| 71 |
public Throwable getThrown() |
| 72 |
{ |
| 73 |
return thrown; |
| 74 |
} |
| 75 |
|
| 76 |
public void run() |
| 77 |
{ |
| 78 |
InputStream is = null; |
| 79 |
OutputStream os = null; |
| 80 |
|
| 81 |
|
| 82 |
try |
| 83 |
{ |
| 84 |
ConnectionBuildListener cbl = null; |
| 85 |
|
| 86 |
is = socket.getInputStream(); |
| 87 |
os = socket.getOutputStream(); |
| 88 |
|
| 89 |
ObjectInputStream ois = new ObjectInputStream(is); |
| 90 |
ObjectOutputStream oos = new ObjectOutputStream(os); |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
oos.writeObject(new Response()); |
| 95 |
|
| 96 |
boolean disconnect = false; |
| 97 |
Command inputCommand = null; |
| 98 |
Response response = null; |
| 99 |
|
| 100 |
while (! disconnect) |
| 101 |
{ |
| 102 |
task.getProject().log("Reading command object.", |
| 103 |
Project.MSG_DEBUG); |
| 104 |
|
| 105 |
inputCommand = (Command) ois.readObject(); |
| 106 |
|
| 107 |
task.getProject().log("Executing command object: " + inputCommand, |
| 108 |
Project.MSG_DEBUG); |
| 109 |
|
| 110 |
response = new Response(); |
| 111 |
|
| 112 |
try |
| 113 |
{ |
| 114 |
cbl = new ConnectionBuildListener(); |
| 115 |
task.getProject().addBuildListener(cbl); |
| 116 |
|
| 117 |
inputCommand.execute(task.getProject(), |
| 118 |
inputCommand.getContentLength(), |
| 119 |
is); |
| 120 |
|
| 121 |
response.setSucceeded(true); |
| 122 |
} |
| 123 |
catch (Throwable t) |
| 124 |
{ |
| 125 |
response.setSucceeded(false); |
| 126 |
response.setThrowable(t); |
| 127 |
} |
| 128 |
finally |
| 129 |
{ |
| 130 |
if (cbl != null) |
| 131 |
task.getProject().removeBuildListener(cbl); |
| 132 |
} |
| 133 |
|
| 134 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 135 |
XMLSerializer serial = new XMLSerializer(); |
| 136 |
OutputFormat fmt = new OutputFormat(); |
| 137 |
fmt.setOmitDocumentType(true); |
| 138 |
fmt.setOmitXMLDeclaration(false); |
| 139 |
serial.setOutputFormat(fmt); |
| 140 |
serial.setOutputByteStream(baos); |
| 141 |
serial.serialize(cbl.getDocument()); |
| 142 |
response.setResultsXml(baos.toString()); |
| 143 |
|
| 144 |
task.getProject().log("Executed command object: " + inputCommand, |
| 145 |
Project.MSG_DEBUG); |
| 146 |
|
| 147 |
task.getProject().log("Sending response: " + response, |
| 148 |
Project.MSG_DEBUG); |
| 149 |
|
| 150 |
response.setContentLength(inputCommand.getContentLength()); |
| 151 |
|
| 152 |
oos.writeObject(response); |
| 153 |
|
| 154 |
if (inputCommand.getResponseContentLength() != 0) |
| 155 |
{ |
| 156 |
Util.transferBytes(inputCommand.getReponseContentStream(), |
| 157 |
inputCommand.getResponseContentLength(), |
| 158 |
os, |
| 159 |
true); |
| 160 |
} |
| 161 |
|
| 162 |
if (inputCommand instanceof DisconnectCommand) |
| 163 |
{ |
| 164 |
disconnect = true; |
| 165 |
task.getProject().log("Got disconnect command", |
| 166 |
Project.MSG_DEBUG); |
| 167 |
} |
| 168 |
else if (inputCommand instanceof ShutdownCommand) |
| 169 |
{ |
| 170 |
disconnect = true; |
| 171 |
task.getProject().log("Got shutdown command", |
| 172 |
Project.MSG_DEBUG); |
| 173 |
task.shutdown(); |
| 174 |
} |
| 175 |
|
| 176 |
} |
| 177 |
|
| 178 |
} |
| 179 |
catch (ClassNotFoundException e) |
| 180 |
{ |
| 181 |
thrown = e; |
| 182 |
} |
| 183 |
catch (IOException e) |
| 184 |
{ |
| 185 |
thrown = e; |
| 186 |
} |
| 187 |
catch (Throwable t) |
| 188 |
{ |
| 189 |
thrown = t; |
| 190 |
} |
| 191 |
finally |
| 192 |
{ |
| 193 |
if (is != null) |
| 194 |
{ |
| 195 |
try |
| 196 |
{ |
| 197 |
is.close(); |
| 198 |
} |
| 199 |
catch (IOException e) |
| 200 |
{ |
| 201 |
|
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
if (os != null) |
| 206 |
{ |
| 207 |
try |
| 208 |
{ |
| 209 |
os.close(); |
| 210 |
} |
| 211 |
catch (IOException e) |
| 212 |
{ |
| 213 |
|
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
if (socket != null) |
| 218 |
{ |
| 219 |
try |
| 220 |
{ |
| 221 |
socket.close(); |
| 222 |
} |
| 223 |
catch (IOException e) |
| 224 |
{ |
| 225 |
|
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
} |
| 230 |
} |
| 231 |
} |