1 18 package org.apache.activemq.util; 19 20 import org.apache.activemq.Service; 21 import org.apache.commons.logging.Log; 22 import org.apache.commons.logging.LogFactory; 23 24 import java.util.Iterator ; 25 import java.util.List ; 26 27 33 public class ServiceStopper { 34 private Throwable firstException; 35 36 39 public void stop(Service service) { 40 try { 41 if( service!=null ) { 42 service.stop(); 43 } 44 } 45 catch (Exception e) { 46 onException(service, e); 47 } 48 } 49 50 54 public void run(Callback stopClosure) { 55 try { 56 stopClosure.execute(); 57 } 58 catch (Throwable e) { 59 onException(stopClosure, e); 60 } 61 } 62 63 66 public void stopServices(List services) { 67 for (Iterator iter = services.iterator(); iter.hasNext();) { 68 Service service = (Service) iter.next(); 69 stop(service); 70 } 71 } 72 73 public void onException(Object owner, Throwable e) { 74 logError(owner, e); 75 if (firstException == null) { 76 firstException = e; 77 } 78 } 79 80 83 public void throwFirstException() throws Exception { 84 if (firstException != null) { 85 if (firstException instanceof Exception ) { 86 Exception e = (Exception ) firstException; 87 throw e; 88 } 89 else if (firstException instanceof RuntimeException ) { 90 RuntimeException e = (RuntimeException ) firstException; 91 throw e; 92 } 93 else { 94 throw new RuntimeException ("Unknown type of exception: " + firstException, firstException); 95 } 96 } 97 } 98 99 protected void logError(Object service, Throwable e) { 100 Log log = LogFactory.getLog(service.getClass()); 101 log.error("Could not stop service: " + service + ". Reason: " + e, e); 102 } 103 104 } 105 | Popular Tags |