View Javadoc

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  public abstract class AbstractConnectionStrategy implements ConnectionStrategy
28  {
29      /**
30       * logger used by this class
31       */
32      protected transient Log logger = LogFactory.getLog(getClass());
33  
34      private volatile boolean doThreading = false;
35  
36      private final Object reconnectLock = new Object();
37  
38      public final void connect(final UMOConnectable connectable) throws FatalConnectException
39      {
40          if (doThreading)
41          {
42              try
43              {
44                  MuleManager.getInstance().getWorkManager().scheduleWork(new Work()
45                  {
46                      public void release()
47                      {
48                          // nothing to do
49                      }
50  
51                      public void run()
52                      {
53                          try
54                          {
55                              synchronized (reconnectLock)
56                              {
57                                  doConnect(connectable);
58                              }
59                          }
60                          catch (FatalConnectException e)
61                          {
62                              synchronized (reconnectLock)
63                              {
64                                  resetState();
65                              }
66                              // TODO should really extract an interface for
67                              // classes capable of handling an exception
68                              if (connectable instanceof UMOConnector)
69                              {
70                                  ((UMOConnector) connectable).handleException(e);
71                              }
72                              // TODO: this cast is evil
73                              else if (connectable instanceof AbstractMessageReceiver)
74                              {
75                                  ((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                          }
80                      }
81                  });
82              }
83              catch (WorkException e)
84              {
85                  synchronized (reconnectLock)
86                  {
87                      resetState();
88                  }
89                  throw new FatalConnectException(e, connectable);
90              }
91          }
92          else
93          {
94              try
95              {
96                  synchronized (reconnectLock)
97                  {
98                      doConnect(connectable);
99                  }
100             }
101             finally
102             {
103                 synchronized (reconnectLock)
104                 {
105                     resetState();
106                 }
107             }
108         }
109     }
110 
111     public boolean isDoThreading()
112     {
113         return doThreading;
114     }
115 
116     public void setDoThreading(boolean doThreading)
117     {
118         this.doThreading = doThreading;
119     }
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         if (connectable instanceof UMOMessageReceiver)
131         {
132             return ((UMOMessageReceiver) connectable).getEndpointURI().toString();
133         }
134         else
135         {
136             return connectable.toString();
137         }
138     }
139 
140 }