KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > phoenix > components > application > ListenerSupport


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

8 package org.apache.avalon.phoenix.components.application;
9
10 import org.apache.avalon.phoenix.ApplicationEvent;
11 import org.apache.avalon.phoenix.ApplicationListener;
12 import org.apache.avalon.phoenix.BlockEvent;
13 import org.apache.avalon.phoenix.BlockListener;
14 import org.apache.avalon.phoenix.metadata.BlockMetaData;
15 import org.apache.avalon.phoenix.metadata.SarMetaData;
16
17 /**
18  * Manage a set of {@link ApplicationListener} objects and propogate
19  * {@link ApplicationEvent} notifications to these listeners. Not all
20  * events pass an Applicationevent parameter.
21  *
22  * @author <a HREF="mailto:peter at apache.org">Peter Donald</a>
23  * @author <a HREF="mailto:Paul_Hammant@yahoo.com">Paul Hammant</a>
24  */

25 final class ListenerSupport
26 {
27     //Set of block listeners. Must be accessed from synchronized code
28
private BlockListener[] m_blockListeners = new BlockListener[ 0 ];
29
30     //Set of listeners. Must be accessed from synchronized code
31
private ApplicationListener[] m_listeners = new ApplicationListener[ 0 ];
32
33     /**
34      * fire Event indicating that the Application represented
35      * by specified metaData is starting.
36      *
37      * @param metaData the metaData
38      */

39     void fireApplicationStartingEvent( final SarMetaData metaData )
40         throws Exception JavaDoc
41     {
42         final ApplicationEvent event =
43             new ApplicationEvent( metaData.getName(), metaData );
44         applicationStarting( event );
45     }
46
47     /**
48      * fire Event indicating that Block represented by
49      * specific entry has been added.
50      *
51      * @param entry the entry
52      */

53     void fireBlockAddedEvent( final BlockEntry entry )
54     {
55         blockAdded( createEvent( entry ) );
56     }
57
58     /**
59      * fire Event indicating that Block represented by
60      * specific entry is being removed.
61      *
62      * @param entry the entry
63      */

64     void fireBlockRemovedEvent( final BlockEntry entry )
65     {
66         blockRemoved( createEvent( entry ) );
67     }
68
69     /**
70      * Utility method to create an event for a
71      * specific entry.
72      *
73      * @param entry the entry
74      * @return the new event
75      */

76     private BlockEvent createEvent( final BlockEntry entry )
77     {
78         final BlockMetaData metaData = entry.getMetaData();
79         final BlockEvent event =
80             new BlockEvent( metaData.getName(),
81                             entry.getProxy(),
82                             metaData.getBlockInfo() );
83         return event;
84     }
85
86     /**
87      * Add a ApplicationListener to those requiring notification of
88      * {@link ApplicationEvent}s.
89      *
90      * @param listener the ApplicationListener
91      */

92     public synchronized void addApplicationListener( final ApplicationListener listener )
93     {
94         final ApplicationListener[] listeners = new ApplicationListener[ 1 + m_listeners.length ];
95         System.arraycopy( m_listeners, 0, listeners, 0, m_listeners.length );
96         listeners[ m_listeners.length ] = listener;
97         m_listeners = listeners;
98     }
99
100     /**
101      * Remove a ApplicationListener from those requiring notification of
102      * {@link ApplicationEvent}s.
103      *
104      * @param listener the ApplicationListener
105      */

106     public synchronized void removeApplicationListener( final ApplicationListener listener )
107     {
108         int index = 0;
109         while( index < m_listeners.length )
110         {
111             if( m_listeners[ index ] == listener )
112             {
113                 break;
114             }
115             index++;
116         }
117
118         if( m_listeners.length != index )
119         {
120             final ApplicationListener[] listeners =
121                 new ApplicationListener[ m_listeners.length - 1 ];
122             System.arraycopy( m_listeners, 0, listeners, 0, index );
123             final int length = m_listeners.length - index - 1;
124             System.arraycopy( m_listeners, index + 1, listeners, index, length );
125         }
126     }
127
128     /**
129      * Add a BlockListener to those requiring notification of
130      * {@link BlockEvent}s.
131      *
132      * @param listener the BlockListener
133      */

134     public synchronized void addBlockListener( final BlockListener listener )
135     {
136         final BlockListener[] listeners = new BlockListener[ 1 + m_blockListeners.length ];
137         System.arraycopy( m_blockListeners, 0, listeners, 0, m_blockListeners.length );
138         listeners[ m_listeners.length ] = listener;
139         m_blockListeners = listeners;
140     }
141
142     /**
143      * Remove a BlockListener from those requiring notification of
144      * {@link BlockEvent}s.
145      *
146      * @param listener the BlockListener
147      */

148     public synchronized void removeBlockListener( final BlockListener listener )
149     {
150         int index = 0;
151         while( index < m_blockListeners.length )
152         {
153             if( m_blockListeners[ index ] == listener )
154             {
155                 break;
156             }
157             index++;
158         }
159
160         if( m_blockListeners.length != index )
161         {
162             final BlockListener[] listeners =
163                 new BlockListener[ m_blockListeners.length - 1 ];
164             System.arraycopy( m_blockListeners, 0, listeners, 0, index );
165             final int length = m_blockListeners.length - index - 1;
166             System.arraycopy( m_blockListeners, index + 1, listeners, index, length );
167         }
168     }
169
170     /**
171      * Notification that the application is starting
172      *
173      * @param event the ApplicationEvent
174      */

175     public synchronized void applicationStarting( final ApplicationEvent event )
176         throws Exception JavaDoc
177     {
178         for( int i = 0; i < m_listeners.length; i++ )
179         {
180             m_listeners[ i ].applicationStarting( event );
181         }
182     }
183
184     /**
185      * Notification that the application has started.
186      *
187      */

188     public synchronized void applicationStarted()
189     {
190         for( int i = 0; i < m_listeners.length; i++ )
191         {
192             m_listeners[ i ].applicationStarted();
193         }
194     }
195
196     /**
197      * Notification that the application is stopping
198      *
199      */

200     public synchronized void applicationStopping()
201     {
202         for( int i = 0; i < m_listeners.length; i++ )
203         {
204             m_listeners[ i ].applicationStopping();
205         }
206     }
207
208     /**
209      * Notification that the application has stopped
210      *
211      */

212     public synchronized void applicationStopped()
213     {
214         for( int i = 0; i < m_listeners.length; i++ )
215         {
216             m_listeners[ i ].applicationStopped();
217         }
218     }
219
220     /**
221      * Notification that the application has failed
222      *
223      */

224     public synchronized void applicationFailure( Exception JavaDoc causeOfFailure )
225     {
226         for( int i = 0; i < m_listeners.length; i++ )
227         {
228             m_listeners[ i ].applicationFailure( causeOfFailure );
229         }
230     }
231
232     /**
233      * Notification that a block has just been added
234      * to Server Application.
235      *
236      * @param event the BlockEvent
237      */

238     public synchronized void blockAdded( final BlockEvent event )
239     {
240         for( int i = 0; i < m_listeners.length; i++ )
241         {
242             m_listeners[ i ].blockAdded( event );
243         }
244
245         //Now notify the plain BlockListeners
246
for( int i = 0; i < m_blockListeners.length; i++ )
247         {
248             m_blockListeners[ i ].blockAdded( event );
249         }
250     }
251
252     /**
253      * Notification that a block is just about to be
254      * removed from Server Application.
255      *
256      * @param event the BlockEvent
257      */

258     public synchronized void blockRemoved( final BlockEvent event )
259     {
260         for( int i = 0; i < m_listeners.length; i++ )
261         {
262             m_listeners[ i ].blockRemoved( event );
263         }
264
265         //Now notify the plain BlockListeners
266
for( int i = 0; i < m_blockListeners.length; i++ )
267         {
268             m_blockListeners[ i ].blockRemoved( event );
269         }
270     }
271 }
272
Popular Tags