KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > util > LifecycleSupport


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 467222 2006-10-24 03:17:11Z markt $
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 JavaDoc type, Object JavaDoc data) {
113
114         LifecycleEvent event = new LifecycleEvent(lifecycle, type, data);
115         LifecycleListener interested[] = null;
116         synchronized (listeners) {
117             interested = (LifecycleListener[]) listeners.clone();
118         }
119         for (int i = 0; i < interested.length; i++)
120             interested[i].lifecycleEvent(event);
121
122     }
123
124
125     /**
126      * Remove a lifecycle event listener from this component.
127      *
128      * @param listener The listener to remove
129      */

130     public void removeLifecycleListener(LifecycleListener listener) {
131
132         synchronized (listeners) {
133             int n = -1;
134             for (int i = 0; i < listeners.length; i++) {
135                 if (listeners[i] == listener) {
136                     n = i;
137                     break;
138                 }
139             }
140             if (n < 0)
141                 return;
142             LifecycleListener results[] =
143               new LifecycleListener[listeners.length - 1];
144             int j = 0;
145             for (int i = 0; i < listeners.length; i++) {
146                 if (i != n)
147                     results[j++] = listeners[i];
148             }
149             listeners = results;
150         }
151
152     }
153
154
155 }
156
Popular Tags