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