FileDocCategorySizeDatePackage
LifecycleSupport.javaAPI DocGlassfish v2 API5607Fri May 04 22:32:30 BST 2007org.apache.catalina.util

LifecycleSupport

public final class LifecycleSupport extends Object
Support class to assist in firing LifecycleEvent notifications to registered LifecycleListeners.
author
Craig R. McClanahan
version
$Id: LifecycleSupport.java,v 1.3 2007/05/05 05:32:30 tcfujii Exp $

Fields Summary
private org.apache.catalina.Lifecycle
lifecycle
The source component for lifecycle events that we will fire.
private org.apache.catalina.LifecycleListener[]
listeners
The set of registered LifecycleListeners for event notifications.
Constructors Summary
public LifecycleSupport(org.apache.catalina.Lifecycle lifecycle)
Construct a new LifecycleSupport object associated with the specified Lifecycle component.

param
lifecycle The Lifecycle component that will be the source of events that we fire


        super();
        this.lifecycle = lifecycle;

    
Methods Summary
public voidaddLifecycleListener(org.apache.catalina.LifecycleListener listener)
Add a lifecycle event listener to this component.

param
listener The listener to add



    // --------------------------------------------------------- Public Methods


                       
        

      synchronized (listeners) {
          LifecycleListener results[] =
            new LifecycleListener[listeners.length + 1];
          for (int i = 0; i < listeners.length; i++)
              results[i] = listeners[i];
          results[listeners.length] = listener;
          listeners = results;
      }

    
public org.apache.catalina.LifecycleListener[]findLifecycleListeners()
Get the lifecycle listeners associated with this lifecycle. If this Lifecycle has no listeners registered, a zero-length array is returned.


        return listeners;

    
public voidfireLifecycleEvent(java.lang.String type, java.lang.Object data)
Notify all lifecycle event listeners that a particular event has occurred for this Container. The default implementation performs this notification synchronously using the calling thread.

param
type Event type
param
data Event data


        LifecycleEvent event = new LifecycleEvent(lifecycle, type, data);
        LifecycleListener interested[] = null;
        synchronized (listeners) {
            interested = (LifecycleListener[]) listeners.clone();
        }
        for (int i = 0; i < interested.length; i++)
            interested[i].lifecycleEvent(event);

    
public voidremoveLifecycleListener(org.apache.catalina.LifecycleListener listener)
Remove a lifecycle event listener from this component.

param
listener The listener to remove


        synchronized (listeners) {
            int n = -1;
            for (int i = 0; i < listeners.length; i++) {
                if (listeners[i] == listener) {
                    n = i;
                    break;
                }
            }
            if (n < 0)
                return;
            LifecycleListener results[] =
              new LifecycleListener[listeners.length - 1];
            int j = 0;
            for (int i = 0; i < listeners.length; i++) {
                if (i != n)
                    results[j++] = listeners[i];
            }
            listeners = results;
        }