| 1 package org.sapia.ubik.util; 2 3 73 public class StartStopLock { 74 75 public boolean started, stopped, stopRequested; 76 77 private Throwable _startErr, _stopErr; 78 79 private StopRequestListener _listener; 80 81 82 88 public StartStopLock(StopRequestListener listener){ 89 _listener = listener; 90 } 91 92 public StartStopLock(){ 93 } 94 95 98 public boolean isStarted(){ 99 return started; 100 } 101 102 105 public boolean isStopped(){ 106 return stopped; 107 } 108 109 112 public boolean isStopRequested(){ 113 return stopRequested; 114 } 115 116 122 public synchronized void triggerStop(){ 123 stopRequested = true; 124 125 if(_listener != null){ 126 try{ 127 _listener.onStopRequested(); 128 }catch(Throwable t){ 129 notifyStopped(t); 130 return; 131 } 132 } 133 notifyStopped(null); 134 } 135 136 147 public synchronized void waitStarted(long timeout) throws InterruptedException , Throwable { 148 while(!started && _startErr == null){ 149 wait(timeout); 150 } 151 if(_startErr != null) 152 throw _startErr; 153 } 154 155 164 public synchronized void waitStarted() throws InterruptedException , Throwable { 165 while(!started && _startErr == null){ 166 wait(); 167 } 168 if(_startErr != null) 169 throw _startErr; 170 } 171 172 183 public synchronized void waitStopped(long timeout) throws InterruptedException , Throwable { 184 while(!stopped && _stopErr == null){ 185 wait(timeout); 186 } 187 if(_stopErr != null) 188 throw _stopErr; 189 } 190 191 200 public synchronized void waitStopped() throws InterruptedException , Throwable { 201 while(!stopped && _stopErr == null){ 202 wait(); 203 } 204 if(_stopErr != null) 205 throw _stopErr; 206 } 207 208 211 public synchronized void notifyStarted(Throwable err){ 212 _startErr = err; 213 if(err == null){ 214 started = true; 215 } 216 notifyAll(); 217 } 218 219 222 public synchronized void notifyStopped(Throwable err){ 223 _stopErr = err; 224 if(err == null){ 225 stopped = true; 226 } 227 notifyAll(); 228 } 229 230 public interface StopRequestListener{ 231 public void onStopRequested() throws Throwable ; 232 } 233 234 } 235 | Popular Tags |