01: package org.testng.remote.adapter;
02:
03: import java.io.IOException;
04: import java.net.Socket;
05: import java.net.UnknownHostException;
06: import java.util.ArrayList;
07: import java.util.List;
08: import java.util.Properties;
09:
10: import org.testng.internal.Utils;
11: import org.testng.internal.remote.SlavePool;
12: import org.testng.internal.thread.ThreadUtil;
13: import org.testng.remote.RemoteSuiteWorker;
14: import org.testng.xml.XmlSuite;
15:
16: /**
17: * Default Master adapter, provides an adapter based on hosts file.
18: *
19: *
20: * @author Guy Korland
21: * @date April 20, 2007
22: */
23: public class DefaultMastertAdapter implements IMasterAdapter {
24: public static final String HOSTS = "testng.hosts";
25:
26: private String[] m_hosts;
27:
28: final private SlavePool m_slavePool = new SlavePool();
29: final private List<Runnable> m_workers = new ArrayList<Runnable>();
30:
31: /*
32: * @see org.testng.remote.adapter.IMasterAdapter#init(java.util.Properties)
33: */
34: public void init(Properties properties) {
35: String hostLine = properties.getProperty(HOSTS);
36: m_hosts = hostLine.split(" ");
37:
38: //
39: // Create one socket per host found
40: //
41: Socket[] sockets = new Socket[m_hosts.length];
42: for (int i = 0; i < m_hosts.length; i++) {
43: String host = m_hosts[i];
44: String[] s = host.split(":");
45: try {
46: sockets[i] = new Socket(s[0], Integer.parseInt(s[1]));
47: } catch (NumberFormatException e) {
48: e.printStackTrace(System.out);
49: } catch (UnknownHostException e) {
50: e.printStackTrace(System.out);
51: } catch (IOException e) {
52: Utils.error("Couldn't connect to " + host + ": "
53: + e.getMessage());
54: }
55: }
56:
57: //
58: // Add these hosts to the pool
59: //
60: try {
61: m_slavePool.addSlaves(sockets);
62: } catch (IOException e1) {
63: e1.printStackTrace(System.out);
64: }
65: }
66:
67: /*
68: * @see org.testng.remote.adapter.IMasterAdapter#runSuitesRemotely(java.util.List, org.testng.internal.annotations.IAnnotationFinder, org.testng.internal.annotations.IAnnotationFinder)
69: */
70: public void runSuitesRemotely(XmlSuite suite,
71: RemoteResultListener listener) throws IOException {
72: m_workers.add(new RemoteSuiteWorker(suite, m_slavePool,
73: listener));
74: }
75:
76: public void awaitTermination(long timeout)
77: throws InterruptedException {
78: ThreadUtil.execute(m_workers, 1, 10 * 1000L, false);
79: }
80: }
|