1 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 105 final class ListenerSupport 106 { 107 private BlockListener[] m_blockListeners = new BlockListener[ 0 ]; 109 110 private ApplicationListener[] m_listeners = new ApplicationListener[ 0 ]; 112 113 119 void fireApplicationStartingEvent( final SarMetaData metaData ) 120 throws Exception 121 { 122 final ApplicationEvent event = 123 new ApplicationEvent( metaData.getName(), metaData ); 124 applicationStarting( event ); 125 } 126 127 133 void fireBlockAddedEvent( final BlockEntry entry ) 134 { 135 blockAdded( createEvent( entry ) ); 136 } 137 138 144 void fireBlockRemovedEvent( final BlockEntry entry ) 145 { 146 blockRemoved( createEvent( entry ) ); 147 } 148 149 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 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 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 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 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 269 private synchronized void applicationStarting( 270 final ApplicationEvent event ) 271 throws Exception 272 { 273 for( int i = 0; i < m_listeners.length; i++ ) 274 { 275 m_listeners[ i ].applicationStarting( event ); 276 } 277 } 278 279 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 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 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 315 public synchronized void applicationFailure( 316 final Exception causeOfFailure ) 317 { 318 for( int i = 0; i < m_listeners.length; i++ ) 319 { 320 m_listeners[ i ].applicationFailure( causeOfFailure ); 321 } 322 } 323 324 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 for( int i = 0; i < m_blockListeners.length; i++ ) 338 { 339 m_blockListeners[ i ].blockAdded( event ); 340 } 341 } 342 343 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 for( int i = 0; i < m_blockListeners.length; i++ ) 358 { 359 m_blockListeners[ i ].blockRemoved( event ); 360 } 361 } 362 } 363 | Popular Tags |