KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > excalibur > instrument > manager > http > server > AbstractLogEnabledInstrumentableStartable


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19
20 package org.apache.excalibur.instrument.manager.http.server;
21
22 import org.apache.avalon.framework.activity.Startable;
23
24 import org.apache.excalibur.instrument.AbstractLogEnabledInstrumentable;
25
26 /**
27  *
28  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
29  * @version $Revision: 1.4 $
30  */

31 abstract class AbstractLogEnabledInstrumentableStartable
32     extends AbstractLogEnabledInstrumentable
33     implements Startable, Runnable JavaDoc
34 {
35     /** Reference to the worker thread. */
36     private Thread JavaDoc m_runner;
37     
38     /** Flag set when the m_runner thread has been asked to stop. */
39     private boolean m_runnerStop = false;
40     
41     /*---------------------------------------------------------------
42      * Constructors
43      *-------------------------------------------------------------*/

44     /**
45      * Creates a new AbstractLogEnabledInstrumentableStartable.
46      */

47     public AbstractLogEnabledInstrumentableStartable()
48     {
49         super();
50     }
51     
52     /*---------------------------------------------------------------
53      * Startable Methods
54      *-------------------------------------------------------------*/

55     /**
56      * Starts the runner thread.
57      *
58      * @throws Exception If there are any problems.
59      */

60     public void start()
61         throws Exception JavaDoc
62     {
63         getLogger().debug( "Starting..." );
64         
65         m_runner = new Thread JavaDoc( this, getInstrumentableName() + "_runner" );
66         m_runner.start();
67     }
68     
69     /**
70      * Stops the runner thread, blocking until it has stopped.
71      *
72      * @throws Exception If there are any problems stopping the component.
73      */

74     public void stop()
75         throws Exception JavaDoc
76     {
77         getLogger().debug( "Stopping." );
78         
79         Thread JavaDoc runner = m_runner;
80         m_runnerStop = true;
81         if ( runner != null )
82         {
83             runner.interrupt();
84         }
85         
86         // Give the user code a change to stop cleanly.
87
try
88         {
89             stopRunner();
90         }
91         catch ( Throwable JavaDoc t )
92         {
93             getLogger().error( "Encountered a problem while stopping the component.", t );
94         }
95         
96         getLogger().debug( "Waiting for runner thread to stop." );
97         synchronized ( this )
98         {
99             while ( m_runner != null )
100             {
101                 try
102                 {
103                     // Wait to be notified that the thread has exited.
104
this.wait();
105                 }
106                 catch ( InterruptedException JavaDoc e )
107                 {
108                     // Ignore
109
}
110             }
111         }
112         getLogger().debug( "Stopped." );
113     }
114     
115     /*---------------------------------------------------------------
116      * Runable Methods
117      *-------------------------------------------------------------*/

118     /**
119      * Run method which is responsible for launching the runner method and
120      * handling the shutdown cycle.
121      */

122     public void run()
123     {
124         if ( Thread.currentThread() != m_runner )
125         {
126             throw new IllegalStateException JavaDoc( "Private method." );
127         }
128         
129         getLogger().debug( "Runner thread started." );
130         
131         try
132         {
133             try
134             {
135                 runner();
136             }
137             catch ( Throwable JavaDoc t )
138             {
139                 getLogger().warn(
140                     "The runner method threw an uncaught exception, runner is terminating,", t );
141             }
142         }
143         finally
144         {
145             synchronized ( this )
146             {
147                 m_runner = null;
148                 
149                 // Wake up the stop method if it is waiting for the runner to stop.
150
this.notify();
151             }
152
153             getLogger().debug( "Runner thread stopped." );
154         }
155     }
156     
157     /*---------------------------------------------------------------
158      * Methods
159      *-------------------------------------------------------------*/

160     /**
161      * Called when the component is being stopped, the isStopping method will
162      * always return true when this method is called. This version of the
163      * method does nothing.
164      *
165      * @throws Exception If there are any problems
166      */

167     protected void stopRunner()
168         throws Exception JavaDoc
169     {
170     }
171     
172     /**
173      * Runner method that will be called when the component is started.
174      * The method must monitor the isStopping() method and make sure
175      * that it returns in a timely manner when the isStopping() method
176      * returns true.
177      */

178     protected abstract void runner();
179     
180     /**
181      * Returns true when the component is in the process of being stopped.
182      *
183      * @return True when the component is in the process of being stopped.
184      */

185     public boolean isStopping()
186     {
187         return m_runnerStop;
188     }
189 }
190
Popular Tags