1   //========================================================================
2   //$Id: AbstractLifeCycle.java,v 1.3 2005/11/11 22:55:41 gregwilkins Exp $
3   //Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.
4   //------------------------------------------------------------------------
5   //Licensed under the Apache License, Version 2.0 (the "License");
6   //you may not use this file except in compliance with the License.
7   //You may obtain a copy of the License at
8   //http://www.apache.org/licenses/LICENSE-2.0
9   //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  
20  /**
21   * Basic implementation of the life cycle interface for components.
22   * @author gregw
23   */
24  public abstract class AbstractLifeCycle implements LifeCycle
25  {
26      private Object _lock=new Object();
27      private final int FAILED=-1,STOPPED=0,STARTING=1,STARTED=2,STOPPING=3;
28      private transient int _state=STOPPED;
29      protected void doStart() throws Exception {}
30      protected void doStop() throws Exception {}
31  
32  
33      public final void start() throws Exception
34      {
35          synchronized (_lock)
36          {
37              try
38              {
39                  if (_state==STARTED || _state==STARTING)
40                      return;
41                  _state=STARTING;
42                  doStart();
43                  Log.debug("started {}",this);
44                  _state=STARTED;
45              }
46              catch (Exception e)
47              {
48                  Log.warn("failed "+this,e);
49                  _state=FAILED;
50                  throw e;
51              }
52              catch(Error e)
53              {
54                  Log.warn("failed "+this,e);
55                  _state=FAILED;
56                  throw e;
57              }
58          }
59      }
60  
61      public final void stop() throws Exception
62      {
63          synchronized (_lock)
64          {
65              try
66              {
67                  if (_state==STOPPING || _state==STOPPED)
68                      return;
69                  _state=STOPPING;
70                  doStop();
71                  Log.debug("stopped {}",this);
72                  _state=STOPPED;
73              }
74              catch (Exception e)
75              {
76                  Log.warn("failed "+this,e);
77                  _state=FAILED;
78                  throw e;
79              }
80              catch(Error e)
81              {
82                  Log.warn("failed "+this,e);
83                  _state=FAILED;
84                  throw e;
85              }
86          }
87      }
88  
89      public boolean isRunning()
90      {
91          return _state==STARTED || _state==STARTING;
92      }
93  
94      public boolean isStarted()
95      {
96          return _state==STARTED;
97      }
98  
99      public boolean isStarting()
100     {
101         return _state==STARTING;
102     }
103 
104     public boolean isStopping()
105     {
106         return _state==STOPPING;
107     }
108 
109     public boolean isStopped()
110     {
111         return _state==STOPPED;
112     }
113 
114     public boolean isFailed()
115     {
116         return _state==FAILED;
117     }
118 }