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 | 0 | public abstract class AbstractConnectionStrategy implements ConnectionStrategy |
28 | |
{ |
29 | |
|
30 | |
|
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 | |
|
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 | |
|
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 | |
|
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 | |
} |