01: package org.testng.remote.adapter;
02:
03: import java.io.IOException;
04: import java.net.ServerSocket;
05: import java.net.Socket;
06: import java.util.Properties;
07:
08: import org.testng.ISuite;
09: import org.testng.internal.Utils;
10: import org.testng.remote.ConnectionInfo;
11: import org.testng.xml.XmlSuite;
12:
13: /**
14: * Default Slave adapter, provides an adapter based on static port.
15: *
16: *
17: * @author Guy Korland
18: * @date April 20, 2007
19: */
20: public class DefaultWorkerAdapter implements IWorkerApadter {
21: public static final String SLAVE_PORT = "slave.port";
22:
23: private ConnectionInfo m_connectionInfo;
24: private int m_clientPort;
25:
26: public void init(Properties prop) throws Exception {
27: m_clientPort = Integer.parseInt(prop.getProperty(SLAVE_PORT,
28: "0"));
29: m_connectionInfo = resetSocket(m_clientPort, null);
30: }
31:
32: /*
33: * @see org.testng.remote.adapter.IWorkerApadter#getSuite(long)
34: */
35: public XmlSuite getSuite(long timeout) throws InterruptedException,
36: IOException {
37: try {
38: return (XmlSuite) m_connectionInfo.getOis().readObject();
39: } catch (ClassNotFoundException e) {
40: e.printStackTrace(System.out);
41: throw new RuntimeException(e);
42: } catch (IOException ex) {
43: log("Connection closed " + ex.getMessage());
44: m_connectionInfo = resetSocket(m_clientPort,
45: m_connectionInfo);
46: throw ex;
47: }
48: }
49:
50: /*
51: * @see org.testng.remote.adapter.IWorkerApadter#returnResult(org.testng.ISuite)
52: */
53: public void returnResult(ISuite result) throws IOException {
54: try {
55: m_connectionInfo.getOos().writeObject(result);
56: } catch (IOException ex) {
57: log("Connection closed " + ex.getMessage());
58: m_connectionInfo = resetSocket(m_clientPort,
59: m_connectionInfo);
60: throw ex;
61: }
62: }
63:
64: private static ConnectionInfo resetSocket(int clientPort,
65: ConnectionInfo oldCi) throws IOException {
66: ConnectionInfo result = new ConnectionInfo();
67: ServerSocket serverSocket = new ServerSocket(clientPort);
68: serverSocket.setReuseAddress(true);
69: log("Waiting for connections on port " + clientPort);
70: Socket socket = serverSocket.accept();
71: result.setSocket(socket);
72:
73: return result;
74: }
75:
76: private static void log(String string) {
77: Utils.log("", 2, string);
78: }
79: }
|