KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > util > ServiceStopper


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

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 JavaDoc;
25 import java.util.List JavaDoc;
26
27 /**
28  * A helper class used to stop a bunch of services, catching and logging any
29  * exceptions and then throwing the first exception when everything is stoped.
30  *
31  * @version $Revision: 1.1 $
32  */

33 public class ServiceStopper {
34     private Throwable JavaDoc firstException;
35
36     /**
37      * Stops the given service, catching any exceptions that are thrown.
38      */

39     public void stop(Service service) {
40         try {
41             if( service!=null ) {
42                 service.stop();
43             }
44         }
45         catch (Exception JavaDoc e) {
46             onException(service, e);
47         }
48     }
49
50     /**
51      * Performs the given code to stop some service handling the exceptions
52      * which may be thrown properly
53      */

54     public void run(Callback stopClosure) {
55         try {
56             stopClosure.execute();
57         }
58         catch (Throwable JavaDoc e) {
59             onException(stopClosure, e);
60         }
61     }
62
63     /**
64      * Stops a list of services
65      */

66     public void stopServices(List JavaDoc services) {
67         for (Iterator JavaDoc iter = services.iterator(); iter.hasNext();) {
68             Service service = (Service) iter.next();
69             stop(service);
70         }
71     }
72
73     public void onException(Object JavaDoc owner, Throwable JavaDoc e) {
74         logError(owner, e);
75         if (firstException == null) {
76             firstException = e;
77         }
78     }
79
80     /**
81      * Throws the first exception that was thrown if there was one.
82      */

83     public void throwFirstException() throws Exception JavaDoc {
84         if (firstException != null) {
85             if (firstException instanceof Exception JavaDoc) {
86                 Exception JavaDoc e = (Exception JavaDoc) firstException;
87                 throw e;
88             }
89             else if (firstException instanceof RuntimeException JavaDoc) {
90                 RuntimeException JavaDoc e = (RuntimeException JavaDoc) firstException;
91                 throw e;
92             }
93             else {
94                 throw new RuntimeException JavaDoc("Unknown type of exception: " + firstException, firstException);
95             }
96         }
97     }
98
99     protected void logError(Object JavaDoc service, Throwable JavaDoc 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