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