KickJava   Java API By Example, From Geeks To Geeks.

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


1 // ========================================================================
2
// $Id: Container.java,v 1.4 2005/08/13 08:49:59 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.IOException JavaDoc;
19 import java.io.Serializable JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.EventListener JavaDoc;
23
24 import org.apache.commons.logging.Log;
25 import org.mortbay.log.LogFactory;
26 import org.mortbay.http.HttpContext;
27
28
29
30 /* ------------------------------------------------------------ */
31 /** Abstract Container.
32  * Provides base handling for LifeCycle and Component events.
33  *
34  * @version $Id: Container.java,v 1.4 2005/08/13 08:49:59 gregwilkins Exp $
35  * @author Greg Wilkins (gregw)
36  */

37 public abstract class Container implements LifeCycle,EventProvider,Serializable JavaDoc
38 {
39     private static Log log = LogFactory.getLog(Container.class);
40     
41     private Object JavaDoc _eventListeners;
42     private Object JavaDoc _components;
43     
44     private transient boolean _started;
45     private transient boolean _starting;
46     private transient boolean _stopping;
47
48     
49     /* ------------------------------------------------------------ */
50     /** Start the server.
51      * Generate LifeCycleEvents for starting and started either side of a call to doStart
52      */

53     public synchronized final void start()
54         throws Exception JavaDoc
55     {
56         if (_started || _starting)
57             return;
58         
59         _starting=true;
60         
61         if (log.isDebugEnabled()) log.debug("Starting "+this);
62         LifeCycleEvent event = new LifeCycleEvent(this);
63         for(int i=0;i<LazyList.size(_eventListeners);i++)
64         {
65             EventListener JavaDoc listener=(EventListener JavaDoc)LazyList.get(_eventListeners,i);
66             if (listener instanceof LifeCycleListener)
67                 ((LifeCycleListener)listener).lifeCycleStarting(event);
68         }
69         
70         try
71         {
72             doStart();
73             _started=true;
74             log.info("Started "+this);
75             for(int i=0;i<LazyList.size(_eventListeners);i++)
76             {
77                 EventListener JavaDoc listener=(EventListener JavaDoc)LazyList.get(_eventListeners,i);
78                 if (listener instanceof LifeCycleListener)
79                     ((LifeCycleListener)listener).lifeCycleStarted(event);
80             }
81         }
82         catch(Throwable JavaDoc e)
83         {
84             LifeCycleEvent failed = new LifeCycleEvent(this,e);
85             for(int i=0;i<LazyList.size(_eventListeners);i++)
86             {
87                 EventListener JavaDoc listener=(EventListener JavaDoc)LazyList.get(_eventListeners,i);
88                 if (listener instanceof LifeCycleListener)
89                     ((LifeCycleListener)listener).lifeCycleFailure(event);
90             }
91             if (e instanceof Exception JavaDoc)
92                 throw (Exception JavaDoc)e;
93             if (e instanceof RuntimeException JavaDoc)
94                 throw (RuntimeException JavaDoc)e;
95             if (e instanceof Error JavaDoc)
96                 throw (Error JavaDoc)e;
97             log.warn(LogSupport.EXCEPTION, e);
98         }
99         finally
100         {
101             _starting=false;
102         }
103     }
104     
105
106     /* ------------------------------------------------------------ */
107     /** Do start operations.
108      * This abstract method is called by start
109      * to perform the actual start operations.
110      */

111     protected abstract void doStart()
112         throws Exception JavaDoc;
113     
114     /* ------------------------------------------------------------ */
115     public synchronized boolean isStarted()
116     {
117         return _started;
118     }
119     
120     /* ------------------------------------------------------------ */
121     protected synchronized boolean isStarting()
122     {
123         return _starting;
124     }
125     
126     /* ------------------------------------------------------------ */
127     protected synchronized boolean isStopping()
128     {
129         return _stopping;
130     }
131
132     /* ------------------------------------------------------------ */
133     /** Stop the container.
134      * Generate LifeCycleEvents for stopping and stopped either side of a call to doStop
135      */

136     public synchronized final void stop()
137         throws InterruptedException JavaDoc
138     {
139         if (!_started || _stopping)
140             return;
141         _stopping=true;
142
143         if (log.isDebugEnabled()) log.debug("Stopping "+this);
144         LifeCycleEvent event = new LifeCycleEvent(this);
145         for(int i=0;i<LazyList.size(_eventListeners);i++)
146         {
147             EventListener JavaDoc listener=(EventListener JavaDoc)LazyList.get(_eventListeners,i);
148             if (listener instanceof LifeCycleListener)
149                 ((LifeCycleListener)listener).lifeCycleStopping(event);
150         }
151         
152         try
153         {
154             doStop();
155             _started=false;
156             log.info("Stopped "+this);
157             for(int i=0;i<LazyList.size(_eventListeners);i++)
158             {
159                 EventListener JavaDoc listener=(EventListener JavaDoc)LazyList.get(_eventListeners,i);
160                 if (listener instanceof LifeCycleListener)
161                     ((LifeCycleListener)listener).lifeCycleStopped(event);
162             }
163         }
164         catch(Throwable JavaDoc e)
165         {
166             event = new LifeCycleEvent(this,e);
167             for(int i=0;i<LazyList.size(_eventListeners);i++)
168             {
169                 EventListener JavaDoc listener=(EventListener JavaDoc)LazyList.get(_eventListeners,i);
170                 if (listener instanceof LifeCycleListener)
171                     ((LifeCycleListener)listener).lifeCycleFailure(event);
172             }
173             if (e instanceof InterruptedException JavaDoc)
174                 throw (InterruptedException JavaDoc)e;
175             if (e instanceof RuntimeException JavaDoc)
176                 throw (RuntimeException JavaDoc)e;
177             if (e instanceof Error JavaDoc)
178                 throw (Error JavaDoc)e;
179             log.warn(LogSupport.EXCEPTION, e);
180         }
181         finally
182         {
183             _stopping=false;
184         }
185     }
186     
187     /* ------------------------------------------------------------ */
188     /** Do stop operations.
189      * This abstract method is called by stop
190      * to perform the actual stop operations.
191      */

192     protected abstract void doStop()
193         throws Exception JavaDoc;
194     
195     /* ------------------------------------------------------------ */
196     protected void addComponent(Object JavaDoc o)
197     {
198         if (!LazyList.contains(_components,o))
199         {
200             _components = LazyList.add(_components,o);
201             if(log.isDebugEnabled())log.debug("add component: "+o);
202             ComponentEvent event = new ComponentEvent(this,o);
203             for(int i=0;i<LazyList.size(_eventListeners);i++)
204             {
205                 EventListener JavaDoc listener=(EventListener JavaDoc)LazyList.get(_eventListeners,i);
206                 if (listener instanceof ComponentListener)
207                     ((ComponentListener)listener).addComponent(event);
208             }
209         }
210     }
211     
212     /* ------------------------------------------------------------ */
213     protected void removeComponent(Object JavaDoc o)
214     {
215         if (LazyList.contains(_components,o))
216         {
217             _components=LazyList.remove(_components, o);
218             if(log.isDebugEnabled())log.debug("remove component: "+o);
219             ComponentEvent event = new ComponentEvent(this,o);
220             for(int i=0;i<LazyList.size(_eventListeners);i++)
221             {
222                 EventListener JavaDoc listener=(EventListener JavaDoc)LazyList.get(_eventListeners,i);
223                 if (listener instanceof ComponentListener)
224                     ((ComponentListener)listener).removeComponent(event);
225             }
226         }
227     }
228
229     /* ------------------------------------------------------------ */
230     /** Add a server event listener.
231      * @param listener ComponentEventListener or LifeCycleEventListener
232      */

233     public void addEventListener(EventListener JavaDoc listener)
234         throws IllegalArgumentException JavaDoc
235     {
236        
237         if (_eventListeners==null)
238             _eventListeners=new ArrayList JavaDoc();
239         
240         if (listener instanceof ComponentListener ||
241             listener instanceof LifeCycleListener )
242         {
243             if(log.isDebugEnabled())log.debug("addEventListener: "+listener);
244             _eventListeners=LazyList.add(_eventListeners,listener);
245         }
246       
247     }
248     
249     /* ------------------------------------------------------------ */
250     public void removeEventListener(EventListener JavaDoc listener)
251     {
252         if(log.isDebugEnabled())log.debug("removeEventListener: "+listener);
253         _eventListeners=LazyList.remove(_eventListeners,listener);
254     }
255
256     /* ------------------------------------------------------------ */
257     /** Destroy a stopped server.
258      * Remove all components and send notifications to all event
259      * listeners. The HttpServer must be stopped before it can be destroyed.
260      */

261     public void destroy()
262     {
263         if (isStarted())
264             throw new IllegalStateException JavaDoc("Started");
265
266         if (_components!=null && _eventListeners!=null)
267         {
268             for (int c=0;c<LazyList.size(_components);c++)
269             {
270                 Object JavaDoc o=LazyList.get(_components,c);
271                 if (o instanceof HttpContext )
272                     ((HttpContext)o).destroy();
273                 
274                 ComponentEvent event = new ComponentEvent(this,o);
275                 for(int i=0;i<LazyList.size(_eventListeners);i++)
276                 {
277                     EventListener JavaDoc listener=(EventListener JavaDoc)LazyList.get(_eventListeners,i);
278                     if (listener instanceof ComponentListener)
279                         ((ComponentListener)listener).removeComponent(event);
280                 }
281             }
282         }
283         
284         _components=null;
285         _eventListeners=null;
286     }
287
288     /* ------------------------------------------------------------ */
289     public Collection JavaDoc getComponents()
290     {
291         return LazyList.getList(_eventListeners, false);
292     }
293
294     /* ------------------------------------------------------------ */
295     private void readObject(java.io.ObjectInputStream JavaDoc in)
296         throws IOException JavaDoc, ClassNotFoundException JavaDoc
297     {
298         in.defaultReadObject();
299         for (int c=0;c<LazyList.size(_components);c++)
300         {
301             Object JavaDoc o = LazyList.get(_components,c);
302             ComponentEvent event = new ComponentEvent(this,o);
303             for(int i=0;i<LazyList.size(_eventListeners);i++)
304             {
305                 EventListener JavaDoc listener=(EventListener JavaDoc)LazyList.get(_eventListeners,i);
306                 if (listener instanceof ComponentListener)
307                     ((ComponentListener)listener).addComponent(event);
308             }
309         }
310     }
311 }
312
Popular Tags