Save This Page
Home » apache-tomcat-6.0.16-src » org.apache » catalina » util » [javadoc | source]
    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    * 
    9    *      http://www.apache.org/licenses/LICENSE-2.0
   10    * 
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   
   18   
   19   package org.apache.catalina.util;
   20   
   21   
   22   import org.apache.catalina.Lifecycle;
   23   import org.apache.catalina.LifecycleEvent;
   24   import org.apache.catalina.LifecycleListener;
   25   
   26   
   27   /**
   28    * Support class to assist in firing LifecycleEvent notifications to
   29    * registered LifecycleListeners.
   30    *
   31    * @author Craig R. McClanahan
   32    * @version $Id: LifecycleSupport.java 510482 2007-02-22 12:34:36Z remm $
   33    */
   34   
   35   public final class LifecycleSupport {
   36   
   37   
   38       // ----------------------------------------------------------- Constructors
   39   
   40   
   41       /**
   42        * Construct a new LifecycleSupport object associated with the specified
   43        * Lifecycle component.
   44        *
   45        * @param lifecycle The Lifecycle component that will be the source
   46        *  of events that we fire
   47        */
   48       public LifecycleSupport(Lifecycle lifecycle) {
   49   
   50           super();
   51           this.lifecycle = lifecycle;
   52   
   53       }
   54   
   55   
   56       // ----------------------------------------------------- Instance Variables
   57   
   58   
   59       /**
   60        * The source component for lifecycle events that we will fire.
   61        */
   62       private Lifecycle lifecycle = null;
   63   
   64   
   65       /**
   66        * The set of registered LifecycleListeners for event notifications.
   67        */
   68       private LifecycleListener listeners[] = new LifecycleListener[0];
   69   
   70   
   71       // --------------------------------------------------------- Public Methods
   72   
   73   
   74       /**
   75        * Add a lifecycle event listener to this component.
   76        *
   77        * @param listener The listener to add
   78        */
   79       public void addLifecycleListener(LifecycleListener listener) {
   80   
   81         synchronized (listeners) {
   82             LifecycleListener results[] =
   83               new LifecycleListener[listeners.length + 1];
   84             for (int i = 0; i < listeners.length; i++)
   85                 results[i] = listeners[i];
   86             results[listeners.length] = listener;
   87             listeners = results;
   88         }
   89   
   90       }
   91   
   92   
   93       /**
   94        * Get the lifecycle listeners associated with this lifecycle. If this 
   95        * Lifecycle has no listeners registered, a zero-length array is returned.
   96        */
   97       public LifecycleListener[] findLifecycleListeners() {
   98   
   99           return listeners;
  100   
  101       }
  102   
  103   
  104       /**
  105        * Notify all lifecycle event listeners that a particular event has
  106        * occurred for this Container.  The default implementation performs
  107        * this notification synchronously using the calling thread.
  108        *
  109        * @param type Event type
  110        * @param data Event data
  111        */
  112       public void fireLifecycleEvent(String type, Object data) {
  113   
  114           LifecycleEvent event = new LifecycleEvent(lifecycle, type, data);
  115           LifecycleListener interested[] = listeners;
  116           for (int i = 0; i < interested.length; i++)
  117               interested[i].lifecycleEvent(event);
  118   
  119       }
  120   
  121   
  122       /**
  123        * Remove a lifecycle event listener from this component.
  124        *
  125        * @param listener The listener to remove
  126        */
  127       public void removeLifecycleListener(LifecycleListener listener) {
  128   
  129           synchronized (listeners) {
  130               int n = -1;
  131               for (int i = 0; i < listeners.length; i++) {
  132                   if (listeners[i] == listener) {
  133                       n = i;
  134                       break;
  135                   }
  136               }
  137               if (n < 0)
  138                   return;
  139               LifecycleListener results[] =
  140                 new LifecycleListener[listeners.length - 1];
  141               int j = 0;
  142               for (int i = 0; i < listeners.length; i++) {
  143                   if (i != n)
  144                       results[j++] = listeners[i];
  145               }
  146               listeners = results;
  147           }
  148   
  149       }
  150   
  151   
  152   }

Save This Page
Home » apache-tomcat-6.0.16-src » org.apache » catalina » util » [javadoc | source]