1 24 25 package com.rift.coad.lib.deployment.test; 27 28 import java.util.List ; 30 import java.util.ArrayList ; 31 import java.util.StringTokenizer ; 32 import java.util.Date ; 33 34 import com.rift.coad.lib.configuration.Configuration; 36 import com.rift.coad.lib.configuration.ConfigurationFactory; 37 38 44 public class TestMonitor { 45 46 private static TestMonitor singleton = null; 48 49 private long timeout = 0; 51 private List entries = null; 52 53 54 59 private TestMonitor() throws TestException { 60 try { 61 entries = new ArrayList (); 63 64 Configuration config = ConfigurationFactory.getInstance().getConfig( 66 this.getClass()); 67 68 timeout = config.getLong("Timeout"); 70 StringTokenizer stringTok = new StringTokenizer ( 71 config.getString("Monitor"),","); 72 while (stringTok.hasMoreTokens()) { 73 entries.add(stringTok.nextToken()); 74 } 75 76 } catch (Exception ex) { 77 throw new TestException("Failed to init the test monitor.",ex); 78 } 79 } 80 81 82 87 public static synchronized void init() throws TestException { 88 singleton = new TestMonitor(); 89 } 90 91 92 98 public static synchronized TestMonitor getInstance() throws TestException { 99 if (singleton != null) { 100 return singleton; 101 } 102 throw new TestException("The test monitor has not been initialized."); 103 } 104 105 106 112 public synchronized void alert(String element) throws TestException { 113 if (entries.contains(element)) { 114 entries.remove(element); 115 notify(); 116 } 117 } 118 119 120 126 public synchronized void monitor() throws TestException { 127 try { 128 Date startTime = new Date (); 129 while (entries.size() > 0) { 130 wait(timeout); 131 Date currentTime = new Date (); 132 if ((currentTime.getTime() - timeout) > startTime.getTime()) { 133 break; 134 } 135 } 136 } catch (Exception ex) { 137 throw new TestException("Monitoring failed because : " + 138 ex.getMessage(),ex); 139 } 140 141 if (entries.size() > 0) { 143 String entryString = new String (); 144 String sep = ""; 145 for (int count = 0; count < entries.size(); count++) { 146 entryString += sep + entries.get(count).toString(); 147 sep = ":"; 148 } 149 throw new TestException( 150 "Monitor test failed because not all monitored entries " + 151 "reported successfully [" + entryString + "]"); 152 } 153 } 154 } 155 | Popular Tags |