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                  setStarted();
57              }
58              catch (Exception e)
59              {
60                  setFailed(e);
61                  throw e;
62              }
63              catch (Error e)
64              {
65                  setFailed(e);
66                  throw e;
67              }
68          }
69      }
70  
71      public final void stop() throws Exception
72      {
73          synchronized (_lock)
74          {
75              try
76              {
77                  if (_state == __STOPPING || _state == __STOPPED)
78                      return;
79                  setStopping();
80                  doStop();
81                  setStopped();
82              }
83              catch (Exception e)
84              {
85                  setFailed(e);
86                  throw e;
87              }
88              catch (Error e)
89              {
90                  setFailed(e);
91                  throw e;
92              }
93          }
94      }
95  
96      public boolean isRunning()
97      {
98          return _state == __STARTED || _state == __STARTING;
99      }
100 
101     public boolean isStarted()
102     {
103         return _state == __STARTED;
104     }
105 
106     public boolean isStarting()
107     {
108         return _state == __STARTING;
109     }
110 
111     public boolean isStopping()
112     {
113         return _state == __STOPPING;
114     }
115 
116     public boolean isStopped()
117     {
118         return _state == __STOPPED;
119     }
120 
121     public boolean isFailed()
122     {
123         return _state == __FAILED;
124     }
125 
126     public void addLifeCycleListener(LifeCycle.Listener listener)
127     {
128         _listeners = (LifeCycle.Listener[])LazyList.addToArray(_listeners,listener,LifeCycle.Listener.class);
129     }
130 
131     public void removeLifeCycleListener(LifeCycle.Listener listener)
132     {
133         _listeners = (LifeCycle.Listener[])LazyList.removeFromArray(_listeners,listener);
134     }
135     
136     public String getState()
137     {
138         switch(_state)
139         {
140             case __FAILED: return FAILED;
141             case __STARTING: return STARTING;
142             case __STARTED: return STARTED;
143             case __STOPPING: return STOPPING;
144             case __STOPPED: return STOPPED;
145         }
146         return null;
147     }
148 
149     private void setStarted()
150     {
151         Log.debug(STARTED+" {}",this);
152         _state = __STARTED;
153         if (_listeners != null)
154         {
155             for (int i = 0; i < _listeners.length; i++)
156             {
157                 _listeners[i].lifeCycleStarted(this);
158             }
159         }
160     }
161 
162     private void setStarting()
163     {
164         Log.debug("Starting {}",this);
165         _state = __STARTING;
166         if (_listeners != null)
167         {
168             for (int i = 0; i < _listeners.length; i++)
169             {
170                 _listeners[i].lifeCycleStarting(this);
171             }
172         }
173     }
174 
175     private void setStopping()
176     {
177         _state = __STOPPING;
178         if (_listeners != null)
179         {
180             for (int i = 0; i < _listeners.length; i++)
181             {
182                 _listeners[i].lifeCycleStopping(this);
183             }
184         }
185     }
186 
187     private void setStopped()
188     {
189         Log.debug(STOPPED+" {}",this);
190         _state = __STOPPED;
191         if (_listeners != null)
192         {
193             for (int i = 0; i < _listeners.length; i++)
194             {
195                 _listeners[i].lifeCycleStopped(this);
196             }
197         }
198     }
199 
200     private void setFailed(Throwable th)
201     {
202         Log.warn(FAILED+" " + this+": "+th);
203         Log.debug(th);
204         _state = __FAILED;
205         if (_listeners != null)
206         {
207             for (int i = 0; i < _listeners.length; i++)
208             {
209                 _listeners[i].lifeCycleFailure(this,th);
210             }
211         }
212     }
213 
214     public static abstract class AbstractLifeCycleListener implements LifeCycle.Listener
215     {
216         public void lifeCycleFailure(LifeCycle event, Throwable cause) {}
217         public void lifeCycleStarted(LifeCycle event) {}
218         public void lifeCycleStarting(LifeCycle event) {}
219         public void lifeCycleStopped(LifeCycle event) {}
220         public void lifeCycleStopping(LifeCycle event) {}
221     }
222 }