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 org.apache.catalina.Lifecycle;
020: import org.apache.catalina.LifecycleEvent;
021: import org.apache.catalina.LifecycleListener;
022:
023: /**
024: * Support class to assist in firing LifecycleEvent notifications to
025: * registered LifecycleListeners.
026: *
027: * @author Craig R. McClanahan
028: * @version $Id: LifecycleSupport.java,v 1.2 2004/02/27 14:58:50 yoavs Exp $
029: */
030:
031: public final class LifecycleSupport {
032:
033: // ----------------------------------------------------------- Constructors
034:
035: /**
036: * Construct a new LifecycleSupport object associated with the specified
037: * Lifecycle component.
038: *
039: * @param lifecycle The Lifecycle component that will be the source
040: * of events that we fire
041: */
042: public LifecycleSupport(Lifecycle lifecycle) {
043:
044: super ();
045: this .lifecycle = lifecycle;
046:
047: }
048:
049: // ----------------------------------------------------- Instance Variables
050:
051: /**
052: * The source component for lifecycle events that we will fire.
053: */
054: private Lifecycle lifecycle = null;
055:
056: /**
057: * The set of registered LifecycleListeners for event notifications.
058: */
059: private LifecycleListener listeners[] = new LifecycleListener[0];
060:
061: // --------------------------------------------------------- Public Methods
062:
063: /**
064: * Add a lifecycle event listener to this component.
065: *
066: * @param listener The listener to add
067: */
068: public void addLifecycleListener(LifecycleListener listener) {
069:
070: synchronized (listeners) {
071: LifecycleListener results[] = new LifecycleListener[listeners.length + 1];
072: for (int i = 0; i < listeners.length; i++)
073: results[i] = listeners[i];
074: results[listeners.length] = listener;
075: listeners = results;
076: }
077:
078: }
079:
080: /**
081: * Get the lifecycle listeners associated with this lifecycle. If this
082: * Lifecycle has no listeners registered, a zero-length array is returned.
083: */
084: public LifecycleListener[] findLifecycleListeners() {
085:
086: return listeners;
087:
088: }
089:
090: /**
091: * Notify all lifecycle event listeners that a particular event has
092: * occurred for this Container. The default implementation performs
093: * this notification synchronously using the calling thread.
094: *
095: * @param type Event type
096: * @param data Event data
097: */
098: public void fireLifecycleEvent(String type, Object data) {
099:
100: LifecycleEvent event = new LifecycleEvent(lifecycle, type, data);
101: LifecycleListener interested[] = null;
102: synchronized (listeners) {
103: interested = (LifecycleListener[]) listeners.clone();
104: }
105: for (int i = 0; i < interested.length; i++)
106: interested[i].lifecycleEvent(event);
107:
108: }
109:
110: /**
111: * Remove a lifecycle event listener from this component.
112: *
113: * @param listener The listener to remove
114: */
115: public void removeLifecycleListener(LifecycleListener listener) {
116:
117: synchronized (listeners) {
118: int n = -1;
119: for (int i = 0; i < listeners.length; i++) {
120: if (listeners[i] == listener) {
121: n = i;
122: break;
123: }
124: }
125: if (n < 0)
126: return;
127: LifecycleListener results[] = new LifecycleListener[listeners.length - 1];
128: int j = 0;
129: for (int i = 0; i < listeners.length; i++) {
130: if (i != n)
131: results[j++] = listeners[i];
132: }
133: listeners = results;
134: }
135:
136: }
137:
138: }
|