1 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 25 final class ListenerSupport 26 { 27 private BlockListener[] m_blockListeners = new BlockListener[ 0 ]; 29 30 private ApplicationListener[] m_listeners = new ApplicationListener[ 0 ]; 32 33 39 void fireApplicationStartingEvent( final SarMetaData metaData ) 40 throws Exception 41 { 42 final ApplicationEvent event = 43 new ApplicationEvent( metaData.getName(), metaData ); 44 applicationStarting( event ); 45 } 46 47 53 void fireBlockAddedEvent( final BlockEntry entry ) 54 { 55 blockAdded( createEvent( entry ) ); 56 } 57 58 64 void fireBlockRemovedEvent( final BlockEntry entry ) 65 { 66 blockRemoved( createEvent( entry ) ); 67 } 68 69 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 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 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 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 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 175 public synchronized void applicationStarting( final ApplicationEvent event ) 176 throws Exception 177 { 178 for( int i = 0; i < m_listeners.length; i++ ) 179 { 180 m_listeners[ i ].applicationStarting( event ); 181 } 182 } 183 184 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 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 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 224 public synchronized void applicationFailure( Exception causeOfFailure ) 225 { 226 for( int i = 0; i < m_listeners.length; i++ ) 227 { 228 m_listeners[ i ].applicationFailure( causeOfFailure ); 229 } 230 } 231 232 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 for( int i = 0; i < m_blockListeners.length; i++ ) 247 { 248 m_blockListeners[ i ].blockAdded( event ); 249 } 250 } 251 252 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 for( int i = 0; i < m_blockListeners.length; i++ ) 267 { 268 m_blockListeners[ i ].blockRemoved( event ); 269 } 270 } 271 } 272 | Popular Tags |