001: /*
002: * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/util/InstanceSupport.java,v 1.5 2001/10/11 23:30:58 craigmcc Exp $
003: * $Revision: 1.5 $
004: * $Date: 2001/10/11 23:30:58 $
005: *
006: * ====================================================================
007: *
008: * The Apache Software License, Version 1.1
009: *
010: * Copyright (c) 1999 The Apache Software Foundation. All rights
011: * reserved.
012: *
013: * Redistribution and use in source and binary forms, with or without
014: * modification, are permitted provided that the following conditions
015: * are met:
016: *
017: * 1. Redistributions of source code must retain the above copyright
018: * notice, this list of conditions and the following disclaimer.
019: *
020: * 2. Redistributions in binary form must reproduce the above copyright
021: * notice, this list of conditions and the following disclaimer in
022: * the documentation and/or other materials provided with the
023: * distribution.
024: *
025: * 3. The end-user documentation included with the redistribution, if
026: * any, must include the following acknowlegement:
027: * "This product includes software developed by the
028: * Apache Software Foundation (http://www.apache.org/)."
029: * Alternately, this acknowlegement may appear in the software itself,
030: * if and wherever such third-party acknowlegements normally appear.
031: *
032: * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
033: * Foundation" must not be used to endorse or promote products derived
034: * from this software without prior written permission. For written
035: * permission, please contact apache@apache.org.
036: *
037: * 5. Products derived from this software may not be called "Apache"
038: * nor may "Apache" appear in their names without prior written
039: * permission of the Apache Group.
040: *
041: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
042: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
043: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
044: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
045: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
046: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
047: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
048: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
049: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
050: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
051: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
052: * SUCH DAMAGE.
053: * ====================================================================
054: *
055: * This software consists of voluntary contributions made by many
056: * individuals on behalf of the Apache Software Foundation. For more
057: * information on the Apache Software Foundation, please see
058: * <http://www.apache.org/>.
059: *
060: * [Additional notices, if required by prior licensing conditions]
061: *
062: */
063:
064: package org.apache.catalina.util;
065:
066: import javax.servlet.Filter;
067: import javax.servlet.Servlet;
068: import javax.servlet.ServletRequest;
069: import javax.servlet.ServletResponse;
070: import org.apache.catalina.InstanceEvent;
071: import org.apache.catalina.InstanceListener;
072: import org.apache.catalina.Wrapper;
073:
074: /**
075: * Support class to assist in firing InstanceEvent notifications to
076: * registered InstanceListeners.
077: *
078: * @author Craig R. McClanahan
079: * @version $Id: InstanceSupport.java,v 1.5 2001/10/11 23:30:58 craigmcc Exp $
080: */
081:
082: public final class InstanceSupport {
083:
084: // ----------------------------------------------------------- Constructors
085:
086: /**
087: * Construct a new InstanceSupport object associated with the specified
088: * Instance component.
089: *
090: * @param lifecycle The Instance component that will be the source
091: * of events that we fire
092: */
093: public InstanceSupport(Wrapper wrapper) {
094:
095: super ();
096: this .wrapper = wrapper;
097:
098: }
099:
100: // ----------------------------------------------------- Instance Variables
101:
102: /**
103: * The set of registered InstanceListeners for event notifications.
104: */
105: private InstanceListener listeners[] = new InstanceListener[0];
106:
107: /**
108: * The source component for instance events that we will fire.
109: */
110: private Wrapper wrapper = null;
111:
112: // ------------------------------------------------------------- Properties
113:
114: /**
115: * Return the Wrapper with which we are associated.
116: */
117: public Wrapper getWrapper() {
118:
119: return (this .wrapper);
120:
121: }
122:
123: // --------------------------------------------------------- Public Methods
124:
125: /**
126: * Add a lifecycle event listener to this component.
127: *
128: * @param listener The listener to add
129: */
130: public void addInstanceListener(InstanceListener listener) {
131:
132: synchronized (listeners) {
133: InstanceListener results[] = new InstanceListener[listeners.length + 1];
134: for (int i = 0; i < listeners.length; i++)
135: results[i] = listeners[i];
136: results[listeners.length] = listener;
137: listeners = results;
138: }
139:
140: }
141:
142: /**
143: * Notify all lifecycle event listeners that a particular event has
144: * occurred for this Container. The default implementation performs
145: * this notification synchronously using the calling thread.
146: *
147: * @param type Event type
148: * @param filter The relevant Filter for this event
149: */
150: public void fireInstanceEvent(String type, Filter filter) {
151:
152: if (listeners.length == 0)
153: return;
154:
155: InstanceEvent event = new InstanceEvent(wrapper, filter, type);
156: InstanceListener interested[] = null;
157: synchronized (listeners) {
158: interested = (InstanceListener[]) listeners.clone();
159: }
160: for (int i = 0; i < interested.length; i++)
161: interested[i].instanceEvent(event);
162:
163: }
164:
165: /**
166: * Notify all lifecycle event listeners that a particular event has
167: * occurred for this Container. The default implementation performs
168: * this notification synchronously using the calling thread.
169: *
170: * @param type Event type
171: * @param filter The relevant Filter for this event
172: * @param exception Exception that occurred
173: */
174: public void fireInstanceEvent(String type, Filter filter,
175: Throwable exception) {
176:
177: if (listeners.length == 0)
178: return;
179:
180: InstanceEvent event = new InstanceEvent(wrapper, filter, type,
181: exception);
182: InstanceListener interested[] = null;
183: synchronized (listeners) {
184: interested = (InstanceListener[]) listeners.clone();
185: }
186: for (int i = 0; i < interested.length; i++)
187: interested[i].instanceEvent(event);
188:
189: }
190:
191: /**
192: * Notify all lifecycle event listeners that a particular event has
193: * occurred for this Container. The default implementation performs
194: * this notification synchronously using the calling thread.
195: *
196: * @param type Event type
197: * @param filter The relevant Filter for this event
198: * @param request The servlet request we are processing
199: * @param response The servlet response we are processing
200: */
201: public void fireInstanceEvent(String type, Filter filter,
202: ServletRequest request, ServletResponse response) {
203:
204: if (listeners.length == 0)
205: return;
206:
207: InstanceEvent event = new InstanceEvent(wrapper, filter, type,
208: request, response);
209: InstanceListener interested[] = null;
210: synchronized (listeners) {
211: interested = (InstanceListener[]) listeners.clone();
212: }
213: for (int i = 0; i < interested.length; i++)
214: interested[i].instanceEvent(event);
215:
216: }
217:
218: /**
219: * Notify all lifecycle event listeners that a particular event has
220: * occurred for this Container. The default implementation performs
221: * this notification synchronously using the calling thread.
222: *
223: * @param type Event type
224: * @param filter The relevant Filter for this event
225: * @param request The servlet request we are processing
226: * @param response The servlet response we are processing
227: * @param exception Exception that occurred
228: */
229: public void fireInstanceEvent(String type, Filter filter,
230: ServletRequest request, ServletResponse response,
231: Throwable exception) {
232:
233: if (listeners.length == 0)
234: return;
235:
236: InstanceEvent event = new InstanceEvent(wrapper, filter, type,
237: request, response, exception);
238: InstanceListener interested[] = null;
239: synchronized (listeners) {
240: interested = (InstanceListener[]) listeners.clone();
241: }
242: for (int i = 0; i < interested.length; i++)
243: interested[i].instanceEvent(event);
244:
245: }
246:
247: /**
248: * Notify all lifecycle event listeners that a particular event has
249: * occurred for this Container. The default implementation performs
250: * this notification synchronously using the calling thread.
251: *
252: * @param type Event type
253: * @param servlet The relevant Servlet for this event
254: */
255: public void fireInstanceEvent(String type, Servlet servlet) {
256:
257: if (listeners.length == 0)
258: return;
259:
260: InstanceEvent event = new InstanceEvent(wrapper, servlet, type);
261: InstanceListener interested[] = null;
262: synchronized (listeners) {
263: interested = (InstanceListener[]) listeners.clone();
264: }
265: for (int i = 0; i < interested.length; i++)
266: interested[i].instanceEvent(event);
267:
268: }
269:
270: /**
271: * Notify all lifecycle event listeners that a particular event has
272: * occurred for this Container. The default implementation performs
273: * this notification synchronously using the calling thread.
274: *
275: * @param type Event type
276: * @param servlet The relevant Servlet for this event
277: * @param exception Exception that occurred
278: */
279: public void fireInstanceEvent(String type, Servlet servlet,
280: Throwable exception) {
281:
282: if (listeners.length == 0)
283: return;
284:
285: InstanceEvent event = new InstanceEvent(wrapper, servlet, type,
286: exception);
287: InstanceListener interested[] = null;
288: synchronized (listeners) {
289: interested = (InstanceListener[]) listeners.clone();
290: }
291: for (int i = 0; i < interested.length; i++)
292: interested[i].instanceEvent(event);
293:
294: }
295:
296: /**
297: * Notify all lifecycle event listeners that a particular event has
298: * occurred for this Container. The default implementation performs
299: * this notification synchronously using the calling thread.
300: *
301: * @param type Event type
302: * @param servlet The relevant Servlet for this event
303: * @param request The servlet request we are processing
304: * @param response The servlet response we are processing
305: */
306: public void fireInstanceEvent(String type, Servlet servlet,
307: ServletRequest request, ServletResponse response) {
308:
309: if (listeners.length == 0)
310: return;
311:
312: InstanceEvent event = new InstanceEvent(wrapper, servlet, type,
313: request, response);
314: InstanceListener interested[] = null;
315: synchronized (listeners) {
316: interested = (InstanceListener[]) listeners.clone();
317: }
318: for (int i = 0; i < interested.length; i++)
319: interested[i].instanceEvent(event);
320:
321: }
322:
323: /**
324: * Notify all lifecycle event listeners that a particular event has
325: * occurred for this Container. The default implementation performs
326: * this notification synchronously using the calling thread.
327: *
328: * @param type Event type
329: * @param servlet The relevant Servlet for this event
330: * @param request The servlet request we are processing
331: * @param response The servlet response we are processing
332: * @param exception Exception that occurred
333: */
334: public void fireInstanceEvent(String type, Servlet servlet,
335: ServletRequest request, ServletResponse response,
336: Throwable exception) {
337:
338: if (listeners.length == 0)
339: return;
340:
341: InstanceEvent event = new InstanceEvent(wrapper, servlet, type,
342: request, response, exception);
343: InstanceListener interested[] = null;
344: synchronized (listeners) {
345: interested = (InstanceListener[]) listeners.clone();
346: }
347: for (int i = 0; i < interested.length; i++)
348: interested[i].instanceEvent(event);
349:
350: }
351:
352: /**
353: * Remove a lifecycle event listener from this component.
354: *
355: * @param listener The listener to remove
356: */
357: public void removeInstanceListener(InstanceListener listener) {
358:
359: synchronized (listeners) {
360: int n = -1;
361: for (int i = 0; i < listeners.length; i++) {
362: if (listeners[i] == listener) {
363: n = i;
364: break;
365: }
366: }
367: if (n < 0)
368: return;
369: InstanceListener results[] = new InstanceListener[listeners.length - 1];
370: int j = 0;
371: for (int i = 0; i < listeners.length; i++) {
372: if (i != n)
373: results[j++] = listeners[i];
374: }
375: listeners = results;
376: }
377:
378: }
379:
380: }
|