KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > util > InstanceSupport


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 implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18
19 package org.apache.catalina.util;
20
21
22 import javax.servlet.Filter JavaDoc;
23 import javax.servlet.Servlet JavaDoc;
24 import javax.servlet.ServletRequest JavaDoc;
25 import javax.servlet.ServletResponse JavaDoc;
26 import org.apache.catalina.InstanceEvent;
27 import org.apache.catalina.InstanceListener;
28 import org.apache.catalina.Wrapper;
29
30
31 /**
32  * Support class to assist in firing InstanceEvent notifications to
33  * registered InstanceListeners.
34  *
35  * @author Craig R. McClanahan
36  * @version $Id: InstanceSupport.java 467222 2006-10-24 03:17:11Z markt $
37  */

38
39 public final class InstanceSupport {
40
41
42     // ----------------------------------------------------------- Constructors
43

44
45     /**
46      * Construct a new InstanceSupport object associated with the specified
47      * Instance component.
48      *
49      * @param wrapper The component that will be the source
50      * of events that we fire
51      */

52     public InstanceSupport(Wrapper wrapper) {
53
54         super();
55         this.wrapper = wrapper;
56
57     }
58
59
60     // ----------------------------------------------------- Instance Variables
61

62
63     /**
64      * The set of registered InstanceListeners for event notifications.
65      */

66     private InstanceListener listeners[] = new InstanceListener[0];
67
68
69     /**
70      * The source component for instance events that we will fire.
71      */

72     private Wrapper wrapper = null;
73
74
75     // ------------------------------------------------------------- Properties
76

77
78     /**
79      * Return the Wrapper with which we are associated.
80      */

81     public Wrapper getWrapper() {
82
83         return (this.wrapper);
84
85     }
86
87
88     // --------------------------------------------------------- Public Methods
89

90
91     /**
92      * Add a lifecycle event listener to this component.
93      *
94      * @param listener The listener to add
95      */

96     public void addInstanceListener(InstanceListener listener) {
97
98       synchronized (listeners) {
99           InstanceListener results[] =
100             new InstanceListener[listeners.length + 1];
101           for (int i = 0; i < listeners.length; i++)
102               results[i] = listeners[i];
103           results[listeners.length] = listener;
104           listeners = results;
105       }
106
107     }
108
109
110     /**
111      * Notify all lifecycle event listeners that a particular event has
112      * occurred for this Container. The default implementation performs
113      * this notification synchronously using the calling thread.
114      *
115      * @param type Event type
116      * @param filter The relevant Filter for this event
117      */

118     public void fireInstanceEvent(String JavaDoc type, Filter JavaDoc filter) {
119
120         if (listeners.length == 0)
121             return;
122
123         InstanceEvent event = new InstanceEvent(wrapper, filter, type);
124         InstanceListener interested[] = null;
125         synchronized (listeners) {
126             interested = (InstanceListener[]) listeners.clone();
127         }
128         for (int i = 0; i < interested.length; i++)
129             interested[i].instanceEvent(event);
130
131     }
132
133
134     /**
135      * Notify all lifecycle event listeners that a particular event has
136      * occurred for this Container. The default implementation performs
137      * this notification synchronously using the calling thread.
138      *
139      * @param type Event type
140      * @param filter The relevant Filter for this event
141      * @param exception Exception that occurred
142      */

143     public void fireInstanceEvent(String JavaDoc type, Filter JavaDoc filter,
144                                   Throwable JavaDoc exception) {
145
146         if (listeners.length == 0)
147             return;
148
149         InstanceEvent event = new InstanceEvent(wrapper, filter, type,
150                                                 exception);
151         InstanceListener interested[] = null;
152         synchronized (listeners) {
153             interested = (InstanceListener[]) listeners.clone();
154         }
155         for (int i = 0; i < interested.length; i++)
156             interested[i].instanceEvent(event);
157
158     }
159
160
161     /**
162      * Notify all lifecycle event listeners that a particular event has
163      * occurred for this Container. The default implementation performs
164      * this notification synchronously using the calling thread.
165      *
166      * @param type Event type
167      * @param filter The relevant Filter for this event
168      * @param request The servlet request we are processing
169      * @param response The servlet response we are processing
170      */

171     public void fireInstanceEvent(String JavaDoc type, Filter JavaDoc filter,
172                                   ServletRequest JavaDoc request,
173                                   ServletResponse JavaDoc response) {
174
175         if (listeners.length == 0)
176             return;
177
178         InstanceEvent event = new InstanceEvent(wrapper, filter, type,
179                                                 request, response);
180         InstanceListener interested[] = null;
181         synchronized (listeners) {
182             interested = (InstanceListener[]) listeners.clone();
183         }
184         for (int i = 0; i < interested.length; i++)
185             interested[i].instanceEvent(event);
186
187     }
188
189
190     /**
191      * Notify all lifecycle event listeners that a particular event has
192      * occurred for this Container. The default implementation performs
193      * this notification synchronously using the calling thread.
194      *
195      * @param type Event type
196      * @param filter The relevant Filter for this event
197      * @param request The servlet request we are processing
198      * @param response The servlet response we are processing
199      * @param exception Exception that occurred
200      */

201     public void fireInstanceEvent(String JavaDoc type, Filter JavaDoc filter,
202                                   ServletRequest JavaDoc request,
203                                   ServletResponse JavaDoc response,
204                                   Throwable JavaDoc exception) {
205
206         if (listeners.length == 0)
207             return;
208
209         InstanceEvent event = new InstanceEvent(wrapper, filter, type,
210                                                 request, response, exception);
211         InstanceListener interested[] = null;
212         synchronized (listeners) {
213             interested = (InstanceListener[]) listeners.clone();
214         }
215         for (int i = 0; i < interested.length; i++)
216             interested[i].instanceEvent(event);
217
218     }
219
220
221     /**
222      * Notify all lifecycle event listeners that a particular event has
223      * occurred for this Container. The default implementation performs
224      * this notification synchronously using the calling thread.
225      *
226      * @param type Event type
227      * @param servlet The relevant Servlet for this event
228      */

229     public void fireInstanceEvent(String JavaDoc type, Servlet JavaDoc servlet) {
230
231         if (listeners.length == 0)
232             return;
233
234         InstanceEvent event = new InstanceEvent(wrapper, servlet, type);
235         InstanceListener interested[] = null;
236         synchronized (listeners) {
237             interested = (InstanceListener[]) listeners.clone();
238         }
239         for (int i = 0; i < interested.length; i++)
240             interested[i].instanceEvent(event);
241
242     }
243
244
245     /**
246      * Notify all lifecycle event listeners that a particular event has
247      * occurred for this Container. The default implementation performs
248      * this notification synchronously using the calling thread.
249      *
250      * @param type Event type
251      * @param servlet The relevant Servlet for this event
252      * @param exception Exception that occurred
253      */

254     public void fireInstanceEvent(String JavaDoc type, Servlet JavaDoc servlet,
255                                   Throwable JavaDoc exception) {
256
257         if (listeners.length == 0)
258             return;
259
260         InstanceEvent event = new InstanceEvent(wrapper, servlet, type,
261                                                 exception);
262         InstanceListener interested[] = null;
263         synchronized (listeners) {
264             interested = (InstanceListener[]) listeners.clone();
265         }
266         for (int i = 0; i < interested.length; i++)
267             interested[i].instanceEvent(event);
268
269     }
270
271
272     /**
273      * Notify all lifecycle event listeners that a particular event has
274      * occurred for this Container. The default implementation performs
275      * this notification synchronously using the calling thread.
276      *
277      * @param type Event type
278      * @param servlet The relevant Servlet for this event
279      * @param request The servlet request we are processing
280      * @param response The servlet response we are processing
281      */

282     public void fireInstanceEvent(String JavaDoc type, Servlet JavaDoc servlet,
283                                   ServletRequest JavaDoc request,
284                                   ServletResponse JavaDoc response) {
285
286         if (listeners.length == 0)
287             return;
288
289         InstanceEvent event = new InstanceEvent(wrapper, servlet, type,
290                                                 request, response);
291         InstanceListener interested[] = null;
292         synchronized (listeners) {
293             interested = (InstanceListener[]) listeners.clone();
294         }
295         for (int i = 0; i < interested.length; i++)
296             interested[i].instanceEvent(event);
297
298     }
299
300
301     /**
302      * Notify all lifecycle event listeners that a particular event has
303      * occurred for this Container. The default implementation performs
304      * this notification synchronously using the calling thread.
305      *
306      * @param type Event type
307      * @param servlet The relevant Servlet for this event
308      * @param request The servlet request we are processing
309      * @param response The servlet response we are processing
310      * @param exception Exception that occurred
311      */

312     public void fireInstanceEvent(String JavaDoc type, Servlet JavaDoc servlet,
313                                   ServletRequest JavaDoc request,
314                                   ServletResponse JavaDoc response,
315                                   Throwable JavaDoc exception) {
316
317         if (listeners.length == 0)
318             return;
319
320         InstanceEvent event = new InstanceEvent(wrapper, servlet, type,
321                                                 request, response, exception);
322         InstanceListener interested[] = null;
323         synchronized (listeners) {
324             interested = (InstanceListener[]) listeners.clone();
325         }
326         for (int i = 0; i < interested.length; i++)
327             interested[i].instanceEvent(event);
328
329     }
330
331
332     /**
333      * Remove a lifecycle event listener from this component.
334      *
335      * @param listener The listener to remove
336      */

337     public void removeInstanceListener(InstanceListener listener) {
338
339         synchronized (listeners) {
340             int n = -1;
341             for (int i = 0; i < listeners.length; i++) {
342                 if (listeners[i] == listener) {
343                     n = i;
344                     break;
345                 }
346             }
347             if (n < 0)
348                 return;
349             InstanceListener results[] =
350               new InstanceListener[listeners.length - 1];
351             int j = 0;
352             for (int i = 0; i < listeners.length; i++) {
353                 if (i != n)
354                     results[j++] = listeners[i];
355             }
356             listeners = results;
357         }
358
359     }
360
361
362 }
363
Popular Tags