1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.construct.builder; |
8 | |
|
9 | |
import java.util.Arrays; |
10 | |
|
11 | |
import org.mule.MessageExchangePattern; |
12 | |
import org.mule.api.MuleContext; |
13 | |
import org.mule.api.MuleException; |
14 | |
import org.mule.api.endpoint.EndpointBuilder; |
15 | |
import org.mule.api.endpoint.InboundEndpoint; |
16 | |
import org.mule.api.endpoint.OutboundEndpoint; |
17 | |
import org.mule.api.processor.MessageProcessor; |
18 | |
import org.mule.api.transaction.TransactionConfig; |
19 | |
import org.mule.api.transaction.TransactionException; |
20 | |
import org.mule.api.transaction.TransactionFactory; |
21 | |
import org.mule.api.transformer.Transformer; |
22 | |
import org.mule.config.i18n.MessageFactory; |
23 | |
import org.mule.construct.Bridge; |
24 | |
import org.mule.transaction.MuleTransactionConfig; |
25 | |
import org.mule.transaction.XaTransactionFactory; |
26 | |
import org.mule.util.ClassUtils; |
27 | |
import org.mule.util.StringUtils; |
28 | |
|
29 | 0 | public class BridgeBuilder extends |
30 | |
AbstractFlowConstructWithSingleInboundAndOutboundEndpointBuilder<BridgeBuilder, Bridge> |
31 | |
{ |
32 | 0 | protected MessageExchangePattern exchangePattern = MessageExchangePattern.REQUEST_RESPONSE; |
33 | 0 | protected boolean transacted = false; |
34 | |
|
35 | |
@Override |
36 | |
protected MessageExchangePattern getInboundMessageExchangePattern() |
37 | |
{ |
38 | 0 | return exchangePattern; |
39 | |
} |
40 | |
|
41 | |
@Override |
42 | |
protected MessageExchangePattern getOutboundMessageExchangePattern() |
43 | |
{ |
44 | 0 | return exchangePattern; |
45 | |
} |
46 | |
|
47 | |
public BridgeBuilder exchangePattern(MessageExchangePattern exchangePattern) |
48 | |
{ |
49 | 0 | this.exchangePattern = exchangePattern; |
50 | 0 | return this; |
51 | |
} |
52 | |
|
53 | |
public BridgeBuilder transacted(boolean transacted) |
54 | |
{ |
55 | 0 | this.transacted = transacted; |
56 | 0 | return this; |
57 | |
} |
58 | |
|
59 | |
public BridgeBuilder inboundTransformers(Transformer... transformers) |
60 | |
{ |
61 | 0 | this.inboundTransformers = Arrays.asList((MessageProcessor[]) transformers); |
62 | 0 | return this; |
63 | |
} |
64 | |
|
65 | |
public BridgeBuilder inboundResponseTransformers(Transformer... responseTransformers) |
66 | |
{ |
67 | 0 | this.inboundResponseTransformers = Arrays.asList((MessageProcessor[]) responseTransformers); |
68 | 0 | return this; |
69 | |
} |
70 | |
|
71 | |
@Override |
72 | |
protected void doConfigureInboundEndpointBuilder(MuleContext muleContext, EndpointBuilder endpointBuilder) |
73 | |
{ |
74 | 0 | if (transacted) |
75 | |
{ |
76 | 0 | MuleTransactionConfig transactionConfig = new MuleTransactionConfig(); |
77 | 0 | transactionConfig.setMuleContext(muleContext); |
78 | 0 | transactionConfig.setAction(TransactionConfig.ACTION_BEGIN_OR_JOIN); |
79 | 0 | endpointBuilder.setTransactionConfig(transactionConfig); |
80 | |
} |
81 | 0 | } |
82 | |
|
83 | |
@Override |
84 | |
protected void doConfigureOutboundEndpointBuilder(MuleContext muleContext, EndpointBuilder endpointBuilder) |
85 | |
{ |
86 | 0 | if (transacted) |
87 | |
{ |
88 | 0 | MuleTransactionConfig transactionConfig = new MuleTransactionConfig(); |
89 | 0 | transactionConfig.setMuleContext(muleContext); |
90 | 0 | transactionConfig.setAction(TransactionConfig.ACTION_ALWAYS_JOIN); |
91 | 0 | endpointBuilder.setTransactionConfig(transactionConfig); |
92 | |
} |
93 | 0 | } |
94 | |
|
95 | |
@Override |
96 | |
protected Bridge buildFlowConstruct(MuleContext muleContext) throws MuleException |
97 | |
{ |
98 | 0 | InboundEndpoint inboundEndpoint = getOrBuildInboundEndpoint(muleContext); |
99 | 0 | OutboundEndpoint outboundEndpoint = getOrBuildOutboundEndpoint(muleContext); |
100 | |
|
101 | 0 | if (transacted) |
102 | |
{ |
103 | 0 | setTransactionFactoriesIfNeeded(inboundEndpoint, outboundEndpoint); |
104 | |
} |
105 | |
|
106 | 0 | return new Bridge(name, muleContext, inboundEndpoint, outboundEndpoint, exchangePattern, transacted); |
107 | |
} |
108 | |
|
109 | |
private void setTransactionFactoriesIfNeeded(InboundEndpoint inboundEndpoint, |
110 | |
OutboundEndpoint outboundEndpoint) throws MuleException |
111 | |
{ |
112 | 0 | String inboundProtocol = inboundEndpoint.getConnector().getProtocol(); |
113 | 0 | String outboundProtocol = outboundEndpoint.getConnector().getProtocol(); |
114 | |
|
115 | 0 | boolean needXA = !inboundProtocol.equals(outboundProtocol); |
116 | |
|
117 | 0 | TransactionConfig inboundTransactionConfig = inboundEndpoint.getTransactionConfig(); |
118 | |
|
119 | 0 | if (inboundTransactionConfig.getFactory() == null) |
120 | |
{ |
121 | 0 | TransactionFactory transactionFactory = needXA |
122 | |
? new XaTransactionFactory() |
123 | |
: getTransactionFactory(inboundProtocol); |
124 | |
|
125 | 0 | inboundTransactionConfig.setFactory(transactionFactory); |
126 | |
} |
127 | |
|
128 | 0 | TransactionConfig outboundTransactionConfig = outboundEndpoint.getTransactionConfig(); |
129 | |
|
130 | 0 | if (outboundTransactionConfig.getFactory() == null) |
131 | |
{ |
132 | 0 | TransactionFactory transactionFactory = needXA |
133 | |
? new XaTransactionFactory() |
134 | |
: getTransactionFactory(outboundProtocol); |
135 | 0 | outboundTransactionConfig.setFactory(transactionFactory); |
136 | |
} |
137 | 0 | } |
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
private TransactionFactory getTransactionFactory(String protocol) throws MuleException |
143 | |
{ |
144 | 0 | String protocolTransactionFactoryClassName = "org.mule.transport." + StringUtils.lowerCase(protocol) |
145 | |
+ "." + StringUtils.capitalize(protocol) |
146 | |
+ "TransactionFactory"; |
147 | |
|
148 | 0 | if (!ClassUtils.isClassOnPath(protocolTransactionFactoryClassName, getClass())) |
149 | |
{ |
150 | 0 | throw new TransactionException( |
151 | |
MessageFactory.createStaticMessage("Failed to locate a transaction factory for protocol: " |
152 | |
+ protocol)); |
153 | |
} |
154 | |
|
155 | |
try |
156 | |
{ |
157 | 0 | return (TransactionFactory) ClassUtils.instanciateClass(protocolTransactionFactoryClassName); |
158 | |
} |
159 | 0 | catch (Exception e) |
160 | |
{ |
161 | 0 | throw new TransactionException( |
162 | |
MessageFactory.createStaticMessage("Failed to instantiate a transaction factory for protocol: " |
163 | |
+ protocol), e); |
164 | |
} |
165 | |
} |
166 | |
} |