KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.concurrent.atomic.AtomicBoolean JavaDoc;
21
22 import org.apache.activemq.Service;
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25
26 /**
27  * A helper class for working with services together with a useful base class for service implementations.
28  *
29  * @version $Revision: 1.1 $
30  */

31 public abstract class ServiceSupport implements Service {
32     private static final Log log = LogFactory.getLog(ServiceSupport.class);
33
34     private AtomicBoolean JavaDoc started = new AtomicBoolean JavaDoc(false);
35     private AtomicBoolean JavaDoc stopping = new AtomicBoolean JavaDoc(false);
36     private AtomicBoolean JavaDoc stopped = new AtomicBoolean JavaDoc(false);
37
38     public static void dispose(Service service) {
39         try {
40             service.stop();
41         }
42         catch (Exception JavaDoc e) {
43             log.debug("Could not stop service: " + service + ". Reason: " + e, e);
44         }
45     }
46
47     public void start() throws Exception JavaDoc {
48         if (started.compareAndSet(false, true)) {
49             doStart();
50         }
51     }
52
53     public void stop() throws Exception JavaDoc {
54         if (stopped.compareAndSet(false, true)) {
55             stopping.set(true);
56             ServiceStopper stopper = new ServiceStopper();
57             try {
58                 doStop(stopper);
59             }
60             catch (Exception JavaDoc e) {
61                 stopper.onException(this, e);
62             }
63             stopped.set(true);
64             started.set(false);
65             stopping.set(false);
66             stopper.throwFirstException();
67         }
68     }
69
70     /**
71      * @return true if this service has been started
72      */

73     public boolean isStarted() {
74         return started.get();
75     }
76
77     /**
78      * @return true if this service is in the process of closing
79      */

80     public boolean isStopping() {
81         return stopping.get();
82     }
83
84     
85     /**
86      * @return true if this service is closed
87      */

88     public boolean isStopped() {
89         return stopped.get();
90     }
91
92     protected abstract void doStop(ServiceStopper stopper) throws Exception JavaDoc;
93
94     protected abstract void doStart() throws Exception JavaDoc;
95 }
96
Popular Tags