KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > systest > AgentStopper


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.systest;
19
20 /**
21  * A helper class used to stop a bunch of services, catching and logging any
22  * exceptions and then throwing the first exception when everything is stoped.
23  *
24  * @version $Revision: 1.1 $
25  */

26 public class AgentStopper {
27     private Exception JavaDoc firstException;
28
29     /**
30      * Stops the given service, catching any exceptions that are thrown.
31      */

32     public void stop(Agent service) {
33         if (service != null) {
34             try {
35                 service.stop(this);
36             }
37             catch (Exception JavaDoc e) {
38                 onException(service, e);
39             }
40         }
41     }
42
43     public void onException(Object JavaDoc owner, Exception JavaDoc e) {
44         logError(owner, e);
45         if (firstException == null) {
46             firstException = e;
47         }
48     }
49
50     /**
51      * Throws the first exception that was thrown if there was one.
52      */

53     public void throwFirstException() throws Exception JavaDoc {
54         if (firstException != null) {
55             throw firstException;
56         }
57     }
58
59     protected void logError(Object JavaDoc service, Exception JavaDoc e) {
60         System.err.println("Could not stop service: " + service + ". Reason: " + e);
61         e.printStackTrace(System.err);
62     }
63
64 }
65
Popular Tags