~ubuntu-branches/ubuntu/raring/eucalyptus/raring

« back to all changes in this revision

Viewing changes to clc/modules/msgs/src/main/java/com/eucalyptus/util/async/CallbackListenerSequence.java

  • Committer: Package Import Robot
  • Author(s): Brian Thomason
  • Date: 2011-11-29 13:17:52 UTC
  • mfrom: (1.2.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 185.
  • Revision ID: package-import@ubuntu.com-20111129131752-rq31al3ntutv2vvl
Tags: upstream-3.0.999beta1
ImportĀ upstreamĀ versionĀ 3.0.999beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package com.eucalyptus.util.async;
 
2
 
 
3
import java.util.List;
 
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;
 
11
 
 
12
/**
 
13
 * @author decker
 
14
 * @param <T>
 
15
 * @param <R>
 
16
 */
 
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( );
 
21
  
 
22
  /**
 
23
   * Add a callback which is to be invoked when the operation completes, regardless of the outcome.
 
24
   * 
 
25
   * @param c
 
26
   *          - callback to invoke
 
27
   * @return <tt>this</tt>
 
28
   */
 
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() {
 
33
      @Override
 
34
      public void fireException( Throwable t ) {
 
35
        c.fire( );
 
36
      }      
 
37
    } );
 
38
    return this;
 
39
  }
 
40
  
 
41
  /**
 
42
   * Add a callback which is to be invoked when the operation completes, regardless of the outcome.
 
43
   * 
 
44
   * @param c
 
45
   *          - callback to invoke
 
46
   * @return <tt>this</tt>
 
47
   */
 
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 );
 
52
    return this;
 
53
  }
 
54
 
 
55
  /**
 
56
   * Add a callback which is to be invoked if the operation succeeds.
 
57
   * 
 
58
   * @param c
 
59
   *          - callback to invoke
 
60
   * @return <tt>this</tt>
 
61
   */
 
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 );
 
66
    return this;
 
67
  }
 
68
  
 
69
  /**
 
70
   * Add a callback which is to be invoked if the operation fails.
 
71
   * 
 
72
   * @param c
 
73
   *          - callback to invoke
 
74
   * @return <tt>this</tt>
 
75
   */
 
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 );
 
79
    return this;
 
80
  }
 
81
  
 
82
  /**
 
83
   * Fire the response on all listeners.
 
84
   * 
 
85
   * @param response
 
86
   */
 
87
  @Override
 
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 ) {
 
91
      try {
 
92
        EventRecord.here( this.getClass( ), EventType.CALLBACK, "" + cb.getClass( ), "fire(" + response.getClass( ).getCanonicalName( ) + ")" ).extreme( );
 
93
        cb.fire( response );
 
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 );
 
97
      }
 
98
    }
 
99
  }
 
100
  
 
101
  /**
 
102
   * Trigger the failure case.
 
103
   * 
 
104
   * @param t
 
105
   */
 
106
  @Override
 
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 ) {
 
110
      try {
 
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 );
 
116
      }
 
117
    }
 
118
  }
 
119
 
 
120
  
 
121
}