1
package com.eucalyptus.util.async;
4
import org.apache.log4j.Logger;
5
import com.eucalyptus.records.EventRecord;
6
import com.eucalyptus.records.EventType;
7
import com.eucalyptus.records.Logs;
8
import com.eucalyptus.util.Callback;
9
import com.google.common.collect.Lists;
10
import edu.ucsb.eucalyptus.msgs.BaseMessage;
17
public class CallbackListenerSequence<R extends BaseMessage> implements Callback.Checked<R> {
18
private Logger LOG = Logger.getLogger( this.getClass( ) );
19
private List<Callback<R>> successCallbacks = Lists.newArrayList( );
20
private List<Callback.Checked<R>> failureCallbacks = Lists.newArrayList( );
23
* Add a callback which is to be invoked when the operation completes, regardless of the outcome.
26
* - callback to invoke
27
* @return <tt>this</tt>
29
public CallbackListenerSequence<R> addCallback( final UnconditionalCallback c ) {
30
EventRecord.caller( CallbackListenerSequence.class, EventType.CALLBACK, UnconditionalCallback.class.getSimpleName( ), c.getClass( ) ).extreme( );
31
this.successCallbacks.add( c );
32
this.failureCallbacks.add( new Callback.Failure() {
34
public void fireException( Throwable t ) {
42
* Add a callback which is to be invoked when the operation completes, regardless of the outcome.
45
* - callback to invoke
46
* @return <tt>this</tt>
48
public CallbackListenerSequence<R> addCallback( Callback.Checked c ) {
49
EventRecord.caller( CallbackListenerSequence.class, EventType.CALLBACK, Callback.Checked.class.getSimpleName( ), c.getClass( ) ).extreme( );
50
this.successCallbacks.add( c );
51
this.failureCallbacks.add( c );
56
* Add a callback which is to be invoked if the operation succeeds.
59
* - callback to invoke
60
* @return <tt>this</tt>
62
@SuppressWarnings( "unchecked" )
63
public CallbackListenerSequence<R> addSuccessCallback( Callback.Success<R> c ) {
64
EventRecord.caller( CallbackListenerSequence.class, EventType.CALLBACK, Callback.Success.class.getSimpleName( ), c.getClass( ) ).extreme( );
65
this.successCallbacks.add( c );
70
* Add a callback which is to be invoked if the operation fails.
73
* - callback to invoke
74
* @return <tt>this</tt>
76
public CallbackListenerSequence<R> addFailureCallback( Callback.Failure c ) {
77
EventRecord.caller( CallbackListenerSequence.class, EventType.CALLBACK, Callback.Failure.class.getSimpleName( ), c.getClass( ) ).extreme( );
78
this.failureCallbacks.add( c );
83
* Fire the response on all listeners.
88
public void fire( R response ) {
89
EventRecord.here( CallbackListenerSequence.class, EventType.CALLBACK, "fire(" + response.getClass( ).getName( ) + ")" ).extreme( );
90
for ( Callback<R> cb : this.successCallbacks ) {
92
EventRecord.here( this.getClass( ), EventType.CALLBACK, "" + cb.getClass( ), "fire(" + response.getClass( ).getCanonicalName( ) + ")" ).extreme( );
94
} catch ( Exception t ) {
95
this.LOG.error( "Exception occurred while trying to call: " + cb.getClass( ) + ".apply( " + t.getMessage( ) + " )" );
96
this.LOG.error( t, t );
102
* Trigger the failure case.
107
public void fireException( Throwable t ) {
108
EventRecord.here( CallbackListenerSequence.class, EventType.CALLBACK, "fireException(" + t.getClass( ).getName( ) + ")" ).extreme( );
109
for ( Callback.Checked<R> cb : this.failureCallbacks ) {
111
EventRecord.here( this.getClass( ), EventType.CALLBACK, "" + cb.getClass( ), "fireException(" + t.getClass( ).getCanonicalName( ) + ")" ).extreme( );
112
cb.fireException( t );
113
} catch ( Exception t2 ) {
114
this.LOG.error( "Exception occurred while trying to call: " + cb.getClass( ) + ".failure( " + t.getMessage( ) + " )" );
115
this.LOG.error( t2, t2 );