KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > util > LifeCycleThread


1 // ========================================================================
2
// $Id: LifeCycleThread.java,v 1.9 2005/08/13 00:01:28 gregwilkins Exp $
3
// Copyright 1999-2004 Mort Bay Consulting Pty. Ltd.
4
// ------------------------------------------------------------------------
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
// ========================================================================
15

16 package org.mortbay.util;
17
18 import java.io.InterruptedIOException JavaDoc;
19
20 import org.apache.commons.logging.Log;
21 import org.mortbay.log.LogFactory;
22
23 /* ------------------------------------------------------------ */
24 /** Base Thread class implementing LifeCycle.
25  *
26  * @version $Revision: 1.9 $
27  * @author Greg Wilkins (gregw)
28  */

29 public abstract class LifeCycleThread implements LifeCycle, Runnable JavaDoc
30 {
31     private static Log log = LogFactory.getLog(LifeCycleThread.class);
32
33     private boolean _running;
34     private boolean _daemon ;
35     private Thread JavaDoc _thread;
36     
37     /* ------------------------------------------------------------ */
38     public boolean isDaemon()
39     {
40         return _daemon;
41     }
42     
43     /* ------------------------------------------------------------ */
44     public void setDaemon(boolean d)
45     {
46         _daemon = d;
47     }
48     
49     /* ------------------------------------------------------------ */
50     public Thread JavaDoc getThread()
51     {
52         return _thread;
53     }
54     
55     /* ------------------------------------------------------------ */
56     public boolean isStarted()
57     {
58         return _running;
59     }
60
61     /* ------------------------------------------------------------ */
62     public synchronized void start()
63         throws Exception JavaDoc
64     {
65         if (_running)
66         {
67             log.debug("Already started");
68             return;
69         }
70         _running=true;
71         if (_thread==null)
72         {
73             _thread=new Thread JavaDoc(this);
74             _thread.setDaemon(_daemon);
75         }
76         _thread.start();
77     }
78     
79     /* ------------------------------------------------------------ */
80     /**
81      */

82     public synchronized void stop()
83         throws InterruptedException JavaDoc
84     {
85         _running=false;
86         if (_thread!=null)
87         {
88             _thread.interrupt();
89             _thread.join();
90         }
91     }
92     
93
94     /* ------------------------------------------------------------ */
95     /**
96      */

97     public final void run()
98     {
99         try
100         {
101             while(_running)
102             {
103                 try
104                 {
105                     loop();
106                 }
107                 catch(InterruptedException JavaDoc e)
108                 {
109                     LogSupport.ignore(log,e);
110                 }
111                 catch(InterruptedIOException JavaDoc e)
112                 {
113                     LogSupport.ignore(log,e);
114                 }
115                 catch(Exception JavaDoc e)
116                 {
117                     if (exception(e))
118                         break;
119                 }
120                 catch(Error JavaDoc e)
121                 {
122                     if (error(e))
123                         break;
124                 }
125             }
126         }
127         finally
128         {
129             _running=false;
130         }
131     }
132
133     /* ------------------------------------------------------------ */
134     /** Handle exception from loop.
135      * @param e The exception
136      * @return True of the loop should continue;
137      */

138     public boolean exception(Exception JavaDoc e)
139     {
140         log.warn(LogSupport.EXCEPTION,e);
141         return true;
142     }
143     
144     /* ------------------------------------------------------------ */
145     /** Handle error from loop.
146      * @param e The exception
147      * @return True of the loop should continue;
148      */

149     public boolean error(Error JavaDoc e)
150     {
151         log.warn(LogSupport.EXCEPTION,e);
152         return true;
153     }
154     
155     /* ------------------------------------------------------------ */
156     /**
157      * @exception InterruptedException
158      * @exception InterruptedIOException
159      */

160     public abstract void loop()
161     throws InterruptedException JavaDoc,
162            InterruptedIOException JavaDoc,
163            Exception JavaDoc;
164     
165 }
166
Popular Tags