Coverage Report - org.mule.retry.policies.AbstractPolicyTemplate
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractPolicyTemplate
0%
0/39
0%
0/22
0
 
 1  
 /*
 2  
  * $Id: AbstractPolicyTemplate.java 19191 2010-08-25 21:05:23Z tcarlson $
 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.policies;
 12  
 
 13  
 import org.mule.api.MuleContext;
 14  
 import org.mule.api.context.MuleContextAware;
 15  
 import org.mule.api.context.WorkManager;
 16  
 import org.mule.api.retry.RetryCallback;
 17  
 import org.mule.api.retry.RetryContext;
 18  
 import org.mule.api.retry.RetryNotifier;
 19  
 import org.mule.api.retry.RetryPolicy;
 20  
 import org.mule.api.retry.RetryPolicyTemplate;
 21  
 import org.mule.retry.DefaultRetryContext;
 22  
 import org.mule.retry.PolicyStatus;
 23  
 import org.mule.retry.RetryPolicyExhaustedException;
 24  
 import org.mule.retry.notifiers.ConnectNotifier;
 25  
 
 26  
 import java.io.InterruptedIOException;
 27  
 import java.util.Map;
 28  
 
 29  
 import org.apache.commons.logging.Log;
 30  
 import org.apache.commons.logging.LogFactory;
 31  
 
 32  
 /**
 33  
  * Base class for RetryPolicyTemplate implementations.  Uses ConnectNotifier as RetryNotifier
 34  
  * by default.
 35  
  */
 36  0
 public abstract class AbstractPolicyTemplate implements RetryPolicyTemplate, MuleContextAware
 37  
 {
 38  0
     protected RetryNotifier notifier = new ConnectNotifier();
 39  
     
 40  
     /** This data will be made available to the RetryPolicy via the RetryContext. */
 41  
     private Map<Object, Object> metaInfo;
 42  
 
 43  
     private MuleContext muleContext;
 44  
 
 45  0
     protected transient final Log logger = LogFactory.getLog(getClass());
 46  
 
 47  
     public void setMuleContext(MuleContext context)
 48  
     {
 49  0
         this.muleContext = context;
 50  0
     }
 51  
 
 52  
     public RetryContext execute(RetryCallback callback, WorkManager workManager) throws Exception
 53  
     {
 54  0
         PolicyStatus status = null;
 55  0
         RetryPolicy policy = createRetryInstance();
 56  0
         DefaultRetryContext context = new DefaultRetryContext(callback.getWorkDescription(), 
 57  
             metaInfo);
 58  0
         context.setMuleContext(muleContext);
 59  
 
 60  
         try
 61  
         {
 62  0
             Exception cause = null;
 63  
             do
 64  
             {
 65  
                 try
 66  
                 {
 67  0
                     callback.doWork(context);
 68  0
                     if (notifier != null)
 69  
                     {
 70  0
                         notifier.onSuccess(context);
 71  
                     }
 72  0
                     break;
 73  
                 }
 74  0
                 catch (Exception e)
 75  
                 {
 76  0
                     cause = e;
 77  0
                     if (logger.isDebugEnabled())
 78  
                     {
 79  0
                         logger.debug(cause);
 80  
                     }
 81  0
                     if (notifier != null)
 82  
                     {
 83  0
                         notifier.onFailure(context, cause);
 84  
                     }
 85  0
                     if (cause instanceof InterruptedException || cause instanceof InterruptedIOException)
 86  
                     {
 87  0
                         logger.error("Process was interrupted (InterruptedException), ceasing process");
 88  0
                         break;
 89  
                     }
 90  
                     else
 91  
                     {
 92  0
                         status = policy.applyPolicy(cause);
 93  
                     }
 94  
                 }
 95  
             }
 96  0
             while (status.isOk());
 97  
 
 98  0
             if (status == null || status.isOk())
 99  
             {
 100  0
                 return context;
 101  
             }
 102  
             else
 103  
             {
 104  0
                 context.setFailed(cause);
 105  0
                 throw new RetryPolicyExhaustedException(cause, callback.getWorkDescription());
 106  
             }
 107  
         }
 108  
         finally
 109  
         {
 110  0
             if (status != null && status.getThrowable() != null)
 111  
             {
 112  0
                 if (logger.isDebugEnabled())
 113  
                 {
 114  0
                     logger.debug(status.getThrowable());
 115  
                 }
 116  
             }
 117  
         }
 118  
     }
 119  
     
 120  
     public RetryNotifier getNotifier()
 121  
     {
 122  0
         return notifier;
 123  
     }
 124  
 
 125  
     public void setNotifier(RetryNotifier retryNotifier)
 126  
     {
 127  0
         this.notifier = retryNotifier;
 128  0
     }
 129  
 
 130  
     public Map<Object, Object> getMetaInfo()
 131  
     {
 132  0
         return metaInfo;
 133  
     }
 134  
 
 135  
     public void setMetaInfo(Map<Object, Object> metaInfo)
 136  
     {
 137  0
         this.metaInfo = metaInfo;
 138  0
     }
 139  
 
 140  
     // For Spring IoC only
 141  
     public void setId(String id)
 142  
     {
 143  
         // ignore
 144  0
     }
 145  
 }