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