View Javadoc

1   /*
2    * $Id: FutureRetryContext.java 22088 2011-06-03 10:07:47Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.retry.async;
12  
13  import java.util.Map;
14  
15  import org.mule.api.MuleContext;
16  import org.mule.api.MuleMessage;
17  import org.mule.api.retry.RetryContext;
18  
19  /**
20   * An implementation of {@link RetryContext} to be used when a {@link org.mule.api.retry.RetryPolicyTemplate} is
21   * executed in a separate thread via the {@link AsynchronousRetryTemplate}. A FutureRetryContext is a proxy to a real
22   * {@link RetryContext} and provides access to the real context once it becomes available.
23   */
24  public class FutureRetryContext implements RetryContext
25  {
26      private RetryContext delegate;
27  
28      void setDelegateContext(RetryContext context)
29      {
30          this.delegate = context;
31      }
32  
33      public boolean isReady()
34      {
35          return delegate != null;
36      }
37  
38      protected void checkState()
39      {
40          if (!isReady())
41          {
42              throw new IllegalStateException(
43                  "Cannot perform operations on a FutureRetryContext until isReady() returns true");
44          }
45      }
46  
47      public void addReturnMessage(MuleMessage result)
48      {
49          checkState();
50          delegate.addReturnMessage(result);
51      }
52  
53      public String getDescription()
54      {
55          checkState();
56          return delegate.getDescription();
57      }
58  
59      public MuleMessage getFirstReturnMessage()
60      {
61          checkState();
62          return delegate.getFirstReturnMessage();
63      }
64  
65      public Map<Object, Object> getMetaInfo()
66      {
67          checkState();
68          return delegate.getMetaInfo();
69      }
70  
71      public MuleMessage[] getReturnMessages()
72      {
73          checkState();
74          return delegate.getReturnMessages();
75      }
76  
77      public void setReturnMessages(MuleMessage[] returnMessages)
78      {
79          checkState();
80          delegate.setReturnMessages(returnMessages);
81      }
82  
83      public Throwable getLastFailure()
84      {
85          checkState();
86          return delegate.getLastFailure();
87      }
88  
89      public void setOk()
90      {
91          checkState();
92          delegate.setOk();
93      }
94  
95      public void setFailed(Throwable lastFailure)
96      {
97          checkState();
98          delegate.setFailed(lastFailure);
99      }
100 
101     public boolean isOk()
102     {
103         checkState();
104         return delegate.isOk();
105     }
106 
107     public MuleContext getMuleContext()
108     {
109         checkState();
110         return delegate.getMuleContext();
111     }
112 }