1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
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 | |
|
26 | |
|
27 | 3100 | public abstract class AbstractConnectionStrategy implements ConnectionStrategy |
28 | |
{ |
29 | |
|
30 | |
|
31 | |
|
32 | 3100 | protected transient Log logger = LogFactory.getLog(getClass()); |
33 | |
|
34 | 3100 | private volatile boolean doThreading = false; |
35 | |
|
36 | 3100 | private final Object reconnectLock = new Object(); |
37 | |
|
38 | |
public final void connect(final UMOConnectable connectable) throws FatalConnectException |
39 | |
{ |
40 | 80 | if (doThreading) |
41 | |
{ |
42 | |
try |
43 | |
{ |
44 | 0 | MuleManager.getInstance().getWorkManager().scheduleWork(new Work() |
45 | |
{ |
46 | |
public void release() |
47 | |
{ |
48 | |
|
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 | |
|
67 | |
|
68 | 0 | if (connectable instanceof UMOConnector) |
69 | |
{ |
70 | 0 | ((UMOConnector) connectable).handleException(e); |
71 | |
} |
72 | |
|
73 | 0 | else if (connectable instanceof AbstractMessageReceiver) |
74 | |
{ |
75 | 0 | ((AbstractMessageReceiver) connectable).handleException(e); |
76 | |
} |
77 | |
|
78 | 0 | } |
79 | 0 | } |
80 | |
}); |
81 | |
} |
82 | 0 | catch (WorkException e) |
83 | |
{ |
84 | 0 | synchronized (reconnectLock) |
85 | |
{ |
86 | 0 | resetState(); |
87 | 0 | } |
88 | 0 | throw new FatalConnectException(e, connectable); |
89 | 0 | } |
90 | |
} |
91 | |
else |
92 | |
{ |
93 | |
try |
94 | |
{ |
95 | 80 | synchronized (reconnectLock) |
96 | |
{ |
97 | 80 | doConnect(connectable); |
98 | 80 | } |
99 | |
} |
100 | |
finally |
101 | |
{ |
102 | 80 | synchronized (reconnectLock) |
103 | |
{ |
104 | 80 | resetState(); |
105 | 80 | } |
106 | 80 | } |
107 | |
} |
108 | 80 | } |
109 | |
|
110 | |
public boolean isDoThreading() |
111 | |
{ |
112 | 808 | return doThreading; |
113 | |
} |
114 | |
|
115 | |
public void setDoThreading(boolean doThreading) |
116 | |
{ |
117 | 804 | this.doThreading = doThreading; |
118 | 804 | } |
119 | |
|
120 | |
protected abstract void doConnect(UMOConnectable connectable) throws FatalConnectException; |
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
public abstract void resetState(); |
126 | |
|
127 | |
protected String getDescription(UMOConnectable connectable) |
128 | |
{ |
129 | 0 | if (connectable instanceof UMOMessageReceiver) |
130 | |
{ |
131 | 0 | return ((UMOMessageReceiver) connectable).getEndpointURI().toString(); |
132 | |
} |
133 | |
else |
134 | |
{ |
135 | 0 | return connectable.toString(); |
136 | |
} |
137 | |
} |
138 | |
|
139 | |
} |