KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > loom > components > application > ListenerSupport


1 /* ====================================================================
2  * Loom Software License, version 1.1
3  *
4  * Copyright (c) 2003, Loom Group. All rights reserved.
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  *
17  * 3. Neither the name of the Loom Group nor the name "Loom" nor
18  * the names of its contributors may be used to endorse or promote
19  * products derived from this software without specific prior
20  * written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *
35  * ====================================================================
36  *
37  * Loom includes code from the Apache Software Foundation
38  *
39  * ====================================================================
40  * The Apache Software License, Version 1.1
41  *
42  * Copyright (c) 1997-2003 The Apache Software Foundation. All rights
43  * reserved.
44  *
45  * Redistribution and use in source and binary forms, with or without
46  * modification, are permitted provided that the following conditions
47  * are met:
48  *
49  * 1. Redistributions of source code must retain the above copyright
50  * notice, this list of conditions and the following disclaimer.
51  *
52  * 2. Redistributions in binary form must reproduce the above copyright
53  * notice, this list of conditions and the following disclaimer in
54  * the documentation and/or other materials provided with the
55  * distribution.
56  *
57  * 3. The end-user documentation included with the redistribution,
58  * if any, must include the following acknowledgment:
59  * "This product includes software developed by the
60  * Apache Software Foundation (http://www.apache.org/)."
61  * Alternately, this acknowledgment may appear in the software
62  * itself, if and wherever such third-party acknowledgments
63  * normally appear.
64  *
65  * 4. The names "Jakarta", "Avalon", and "Apache Software Foundation"
66  * must not be used to endorse or promote products derived from this
67  * software without prior written permission. For written
68  * permission, please contact apache@apache.org.
69  *
70  * 5. Products derived from this software may not be called "Apache",
71  * nor may "Apache" appear in their name, without prior written
72  * permission of the Apache Software Foundation.
73  *
74  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
75  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
76  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
77  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
78  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
79  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
80  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
81  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
82  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
83  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
84  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
85  * SUCH DAMAGE.
86  */

87 package org.codehaus.loom.components.application;
88
89 import org.apache.avalon.phoenix.ApplicationEvent;
90 import org.apache.avalon.phoenix.ApplicationListener;
91 import org.apache.avalon.phoenix.BlockEvent;
92 import org.apache.avalon.phoenix.BlockListener;
93 import org.apache.avalon.phoenix.metadata.SarMetaData;
94 import org.apache.avalon.phoenix.metainfo.BlockInfo;
95 import org.codehaus.loom.components.util.ComponentInfoConverter;
96
97 /**
98  * Manage a set of {@link ApplicationListener} objects and propogate {@link
99  * ApplicationEvent} notifications to these listeners. Not all events pass an
100  * Applicationevent parameter.
101  *
102  * @author Peter Donald
103  * @author <a HREF="mailto:Paul_Hammant@yahoo.com">Paul Hammant</a>
104  */

