001: /*
002: * Copyright 1999,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.catalina.util;
018:
019: import javax.servlet.Filter;
020: import javax.servlet.Servlet;
021: import javax.servlet.ServletRequest;
022: import javax.servlet.ServletResponse;
023: import org.apache.catalina.InstanceEvent;
024: import org.apache.catalina.InstanceListener;
025: import org.apache.catalina.Wrapper;
026:
027: /**
028: * Support class to assist in firing InstanceEvent notifications to
029: * registered InstanceListeners.
030: *
031: * @author Craig R. McClanahan
032: * @version $Id: InstanceSupport.java,v 1.2 2004/02/27 14:58:50 yoavs Exp $
033: */
034:
035: public final class InstanceSupport {
036:
037: // ----------------------------------------------------------- Constructors
038:
039: /**
040: * Construct a new InstanceSupport object associated with the specified
041: * Instance component.
042: *
043: * @param lifecycle The Instance component that will be the source
044: * of events that we fire
045: */
046: public InstanceSupport(Wrapper wrapper) {
047:
048: super ();
049: this .wrapper = wrapper;
050:
051: }
052:
053: // ----------------------------------------------------- Instance Variables
054:
055: /**
056: * The set of registered InstanceListeners for event notifications.
057: */
058: private InstanceListener listeners[] = new InstanceListener[0];
059:
060: /**
061: * The source component for instance events that we will fire.
062: */
063: private Wrapper wrapper = null;
064:
065: // ------------------------------------------------------------- Properties
066:
067: /**
068: * Return the Wrapper with which we are associated.
069: */
070: public Wrapper getWrapper() {
071:
072: return (this .wrapper);
073:
074: }
075:
076: // --------------------------------------------------------- Public Methods
077:
078: /**
079: * Add a lifecycle event listener to this component.
080: *
081: * @param listener The listener to add
082: */
083: public void addInstanceListener(InstanceListener listener) {
084:
085: synchronized (listeners) {
086: InstanceListener results[] = new InstanceListener[listeners.length + 1];
087: for (int i = 0; i < listeners.length; i++)
088: results[i] = listeners[i];
089: results[listeners.length] = listener;
090: listeners = results;
091: }
092:
093: }
094:
095: /**
096: * Notify all lifecycle event listeners that a particular event has
097: * occurred for this Container. The default implementation performs
098: * this notification synchronously using the calling thread.
099: *
100: * @param type Event type
101: * @param filter The relevant Filter for this event
102: */
103: public void fireInstanceEvent(String type, Filter filter) {
104:
105: if (listeners.length == 0)
106: return;
107:
108: InstanceEvent event = new InstanceEvent(wrapper, filter, type);
109: InstanceListener interested[] = null;
110: synchronized (listeners) {
111: interested = (InstanceListener[]) listeners.clone();
112: }
113: for (int i = 0; i < interested.length; i++)
114: interested[i].instanceEvent(event);
115:
116: }
117:
118: /**
119: * Notify all lifecycle event listeners that a particular event has
120: * occurred for this Container. The default implementation performs
121: * this notification synchronously using the calling thread.
122: *
123: * @param type Event type
124: * @param filter The relevant Filter for this event
125: * @param exception Exception that occurred
126: */
127: public void fireInstanceEvent(String type, Filter filter,
128: Throwable exception) {
129:
130: if (listeners.length == 0)
131: return;
132:
133: InstanceEvent event = new InstanceEvent(wrapper, filter, type,
134: exception);
135: InstanceListener interested[] = null;
136: synchronized (listeners) {
137: interested = (InstanceListener[]) listeners.clone();
138: }
139: for (int i = 0; i < interested.length; i++)
140: interested[i].instanceEvent(event);
141:
142: }
143:
144: /**
145: * Notify all lifecycle event listeners that a particular event has
146: * occurred for this Container. The default implementation performs
147: * this notification synchronously using the calling thread.
148: *
149: * @param type Event type
150: * @param filter The relevant Filter for this event
151: * @param request The servlet request we are processing
152: * @param response The servlet response we are processing
153: */
154: public void fireInstanceEvent(String type, Filter filter,
155: ServletRequest request, ServletResponse response) {
156:
157: if (listeners.length == 0)
158: return;
159:
160: InstanceEvent event = new InstanceEvent(wrapper, filter, type,
161: request, response);
162: InstanceListener interested[] = null;
163: synchronized (listeners) {
164: interested = (InstanceListener[]) listeners.clone();
165: }
166: for (int i = 0; i < interested.length; i++)
167: interested[i].instanceEvent(event);
168:
169: }
170:
171: /**
172: * Notify all lifecycle event listeners that a particular event has
173: * occurred for this Container. The default implementation performs
174: * this notification synchronously using the calling thread.
175: *
176: * @param type Event type
177: * @param filter The relevant Filter for this event
178: * @param request The servlet request we are processing
179: * @param response The servlet response we are processing
180: * @param exception Exception that occurred
181: */
182: public void fireInstanceEvent(String type, Filter filter,
183: ServletRequest request, ServletResponse response,
184: Throwable exception) {
185:
186: if (listeners.length == 0)
187: return;
188:
189: InstanceEvent event = new InstanceEvent(wrapper, filter, type,
190: request, response, exception);
191: InstanceListener interested[] = null;
192: synchronized (listeners) {
193: interested = (InstanceListener[]) listeners.clone();
194: }
195: for (int i = 0; i < interested.length; i++)
196: interested[i].instanceEvent(event);
197:
198: }
199:
200: /**
201: * Notify all lifecycle event listeners that a particular event has
202: * occurred for this Container. The default implementation performs
203: * this notification synchronously using the calling thread.
204: *
205: * @param type Event type
206: * @param servlet The relevant Servlet for this event
207: */
208: public void fireInstanceEvent(String type, Servlet servlet) {
209:
210: if (listeners.length == 0)
211: return;
212:
213: InstanceEvent event = new InstanceEvent(wrapper, servlet, type);
214: InstanceListener interested[] = null;
215: synchronized (listeners) {
216: interested = (InstanceListener[]) listeners.clone();
217: }
218: for (int i = 0; i < interested.length; i++)
219: interested[i].instanceEvent(event);
220:
221: }
222:
223: /**
224: * Notify all lifecycle event listeners that a particular event has
225: * occurred for this Container. The default implementation performs
226: * this notification synchronously using the calling thread.
227: *
228: * @param type Event type
229: * @param servlet The relevant Servlet for this event
230: * @param exception Exception that occurred
231: */
232: public void fireInstanceEvent(String type, Servlet servlet,
233: Throwable exception) {
234:
235: if (listeners.length == 0)
236: return;
237:
238: InstanceEvent event = new InstanceEvent(wrapper, servlet, type,
239: exception);
240: InstanceListener interested[] = null;
241: synchronized (listeners) {
242: interested = (InstanceListener[]) listeners.clone();
243: }
244: for (int i = 0; i < interested.length; i++)
245: interested[i].instanceEvent(event);
246:
247: }
248:
249: /**
250: * Notify all lifecycle event listeners that a particular event has
251: * occurred for this Container. The default implementation performs
252: * this notification synchronously using the calling thread.
253: *
254: * @param type Event type
255: * @param servlet The relevant Servlet for this event
256: * @param request The servlet request we are processing
257: * @param response The servlet response we are processing
258: */
259: public void fireInstanceEvent(String type, Servlet servlet,
260: ServletRequest request, ServletResponse response) {
261:
262: if (listeners.length == 0)
263: return;
264:
265: InstanceEvent event = new InstanceEvent(wrapper, servlet, type,
266: request, response);
267: InstanceListener interested[] = null;
268: synchronized (listeners) {
269: interested = (InstanceListener[]) listeners.clone();
270: }
271: for (int i = 0; i < interested.length; i++)
272: interested[i].instanceEvent(event);
273:
274: }
275:
276: /**
277: * Notify all lifecycle event listeners that a particular event has
278: * occurred for this Container. The default implementation performs
279: * this notification synchronously using the calling thread.
280: *
281: * @param type Event type
282: * @param servlet The relevant Servlet for this event
283: * @param request The servlet request we are processing
284: * @param response The servlet response we are processing
285: * @param exception Exception that occurred
286: */
287: public void fireInstanceEvent(String type, Servlet servlet,
288: ServletRequest request, ServletResponse response,
289: Throwable exception) {
290:
291: if (listeners.length == 0)
292: return;
293:
294: InstanceEvent event = new InstanceEvent(wrapper, servlet, type,
295: request, response, exception);
296: InstanceListener interested[] = null;
297: synchronized (listeners) {
298: interested = (InstanceListener[]) listeners.clone();
299: }
300: for (int i = 0; i < interested.length; i++)
301: interested[i].instanceEvent(event);
302:
303: }
304:
305: /**
306: * Remove a lifecycle event listener from this component.
307: *
308: * @param listener The listener to remove
309: */
310: public void removeInstanceListener(InstanceListener listener) {
311:
312: synchronized (listeners) {
313: int n = -1;
314: for (int i = 0; i < listeners.length; i++) {
315: if (listeners[i] == listener) {
316: n = i;
317: break;
318: }
319: }
320: if (n < 0)
321: return;
322: InstanceListener results[] = new InstanceListener[listeners.length - 1];
323: int j = 0;
324: for (int i = 0; i < listeners.length; i++) {
325: if (i != n)
326: results[j++] = listeners[i];
327: }
328: listeners = results;
329: }
330:
331: }
332:
333: }
|