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