01: //========================================================================
02: //$Id: AbstractLifeCycle.java,v 1.3 2005/11/11 22:55:41 gregwilkins Exp $
03: //Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.
04: //------------------------------------------------------------------------
05: //Licensed under the Apache License, Version 2.0 (the "License");
06: //you may not use this file except in compliance with the License.
07: //You may obtain a copy of the License at
08: //http://www.apache.org/licenses/LICENSE-2.0
09: //Unless required by applicable law or agreed to in writing, software
10: //distributed under the License is distributed on an "AS IS" BASIS,
11: //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: //See the License for the specific language governing permissions and
13: //limitations under the License.
14: //========================================================================
15:
16: package org.mortbay.component;
17:
18: import org.mortbay.log.Log;
19: import org.mortbay.util.MultiException;
20:
21: /**
22: * Basic implementation of the life cycle interface for components.
23: * @author gregw
24: */
25: public abstract class AbstractLifeCycle implements LifeCycle {
26: private final int FAILED = -1, STOPPED = 0, STARTING = 1,
27: STARTED = 2, STOPPING = 3;
28: private transient int _state = STOPPED;
29:
30: protected void doStart() throws Exception {
31: }
32:
33: protected void doStop() throws Exception {
34: }
35:
36: public final void start() throws Exception {
37: try {
38: if (_state == STARTED)
39: return;
40: _state = STARTING;
41: doStart();
42: Log.debug("started {}", this );
43: _state = STARTED;
44: } catch (Exception e) {
45: Log.warn("failed " + this , e);
46: _state = FAILED;
47: throw e;
48: } catch (Error e) {
49: Log.warn("failed " + this , e);
50: _state = FAILED;
51: throw e;
52: }
53: }
54:
55: public final void stop() throws Exception {
56: try {
57: if (_state == STOPPING || _state == STOPPED)
58: return;
59: _state = STOPPING;
60: doStop();
61: Log.debug("stopped {}", this );
62: _state = STOPPED;
63: } catch (Exception e) {
64: Log.warn("failed " + this , e);
65: _state = FAILED;
66: throw e;
67: } catch (Error e) {
68: Log.warn("failed " + this , e);
69: _state = FAILED;
70: throw e;
71: }
72: }
73:
74: public boolean isRunning() {
75: return _state == STARTED || _state == STARTING;
76: }
77:
78: public boolean isStarted() {
79: return _state == STARTED;
80: }
81:
82: public boolean isStarting() {
83: return _state == STARTING;
84: }
85:
86: public boolean isStopping() {
87: return _state == STOPPING;
88: }
89:
90: public boolean isStopped() {
91: return _state == STOPPED;
92: }
93:
94: public boolean isFailed() {
95: return _state == FAILED;
96: }
97: }
|