View Javadoc

1   /*
2    * $Id: AbstractConnectionStrategy.java 10404 2008-01-18 17:06:25Z 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                              // if it's none of the above, it's not handled and Mule just sits doing nothing
78                          }
79                      }
80                  });
81              }
82              catch (WorkException e)
83              {
84                  synchronized (reconnectLock)
85                  {
86                      resetState();
87                  }
88                  throw new FatalConnectException(e, connectable);
89              }
90          }
91          else
92          {
93              try
94              {
95                  synchronized (reconnectLock)
96                  {
97                      doConnect(connectable);
98                  }
99              }
100             finally
101             {
102                 synchronized (reconnectLock)
103                 {
104                     resetState();
105                 }
106             }
107         }
108     }
109 
110     public boolean isDoThreading()
111     {
112         return doThreading;
113     }
114 
115     public void setDoThreading(boolean doThreading)
116     {
117         this.doThreading = doThreading;
118     }
119 
120     protected abstract void doConnect(UMOConnectable connectable) throws FatalConnectException;
121 
122     /**
123      * Resets any state stored in the retry strategy
124      */
125     public abstract void resetState();
126 
127     protected String getDescription(UMOConnectable connectable)
128     {
129         if (connectable instanceof UMOMessageReceiver)
130         {
131             return ((UMOMessageReceiver) connectable).getEndpointURI().toString();
132         }
133         else
134         {
135             return connectable.toString();
136         }
137     }
138 
139 }