169e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorlandpackage org.testng.remote.adapter;
269e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland
30f7e671c94aeedee2fbc796b3318d44b0297b6cdnullinimport java.io.IOException;
40f7e671c94aeedee2fbc796b3318d44b0297b6cdnullinimport java.net.ServerSocket;
50f7e671c94aeedee2fbc796b3318d44b0297b6cdnullinimport java.net.Socket;
60f7e671c94aeedee2fbc796b3318d44b0297b6cdnullinimport java.util.Properties;
70f7e671c94aeedee2fbc796b3318d44b0297b6cdnullin
8ad662c0a3971e0461a1aaa7580fd3c7e7a114a02Cédric Beustimport org.testng.ISuite;
9ad662c0a3971e0461a1aaa7580fd3c7e7a114a02Cédric Beustimport org.testng.internal.Utils;
10ad662c0a3971e0461a1aaa7580fd3c7e7a114a02Cédric Beustimport org.testng.remote.ConnectionInfo;
11ad662c0a3971e0461a1aaa7580fd3c7e7a114a02Cédric Beustimport org.testng.xml.XmlSuite;
12ad662c0a3971e0461a1aaa7580fd3c7e7a114a02Cédric Beust
1369e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland
1469e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland/**
1569e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland * Default Slave adapter, provides an adapter based on static port.
160f7e671c94aeedee2fbc796b3318d44b0297b6cdnullin *
170f7e671c94aeedee2fbc796b3318d44b0297b6cdnullin *
1869e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland * @author	Guy Korland
19ad662c0a3971e0461a1aaa7580fd3c7e7a114a02Cédric Beust * @since 	April 20, 2007
2069e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland */
21c6c960fdd463e58af036e82bf0e35c5a3a953b03Tomás Pollakpublic class DefaultWorkerAdapter implements IWorkerAdapter
2269e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland{
2369e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland	public static final String SLAVE_PORT = "slave.port";
240f7e671c94aeedee2fbc796b3318d44b0297b6cdnullin
2569e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland	private ConnectionInfo m_connectionInfo;
260f7e671c94aeedee2fbc796b3318d44b0297b6cdnullin	private int m_clientPort;
270f7e671c94aeedee2fbc796b3318d44b0297b6cdnullin
280f7e671c94aeedee2fbc796b3318d44b0297b6cdnullin	@Override
290f7e671c94aeedee2fbc796b3318d44b0297b6cdnullin  public void init( Properties prop) throws Exception
3069e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland	{
3169e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland		m_clientPort = Integer.parseInt( prop.getProperty(SLAVE_PORT, "0"));
3269e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland		m_connectionInfo = resetSocket( m_clientPort, null);
3369e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland	}
340f7e671c94aeedee2fbc796b3318d44b0297b6cdnullin
3569e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland	/*
3669e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland	 * @see org.testng.remote.adapter.IWorkerApadter#getSuite(long)
3769e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland	 */
380f7e671c94aeedee2fbc796b3318d44b0297b6cdnullin	@Override
390f7e671c94aeedee2fbc796b3318d44b0297b6cdnullin  public XmlSuite getSuite(long timeout) throws InterruptedException, IOException
4069e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland	{
4169e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland      try {
4269e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland        return (XmlSuite) m_connectionInfo.getOis().readObject();
4369e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland      }
4469e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland      catch (ClassNotFoundException e) {
4569e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland        e.printStackTrace(System.out);
460f7e671c94aeedee2fbc796b3318d44b0297b6cdnullin        throw new RuntimeException( e);
470f7e671c94aeedee2fbc796b3318d44b0297b6cdnullin      }
4869e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland      catch(IOException ex) {
4969e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland        log("Connection closed " + ex.getMessage());
5069e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland        m_connectionInfo = resetSocket(m_clientPort, m_connectionInfo);
5169e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland        throw ex;
5269e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland      }
5369e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland	}
5469e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland
5569e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland	/*
5669e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland	 * @see org.testng.remote.adapter.IWorkerApadter#returnResult(org.testng.ISuite)
5769e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland	 */
580f7e671c94aeedee2fbc796b3318d44b0297b6cdnullin	@Override
590f7e671c94aeedee2fbc796b3318d44b0297b6cdnullin  public void returnResult(ISuite result) throws IOException
6069e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland	{
6169e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland		try
6269e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland		{
6369e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland			m_connectionInfo.getOos().writeObject(result);
6469e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland		}
6569e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland		catch(IOException ex) {
6669e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland			log("Connection closed " + ex.getMessage());
6769e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland			m_connectionInfo = resetSocket(m_clientPort, m_connectionInfo);
6869e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland			throw ex;
6969e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland		}
7069e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland	}
710f7e671c94aeedee2fbc796b3318d44b0297b6cdnullin
720f7e671c94aeedee2fbc796b3318d44b0297b6cdnullin	private static ConnectionInfo resetSocket(int clientPort, ConnectionInfo oldCi)
730f7e671c94aeedee2fbc796b3318d44b0297b6cdnullin	throws IOException
7469e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland	{
7569e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland		ConnectionInfo result = new ConnectionInfo();
7669e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland		ServerSocket serverSocket = new ServerSocket(clientPort);
7769e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland		serverSocket.setReuseAddress(true);
7869e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland		log("Waiting for connections on port " + clientPort);
7969e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland		Socket socket = serverSocket.accept();
8069e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland		result.setSocket(socket);
8169e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland
8269e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland		return result;
8369e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland	}
840f7e671c94aeedee2fbc796b3318d44b0297b6cdnullin
8569e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland	private static void log(String string) {
8669e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland		Utils.log("", 2, string);
8769e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland	}
8869e715ffa3624f1dd4b1bc55cb8919d25dc732b3gkorland}
89