KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > AbstractConnectionStrategy


1 /*
2  * $Id: AbstractConnectionStrategy.java 3982 2006-11-22 14:28:01Z lajos $
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 MuleSource MPL
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 javax.resource.spi.work.Work JavaDoc;
14 import javax.resource.spi.work.WorkException JavaDoc;
15
16 import org.apache.commons.logging.Log;
17 import org.apache.commons.logging.LogFactory;
18 import org.mule.MuleManager;
19 import org.mule.umo.provider.UMOConnectable;
20 import org.mule.umo.provider.UMOMessageReceiver;
21
22 /**
23  * TODO document
24  */

25 public abstract class AbstractConnectionStrategy implements ConnectionStrategy
26 {
27     /**
28      * logger used by this class
29      */

30     protected transient Log logger = LogFactory.getLog(getClass());
31
32     private volatile boolean doThreading = false;
33
34     public final void connect(final UMOConnectable connectable) throws FatalConnectException
35     {
36         if (doThreading)
37         {
38             try
39             {
40                 MuleManager.getInstance().getWorkManager().scheduleWork(new Work JavaDoc()
41                 {
42                     public void release()
43                     {
44                         // nothing to do
45
}
46
47                     public void run()
48                     {
49                         try
50                         {
51                             synchronized (this)
52                             {
53                                 doConnect(connectable);
54                             }
55                         }
56                         catch (FatalConnectException e)
57                         {
58                             synchronized (this)
59                             {
60                                 resetState();
61                             }
62                             // TODO: this cast is evil
63
if (connectable instanceof AbstractMessageReceiver)
64                             {
65                                 ((AbstractMessageReceiver)connectable).handleException(e);
66                             }
67                         }
68                     }
69                 });
70             }
71             catch (WorkException JavaDoc e)
72             {
73                 synchronized (this)
74                 {
75                     resetState();
76                 }
77                 throw new FatalConnectException(e, connectable);
78             }
79         }
80         else
81         {
82             try
83             {
84                 synchronized (this)
85                 {
86                     doConnect(connectable);
87                 }
88             }
89             finally
90             {
91                 synchronized (this)
92                 {
93                     resetState();
94                 }
95             }
96         }
97     }
98
99     public boolean isDoThreading()
100     {
101         return doThreading;
102     }
103
104     public void setDoThreading(boolean doThreading)
105     {
106         this.doThreading = doThreading;
107     }
108
109     public abstract void doConnect(UMOConnectable connectable) throws FatalConnectException;
110
111     /**
112      * Resets any state stored in the retry strategy
113      */

114     public abstract void resetState();
115
116     protected String JavaDoc getDescription(UMOConnectable connectable)
117     {
118         if (connectable instanceof UMOMessageReceiver)
119         {
120             return ((UMOMessageReceiver)connectable).getEndpointURI().toString();
121         }
122         else
123         {
124             return connectable.toString();
125         }
126     }
127
128 }
129
Popular Tags