1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mortbay.component;
17
18 import org.mortbay.log.Log;
19
20
21
22
23
24 public abstract class AbstractLifeCycle implements LifeCycle
25 {
26 private Object _lock=new Object();
27 private final int FAILED=-1,STOPPED=0,STARTING=1,STARTED=2,STOPPING=3;
28 private transient int _state=STOPPED;
29 protected void doStart() throws Exception {}
30 protected void doStop() throws Exception {}
31
32
33 public final void start() throws Exception
34 {
35 synchronized (_lock)
36 {
37 try
38 {
39 if (_state==STARTED || _state==STARTING)
40 return;
41 _state=STARTING;
42 doStart();
43 Log.debug("started {}",this);
44 _state=STARTED;
45 }
46 catch (Exception e)
47 {
48 Log.warn("failed "+this,e);
49 _state=FAILED;
50 throw e;
51 }
52 catch(Error e)
53 {
54 Log.warn("failed "+this,e);
55 _state=FAILED;
56 throw e;
57 }
58 }
59 }
60
61 public final void stop() throws Exception
62 {
63 synchronized (_lock)
64 {
65 try
66 {
67 if (_state==STOPPING || _state==STOPPED)
68 return;
69 _state=STOPPING;
70 doStop();
71 Log.debug("stopped {}",this);
72 _state=STOPPED;
73 }
74 catch (Exception e)
75 {
76 Log.warn("failed "+this,e);
77 _state=FAILED;
78 throw e;
79 }
80 catch(Error e)
81 {
82 Log.warn("failed "+this,e);
83 _state=FAILED;
84 throw e;
85 }
86 }
87 }
88
89 public boolean isRunning()
90 {
91 return _state==STARTED || _state==STARTING;
92 }
93
94 public boolean isStarted()
95 {
96 return _state==STARTED;
97 }
98
99 public boolean isStarting()
100 {
101 return _state==STARTING;
102 }
103
104 public boolean isStopping()
105 {
106 return _state==STOPPING;
107 }
108
109 public boolean isStopped()
110 {
111 return _state==STOPPED;
112 }
113
114 public boolean isFailed()
115 {
116 return _state==FAILED;
117 }
118 }