1 package org.testng.remote.adapter; 2 3 import java.io.IOException; 4 import java.net.Socket; 5 import java.net.UnknownHostException; 6 import java.util.List; 7 import java.util.Properties; 8 9 import org.testng.collections.Lists; 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 * @since April 20, 2007 22 */ 23 public class DefaultMastertAdapter 24 implements IMasterAdapter 25 { 26 public static final String HOSTS = "testng.hosts"; 27 28 private String[] m_hosts; 29 30 final private SlavePool m_slavePool = new SlavePool(); 31 final private List<Runnable> m_workers = Lists.newArrayList(); 32 33 /* 34 * @see org.testng.remote.adapter.IMasterAdapter#init(java.util.Properties) 35 */ 36 @Override init(Properties properties)37 public void init(Properties properties) 38 { 39 String hostLine = properties.getProperty(HOSTS); 40 m_hosts = hostLine.split(" "); 41 42 // 43 // Create one socket per host found 44 // 45 Socket[] sockets = new Socket[m_hosts.length]; 46 for (int i = 0; i < m_hosts.length; i++) { 47 String host = m_hosts[i]; 48 String[] s = host.split(":"); 49 try { 50 sockets[i] = new Socket(s[0], Integer.parseInt(s[1])); 51 } 52 catch (NumberFormatException | UnknownHostException e) { 53 e.printStackTrace(System.out); 54 } catch (IOException e) { 55 Utils.error("Couldn't connect to " + host + ": " + e.getMessage()); 56 } 57 } 58 59 // 60 // Add these hosts to the pool 61 // 62 try { 63 m_slavePool.addSlaves(sockets); 64 } 65 catch (IOException e1) { 66 e1.printStackTrace(System.out); 67 } 68 } 69 70 /* 71 * @see org.testng.remote.adapter.IMasterAdapter#runSuitesRemotely(java.util.List, org.testng.internal.annotations.IAnnotationFinder, org.testng.internal.annotations.IAnnotationFinder) 72 */ 73 @Override runSuitesRemotely( XmlSuite suite, RemoteResultListener listener)74 public void runSuitesRemotely( XmlSuite suite, RemoteResultListener listener) throws IOException 75 { 76 m_workers.add(new RemoteSuiteWorker(suite, m_slavePool, listener)); 77 } 78 79 @Override awaitTermination(long timeout)80 public void awaitTermination(long timeout) throws InterruptedException 81 { 82 ThreadUtil.execute(m_workers, 1, 10 * 1000L, false); 83 } 84 } 85