Coverage Report - org.mule.providers.AbstractConnectionStrategy
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractConnectionStrategy
0%
0/25
0%
0/2
2
AbstractConnectionStrategy$1
0%
0/15
0%
0/2
2
 
 1  
 /*
 2  
  * $Id: AbstractConnectionStrategy.java 7976 2007-08-21 14:26:13Z dirk.olmes $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.providers;
 12  
 
 13  
 import org.mule.MuleManager;
 14  
 import org.mule.umo.provider.UMOConnectable;
 15  
 import org.mule.umo.provider.UMOConnector;
 16  
 import org.mule.umo.provider.UMOMessageReceiver;
 17  
 
 18  
 import javax.resource.spi.work.Work;
 19  
 import javax.resource.spi.work.WorkException;
 20  
 
 21  
 import org.apache.commons.logging.Log;
 22  
 import org.apache.commons.logging.LogFactory;
 23  
 
 24  
 /**
 25  
  * TODO document
 26  
  */
 27  0
 public abstract class AbstractConnectionStrategy implements ConnectionStrategy
 28  
 {
 29  
     /**
 30  
      * logger used by this class
 31  
      */
 32  0
     protected transient Log logger = LogFactory.getLog(getClass());
 33  
 
 34  0
     private volatile boolean doThreading = false;
 35  
 
 36  0
     private final Object reconnectLock = new Object();
 37  
 
 38  
     public final void connect(final UMOConnectable connectable) throws FatalConnectException
 39  
     {
 40  0
         if (doThreading)
 41  
         {
 42  
             try
 43  
             {
 44  0
                 MuleManager.getInstance().getWorkManager().scheduleWork(new Work()
 45  
                 {
 46  
                     public void release()
 47  
                     {
 48  
                         // nothing to do
 49  0
                     }
 50  
 
 51  0
                     public void run()
 52  
                     {
 53  
                         try
 54  
                         {
 55  0
                             synchronized (reconnectLock)
 56  
                             {
 57  0
                                 doConnect(connectable);
 58  0
                             }
 59  
                         }
 60  0
                         catch (FatalConnectException e)
 61  
                         {
 62  0
                             synchronized (reconnectLock)
 63  
                             {
 64  0
                                 resetState();
 65  0
                             }
 66  
                             // TODO should really extract an interface for
 67  
                             // classes capable of handling an exception
 68  0
                             if (connectable instanceof UMOConnector)
 69  
                             {
 70  0
                                 ((UMOConnector) connectable).handleException(e);
 71  
                             }
 72  
                             // TODO: this cast is evil
 73  0
                             else if (connectable instanceof AbstractMessageReceiver)
 74  
                             {
 75  0
                                 ((AbstractMessageReceiver) connectable).handleException(e);
 76  
                             }
 77  
                             // TODO MULE-863: And if it's not?
 78  
                             // AP if it's not, it's not handled and Mule just sits doing nothing
 79  0
                         }
 80  0
                     }
 81  
                 });
 82  
             }
 83  0
             catch (WorkException e)
 84  
             {
 85  0
                 synchronized (reconnectLock)
 86  
                 {
 87  0
                     resetState();
 88  0
                 }
 89  0
                 throw new FatalConnectException(e, connectable);
 90  0
             }
 91  
         }
 92  
         else
 93  
         {
 94  
             try
 95  
             {
 96  0
                 synchronized (reconnectLock)
 97  
                 {
 98  0
                     doConnect(connectable);
 99  0
                 }
 100  
             }
 101  
             finally
 102  
             {
 103  0
                 synchronized (reconnectLock)
 104  
                 {
 105  0
                     resetState();
 106  0
                 }
 107  
             }
 108  
         }
 109  0
     }
 110  
 
 111  
     public boolean isDoThreading()
 112  
     {
 113  0
         return doThreading;
 114  
     }
 115  
 
 116  
     public void setDoThreading(boolean doThreading)
 117  
     {
 118  0
         this.doThreading = doThreading;
 119  0
     }
 120  
 
 121  
     protected abstract void doConnect(UMOConnectable connectable) throws FatalConnectException;
 122  
 
 123  
     /**
 124  
      * Resets any state stored in the retry strategy
 125  
      */
 126  
     public abstract void resetState();
 127  
 
 128  
     protected String getDescription(UMOConnectable connectable)
 129  
     {
 130  0
         if (connectable instanceof UMOMessageReceiver)
 131  
         {
 132  0
             return ((UMOMessageReceiver) connectable).getEndpointURI().toString();
 133  
         }
 134  
         else
 135  
         {
 136  0
             return connectable.toString();
 137  
         }
 138  
     }
 139  
 
 140  
 }