2 // ========================================================================
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.util.component;
21 import java.lang.management.ManagementFactory;
22 import java.util.concurrent.CopyOnWriteArrayList;
24 import org.eclipse.jetty.util.annotation.ManagedAttribute;
25 import org.eclipse.jetty.util.annotation.ManagedObject;
26 import org.eclipse.jetty.util.log.Log;
27 import org.eclipse.jetty.util.log.Logger;
30 * Basic implementation of the life cycle interface for components.
32 @ManagedObject("Abstract Implementation of LifeCycle")
33 public abstract class AbstractLifeCycle implements LifeCycle
35 private static final Logger LOG = Log.getLogger(AbstractLifeCycle.class);
37 public static final String STOPPED="STOPPED";
38 public static final String FAILED="FAILED";
39 public static final String STARTING="STARTING";
40 public static final String STARTED="STARTED";
41 public static final String STOPPING="STOPPING";
42 public static final String RUNNING="RUNNING";
44 private final CopyOnWriteArrayList<LifeCycle.Listener> _listeners=new CopyOnWriteArrayList<LifeCycle.Listener>();
45 private final Object _lock = new Object();
46 private final int __FAILED = -1, __STOPPED = 0, __STARTING = 1, __STARTED = 2, __STOPPING = 3;
47 private volatile int _state = __STOPPED;
48 private long _stopTimeout = 30000;
50 protected void doStart() throws Exception
54 protected void doStop() throws Exception
59 public final void start() throws Exception
65 if (_state == __STARTED || _state == __STARTING)
80 public final void stop() throws Exception
86 if (_state == __STOPPING || _state == __STOPPED)
101 public boolean isRunning()
103 final int state = _state;
105 return state == __STARTED || state == __STARTING;
109 public boolean isStarted()
111 return _state == __STARTED;
115 public boolean isStarting()
117 return _state == __STARTING;
121 public boolean isStopping()
123 return _state == __STOPPING;
127 public boolean isStopped()
129 return _state == __STOPPED;
133 public boolean isFailed()
135 return _state == __FAILED;
139 public void addLifeCycleListener(LifeCycle.Listener listener)
141 _listeners.add(listener);
145 public void removeLifeCycleListener(LifeCycle.Listener listener)
147 _listeners.remove(listener);
150 @ManagedAttribute(value="Lifecycle State for this instance", readonly=true)
151 public String getState()
155 case __FAILED: return FAILED;
156 case __STARTING: return STARTING;
157 case __STARTED: return STARTED;
158 case __STOPPING: return STOPPING;
159 case __STOPPED: return STOPPED;
164 public static String getState(LifeCycle lc)
166 if (lc.isStarting()) return STARTING;
167 if (lc.isStarted()) return STARTED;
168 if (lc.isStopping()) return STOPPING;
169 if (lc.isStopped()) return STOPPED;
173 private void setStarted()
176 if (LOG.isDebugEnabled())
178 LOG.debug(STARTED+" @{}ms {}",ManagementFactory.getRuntimeMXBean().getUptime(),this);
179 for (Listener listener : _listeners)
180 listener.lifeCycleStarted(this);
183 private void setStarting()
185 LOG.debug("starting {}",this);
187 for (Listener listener : _listeners)
188 listener.lifeCycleStarting(this);
191 private void setStopping()
193 LOG.debug("stopping {}",this);
195 for (Listener listener : _listeners)
196 listener.lifeCycleStopping(this);
199 private void setStopped()
202 LOG.debug("{} {}",STOPPED,this);
203 for (Listener listener : _listeners)
204 listener.lifeCycleStopped(this);
207 private void setFailed(Throwable th)
210 LOG.warn(FAILED+" " + this+": "+th,th);
211 for (Listener listener : _listeners)
212 listener.lifeCycleFailure(this,th);
215 @ManagedAttribute(value="The stop timeout in milliseconds")
216 public long getStopTimeout()
221 public void setStopTimeout(long stopTimeout)
223 this._stopTimeout = stopTimeout;
226 public static abstract class AbstractLifeCycleListener implements LifeCycle.Listener
228 @Override public void lifeCycleFailure(LifeCycle event, Throwable cause) {}
229 @Override public void lifeCycleStarted(LifeCycle event) {}
230 @Override public void lifeCycleStarting(LifeCycle event) {}
231 @Override public void lifeCycleStopped(LifeCycle event) {}
232 @Override public void lifeCycleStopping(LifeCycle event) {}