DefaultWorkerAdapter.java revision 69e715ffa3624f1dd4b1bc55cb8919d25dc732b3
169e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorlandpackage org.testng.remote.adapter;
269e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland
369e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorlandimport java.io.IOException;
469e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorlandimport java.net.ServerSocket;
569e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorlandimport java.net.Socket;
669e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorlandimport java.util.Properties;
769e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland
869e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorlandimport org.testng.ISuite;
969e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorlandimport org.testng.internal.Utils;
1069e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorlandimport org.testng.remote.ConnectionInfo;
1169e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorlandimport org.testng.xml.XmlSuite;
1269e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland
1369e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland
1469e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland/**
1569e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland * Default Slave adapter, provides an adapter based on static port.
1669e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland *
1769e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland *
1869e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland * @author	Guy Korland
1969e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland * @date 	April 20, 2007
2069e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland */
2169e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorlandpublic class DefaultWorkerAdapter implements IWorkerApadter
2269e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland{
2369e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland	public static final String SLAVE_PORT = "slave.port";
2469e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland
2569e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland	private ConnectionInfo m_connectionInfo;
2669e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland	private int m_clientPort;
2769e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland
2869e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland	public void init( Properties prop) throws Exception
2969e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland	{
3069e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland		m_clientPort = Integer.parseInt( prop.getProperty(SLAVE_PORT, "0"));
3169e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland		m_connectionInfo = resetSocket( m_clientPort, null);
3269e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland	}
3369e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland
3469e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland	/*
3569e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland	 * @see org.testng.remote.adapter.IWorkerApadter#getSuite(long)
3669e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland	 */
3769e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland	public XmlSuite getSuite(long timeout) throws InterruptedException, IOException
3869e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland	{
3969e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland      try {
4069e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland        return (XmlSuite) m_connectionInfo.getOis().readObject();
4169e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland      }
4269e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland      catch (ClassNotFoundException e) {
4369e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland        e.printStackTrace(System.out);
4469e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland        throw new RuntimeException( e);
4569e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland      }
4669e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland      catch(IOException ex) {
4769e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland        log("Connection closed " + ex.getMessage());
4869e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland        m_connectionInfo = resetSocket(m_clientPort, m_connectionInfo);
4969e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland        throw ex;
5069e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland      }
5169e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland	}
5269e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland
5369e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland	/*
5469e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland	 * @see org.testng.remote.adapter.IWorkerApadter#returnResult(org.testng.ISuite)
5569e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland	 */
5669e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland	public void returnResult(ISuite result) throws IOException
5769e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland	{
5869e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland		try
5969e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland		{
6069e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland			m_connectionInfo.getOos().writeObject(result);
6169e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland		}
6269e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland		catch(IOException ex) {
6369e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland			log("Connection closed " + ex.getMessage());
6469e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland			m_connectionInfo = resetSocket(m_clientPort, m_connectionInfo);
6569e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland			throw ex;
6669e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland		}
6769e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland	}
6869e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland
6969e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland	private static ConnectionInfo resetSocket(int clientPort, ConnectionInfo oldCi)
7069e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland	throws IOException
7169e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland	{
7269e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland		ConnectionInfo result = new ConnectionInfo();
7369e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland		ServerSocket serverSocket = new ServerSocket(clientPort);
7469e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland		serverSocket.setReuseAddress(true);
7569e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland		log("Waiting for connections on port " + clientPort);
7669e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland		Socket socket = serverSocket.accept();
7769e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland		result.setSocket(socket);
7869e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland
7969e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland		return result;
8069e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland	}
8169e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland
8269e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland	private static void log(String string) {
8369e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland		Utils.log("", 2, string);
8469e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland	}
8569e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland}
86