View Javadoc

1   // ========================================================================
2   // Copyright (c) 2004-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package org.eclipse.jetty.util.component;
15  
16  import org.eclipse.jetty.util.LazyList;
17  import org.eclipse.jetty.util.log.Log;
18  
19  /**
20   * Basic implementation of the life cycle interface for components.
21   * 
22   * 
23   */
24  public abstract class AbstractLifeCycle implements LifeCycle
25  {
26      public static final String STOPPED="STOPPED";
27      public static final String FAILED="FAILED";
28      public static final String STARTING="STARTING";
29      public static final String STARTED="STARTED";
30      public static final String STOPPING="STOPPING";
31      public static final String RUNNING="RUNNING";
32      
33      private final Object _lock = new Object();
34      private final int __FAILED = -1, __STOPPED = 0, __STARTING = 1, __STARTED = 2, __STOPPING = 3;
35      private volatile int _state = __STOPPED;
36      protected LifeCycle.Listener[] _listeners;
37  
38      protected void doStart() throws Exception
39      {
40      }
41  
42      protected void doStop() throws Exception
43      {
44      }
45  
46      public final void start() throws Exception
47      {
48          synchronized (_lock)
49          {
50              try
51              {
52                  if (_state == __STARTED || _state == __STARTING)
53                      return;
54                  setStarting();
55                  doStart();
56                  Log.debug(STARTED+" {}",this);
57                  setStarted();
58              }
59              catch (Exception e)
60              {
61                  Log.debug(FAILED+" " + this,e);
62                  setFailed(e);
63                  throw e;
64              }
65              catch (Error e)
66              {
67                  Log.debug(FAILED+" " + this,e);
68                  setFailed(e);
69                  throw e;
70              }
71          }
72      }
73  
74      public final void stop() throws Exception
75      {
76          synchronized (_lock)
77          {
78              try
79              {
80                  if (_state == __STOPPING || _state == __STOPPED)
81                      return;
82                  setStopping();
83                  doStop();
84                  Log.debug(STOPPED+" {}",this);
85                  setStopped();
86              }
87              catch (Exception e)
88              {
89                  Log.debug(FAILED+" " + this,e);
90                  setFailed(e);
91                  throw e;
92              }
93              catch (Error e)
94              {
95                  Log.debug(FAILED+" " + this,e);
96                  setFailed(e);
97                  throw e;
98              }
99          }
100     }
101 
102     public boolean isRunning()
103     {
104         return _state == __STARTED || _state == __STARTING;
105     }
106 
107     public boolean isStarted()
108     {
109         return _state == __STARTED;
110     }
111 
112     public boolean isStarting()
113     {
114         return _state == __STARTING;
115     }
116 
117     public boolean isStopping()
118     {
119         return _state == __STOPPING;
120     }
121 
122     public boolean isStopped()
123     {
124         return _state == __STOPPED;
125     }
126 
127     public boolean isFailed()
128     {
129         return _state == __FAILED;
130     }
131 
132     public void addLifeCycleListener(LifeCycle.Listener listener)
133     {
134         _listeners = (LifeCycle.Listener[])LazyList.addToArray(_listeners,listener,LifeCycle.Listener.class);
135     }
136 
137     public void removeLifeCycleListener(LifeCycle.Listener listener)
138     {
139         LazyList.removeFromArray(_listeners,listener);
140     }
141     
142     public String getState()
143     {
144         switch(_state)
145         {
146             case __FAILED: return FAILED;
147             case __STARTING: return STARTING;
148             case __STARTED: return STARTED;
149             case __STOPPING: return STOPPING;
150             case __STOPPED: return STOPPED;
151         }
152         return null;
153     }
154 
155     private void setStarted()
156     {
157         _state = __STARTED;
158         if (_listeners != null)
159         {
160             for (int i = 0; i < _listeners.length; i++)
161             {
162                 _listeners[i].lifeCycleStarted(this);
163             }
164         }
165     }
166 
167     private void setStarting()
168     {
169         _state = __STARTING;
170         if (_listeners != null)
171         {
172             for (int i = 0; i < _listeners.length; i++)
173             {
174                 _listeners[i].lifeCycleStarting(this);
175             }
176         }
177     }
178 
179     private void setStopping()
180     {
181         _state = __STOPPING;
182         if (_listeners != null)
183         {
184             for (int i = 0; i < _listeners.length; i++)
185             {
186                 _listeners[i].lifeCycleStopping(this);
187             }
188         }
189     }
190 
191     private void setStopped()
192     {
193         _state = __STOPPED;
194         if (_listeners != null)
195         {
196             for (int i = 0; i < _listeners.length; i++)
197             {
198                 _listeners[i].lifeCycleStopped(this);
199             }
200         }
201     }
202 
203     private void setFailed(Throwable error)
204     {
205         _state = __FAILED;
206         if (_listeners != null)
207         {
208             for (int i = 0; i < _listeners.length; i++)
209             {
210                 _listeners[i].lifeCycleFailure(this,error);
211             }
212         }
213     }
214 
215     
216 }