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 java.util.concurrent.CopyOnWriteArrayList;
17  
18  import org.eclipse.jetty.util.LazyList;
19  import org.eclipse.jetty.util.log.Log;
20  
21  /**
22   * Basic implementation of the life cycle interface for components.
23   * 
24   * 
25   */
26  public abstract class AbstractLifeCycle implements LifeCycle
27  {
28      public static final String STOPPED="STOPPED";
29      public static final String FAILED="FAILED";
30      public static final String STARTING="STARTING";
31      public static final String STARTED="STARTED";
32      public static final String STOPPING="STOPPING";
33      public static final String RUNNING="RUNNING";
34      
35      private final Object _lock = new Object();
36      private final int __FAILED = -1, __STOPPED = 0, __STARTING = 1, __STARTED = 2, __STOPPING = 3;
37      private volatile int _state = __STOPPED;
38      
39      protected final CopyOnWriteArrayList<LifeCycle.Listener> _listeners=new CopyOnWriteArrayList<LifeCycle.Listener>();
40  
41      protected void doStart() throws Exception
42      {
43      }
44  
45      protected void doStop() throws Exception
46      {
47      }
48  
49      public final void start() throws Exception
50      {
51          synchronized (_lock)
52          {
53              try
54              {
55                  if (_state == __STARTED || _state == __STARTING)
56                      return;
57                  setStarting();
58                  doStart();
59                  setStarted();
60              }
61              catch (Exception e)
62              {
63                  setFailed(e);
64                  throw e;
65              }
66              catch (Error e)
67              {
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                  setStopped();
85              }
86              catch (Exception e)
87              {
88                  setFailed(e);
89                  throw e;
90              }
91              catch (Error e)
92              {
93                  setFailed(e);
94                  throw e;
95              }
96          }
97      }
98  
99      public boolean isRunning()
100     {
101         return _state == __STARTED || _state == __STARTING;
102     }
103 
104     public boolean isStarted()
105     {
106         return _state == __STARTED;
107     }
108 
109     public boolean isStarting()
110     {
111         return _state == __STARTING;
112     }
113 
114     public boolean isStopping()
115     {
116         return _state == __STOPPING;
117     }
118 
119     public boolean isStopped()
120     {
121         return _state == __STOPPED;
122     }
123 
124     public boolean isFailed()
125     {
126         return _state == __FAILED;
127     }
128 
129     public void addLifeCycleListener(LifeCycle.Listener listener)
130     {
131         _listeners.add(listener);
132     }
133 
134     public void removeLifeCycleListener(LifeCycle.Listener listener)
135     {
136         _listeners.remove(listener);
137     }
138     
139     public String getState()
140     {
141         switch(_state)
142         {
143             case __FAILED: return FAILED;
144             case __STARTING: return STARTING;
145             case __STARTED: return STARTED;
146             case __STOPPING: return STOPPING;
147             case __STOPPED: return STOPPED;
148         }
149         return null;
150     }
151     
152     public static String getState(LifeCycle lc)
153     {
154         if (lc.isStarting()) return STARTING;
155         if (lc.isStarted()) return STARTED;
156         if (lc.isStopping()) return STOPPING;
157         if (lc.isStopped()) return STOPPED;
158         return FAILED;
159     }
160 
161     private void setStarted()
162     {
163         _state = __STARTED;
164         Log.debug(STARTED+" {}",this);
165         for (Listener listener : _listeners)
166             listener.lifeCycleStarted(this);
167     }
168 
169     private void setStarting()
170     {
171         Log.debug("starting {}",this);
172         _state = __STARTING;
173         for (Listener listener : _listeners)
174             listener.lifeCycleStarting(this);
175     }
176 
177     private void setStopping()
178     {
179         Log.debug("stopping {}",this);
180         _state = __STOPPING;
181         for (Listener listener : _listeners)
182             listener.lifeCycleStopping(this);
183     }
184 
185     private void setStopped()
186     {
187         _state = __STOPPED;
188         Log.debug(STOPPED+" {}",this);
189         for (Listener listener : _listeners)
190             listener.lifeCycleStopped(this);
191     }
192 
193     private void setFailed(Throwable th)
194     {
195         _state = __FAILED;
196         Log.warn(FAILED+" " + this+": "+th);
197         Log.debug(th);
198         for (Listener listener : _listeners)
199             listener.lifeCycleFailure(this,th);
200     }
201 
202     public static abstract class AbstractLifeCycleListener implements LifeCycle.Listener
203     {
204         public void lifeCycleFailure(LifeCycle event, Throwable cause) {}
205         public void lifeCycleStarted(LifeCycle event) {}
206         public void lifeCycleStarting(LifeCycle event) {}
207         public void lifeCycleStopped(LifeCycle event) {}
208         public void lifeCycleStopping(LifeCycle event) {}
209     }
210 }