105 final class ListenerSupport
106 {
107     //Set of block listeners. Must be accessed from synchronized code
108
private BlockListener[] m_blockListeners = new BlockListener[ 0 ];
109
110     //Set of listeners. Must be accessed from synchronized code
111
private ApplicationListener[] m_listeners = new ApplicationListener[ 0 ];
112
113     /**
114      * fire Event indicating that the Application represented by specified
115      * metaData is starting.
116      *
117      * @param metaData the metaData
118      */

119     void fireApplicationStartingEvent( final SarMetaData metaData )
120         throws Exception JavaDoc
121     {
122         final ApplicationEvent event =
123             new ApplicationEvent( metaData.getName(), metaData );
124         applicationStarting( event );
125     }
126
127     /**
128      * fire Event indicating that Block represented by specific entry has been
129      * added.
130      *
131      * @param entry the entry
132      */

133     void fireBlockAddedEvent( final BlockEntry entry )
134     {
135         blockAdded( createEvent( entry ) );
136     }
137
138     /**
139      * fire Event indicating that Block represented by specific entry is being
140      * removed.
141      *
142      * @param entry the entry
143      */

144     void fireBlockRemovedEvent( final BlockEntry entry )
145     {
146         blockRemoved( createEvent( entry ) );
147     }
148
149     /**
150      * Utility method to create an event for a specific entry.
151      *
152      * @param entry the entry
153      * @return the new event
154      */

155     private BlockEvent createEvent( final BlockEntry entry )
156     {
157         final BlockInfo blockInfo =
158             ComponentInfoConverter.toBlockInfo( entry.getInfo() );
159         return new BlockEvent( entry.getName(),
160                                entry.getProxy(),
161                                blockInfo );
162     }
163
164     /**
165      * Add a ApplicationListener to those requiring notification of {@link
166      * ApplicationEvent}s.
167      *
168      * @param listener the ApplicationListener
169      */

170     public synchronized void addApplicationListener(
171         final ApplicationListener listener )
172     {
173         final ApplicationListener[] listeners = new ApplicationListener[ 1 +
174             m_listeners.length ];
175         System.arraycopy( m_listeners, 0, listeners, 0, m_listeners.length );
176         listeners[ m_listeners.length ] = listener;
177         m_listeners = listeners;
178     }
179
180     /**
181      * Remove a ApplicationListener from those requiring notification of {@link
182      * ApplicationEvent}s.
183      *
184      * @param listener the ApplicationListener
185      */

186     public synchronized void removeApplicationListener(
187         final ApplicationListener listener )
188     {
189         int index = 0;
190         while( index < m_listeners.length )
191         {
192             if( m_listeners[ index ] == listener )
193             {
194                 break;
195             }
196             index++;
197         }
198
199         if( m_listeners.length != index )
200         {
201             final ApplicationListener[] listeners =
202                 new ApplicationListener[ m_listeners.length - 1 ];
203             System.arraycopy( m_listeners, 0, listeners, 0, index );
204             final int length = m_listeners.length - index - 1;
205             System.arraycopy( m_listeners,
206                               index + 1,
207                               listeners,
208                               index,
209                               length );
210         }
211     }
212
213     /**
214      * Add a BlockListener to those requiring notification of {@link
215      * BlockEvent}s.
216      *
217      * @param listener the BlockListener
218      */

219     public synchronized void addBlockListener( final BlockListener listener )
220     {
221         final BlockListener[] listeners = new BlockListener[ 1 +
222             m_blockListeners.length ];
223         System.arraycopy( m_blockListeners,
224                           0,
225                           listeners,
226                           0,
227                           m_blockListeners.length );
228         listeners[ m_listeners.length ] = listener;
229         m_blockListeners = listeners;
230     }
231
232     /**
233      * Remove a BlockListener from those requiring notification of {@link
234      * BlockEvent}s.
235      *
236      * @param listener the BlockListener
237      */

238     public synchronized void removeBlockListener( final BlockListener listener )
239     {
240         int index = 0;
241         while( index < m_blockListeners.length )
242         {
243             if( m_blockListeners[ index ] == listener )
244             {
245                 break;
246             }
247             index++;
248         }
249
250         if( m_blockListeners.length != index )
251         {
252             final BlockListener[] listeners =
253                 new BlockListener[ m_blockListeners.length - 1 ];
254             System.arraycopy( m_blockListeners, 0, listeners, 0, index );
255             final int length = m_blockListeners.length - index - 1;
256             System.arraycopy( m_blockListeners,
257                               index + 1,
258                               listeners,
259                               index,
260                               length );
261         }
262     }
263
264     /**
265      * Notification that the application is starting
266      *
267      * @param event the ApplicationEvent
268      */

269     private synchronized void applicationStarting(
270         final ApplicationEvent event )
271         throws Exception JavaDoc
272     {
273         for( int i = 0; i < m_listeners.length; i++ )
274         {
275             m_listeners[ i ].applicationStarting( event );
276         }
277     }
278
279     /**
280      * Notification that the application has started.
281      */

282     public synchronized void applicationStarted()
283     {
284         for( int i = 0; i < m_listeners.length; i++ )
285         {
286             m_listeners[ i ].applicationStarted();
287         }
288     }
289
290     /**
291      * Notification that the application is stopping
292      */

293     public synchronized void applicationStopping()
294     {
295         for( int i = 0; i < m_listeners.length; i++ )
296         {
297             m_listeners[ i ].applicationStopping();
298         }
299     }
300
301     /**
302      * Notification that the application has stopped
303      */

304     public synchronized void applicationStopped()
305     {
306         for( int i = 0; i < m_listeners.length; i++ )
307         {
308             m_listeners[ i ].applicationStopped();
309         }
310     }
311
312     /**
313      * Notification that the application has failed
314      */

315     public synchronized void applicationFailure(
316         final Exception JavaDoc causeOfFailure )
317     {
318         for( int i = 0; i < m_listeners.length; i++ )
319         {
320             m_listeners[ i ].applicationFailure( causeOfFailure );
321         }
322     }
323
324     /**
325      * Notification that a block has just been added to Server Application.
326      *
327      * @param event the BlockEvent
328      */

329     private synchronized void blockAdded( final BlockEvent event )
330     {
331         for( int i = 0; i < m_listeners.length; i++ )
332         {
333             m_listeners[ i ].blockAdded( event );
334         }
335
336         //Now notify the plain BlockListeners
337
for( int i = 0; i < m_blockListeners.length; i++ )
338         {
339             m_blockListeners[ i ].blockAdded( event );
340         }
341     }
342
343     /**
344      * Notification that a block is just about to be removed from Server
345      * Application.
346      *
347      * @param event the BlockEvent
348      */

349     private synchronized void blockRemoved( final BlockEvent event )
350     {
351         for( int i = 0; i < m_listeners.length; i++ )
352         {
353             m_listeners[ i ].blockRemoved( event );
354         }
355
356         //Now notify the plain BlockListeners
357
for( int i = 0; i < m_blockListeners.length; i++ )
358         {
359             m_blockListeners[ i ].blockRemoved( event );
360         }
361     }
362 }
363
Popular Tags