1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.construct; |
12 | |
|
13 | |
import org.apache.commons.lang.Validate; |
14 | |
import org.mule.MessageExchangePattern; |
15 | |
import org.mule.api.MuleContext; |
16 | |
import org.mule.api.construct.FlowConstructInvalidException; |
17 | |
import org.mule.api.endpoint.InboundEndpoint; |
18 | |
import org.mule.api.endpoint.OutboundEndpoint; |
19 | |
import org.mule.api.source.MessageSource; |
20 | |
import org.mule.config.i18n.MessageFactory; |
21 | |
import org.mule.construct.processor.FlowConstructStatisticsMessageObserver; |
22 | |
import org.mule.interceptor.LoggingInterceptor; |
23 | |
import org.mule.processor.builder.InterceptingChainMessageProcessorBuilder; |
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
public class Bridge extends AbstractFlowConstruct |
31 | |
{ |
32 | |
private final OutboundEndpoint outboundEndpoint; |
33 | |
private final MessageExchangePattern exchangePattern; |
34 | |
private final boolean transacted; |
35 | |
|
36 | |
public Bridge(String name, |
37 | |
MuleContext muleContext, |
38 | |
MessageSource messageSource, |
39 | |
OutboundEndpoint outboundEndpoint, |
40 | |
MessageExchangePattern exchangePattern, |
41 | |
boolean transacted) |
42 | |
{ |
43 | 0 | super(name, muleContext); |
44 | |
|
45 | 0 | Validate.notNull(messageSource, "messageSource can't be null"); |
46 | 0 | Validate.notNull(outboundEndpoint, "outboundEndpoint can't be null"); |
47 | 0 | Validate.notNull(exchangePattern, "exchangePattern can't be null"); |
48 | |
|
49 | 0 | this.messageSource = messageSource; |
50 | 0 | this.outboundEndpoint = outboundEndpoint; |
51 | 0 | this.exchangePattern = exchangePattern; |
52 | 0 | this.transacted = transacted; |
53 | 0 | } |
54 | |
|
55 | |
@Override |
56 | |
protected void configureMessageProcessors(InterceptingChainMessageProcessorBuilder builder) |
57 | |
{ |
58 | 0 | builder.chain(new LoggingInterceptor()); |
59 | 0 | builder.chain(new FlowConstructStatisticsMessageObserver()); |
60 | 0 | builder.chain(outboundEndpoint); |
61 | 0 | } |
62 | |
|
63 | |
@Override |
64 | |
protected void validateConstruct() throws FlowConstructInvalidException |
65 | |
{ |
66 | 0 | super.validateConstruct(); |
67 | |
|
68 | 0 | if (messageSource instanceof InboundEndpoint) |
69 | |
{ |
70 | 0 | validateInboundEndpoint((InboundEndpoint) messageSource); |
71 | |
} |
72 | |
|
73 | 0 | validateOutboundEndpoint(); |
74 | 0 | } |
75 | |
|
76 | |
private void validateOutboundEndpoint() throws FlowConstructInvalidException |
77 | |
{ |
78 | 0 | if (outboundEndpoint.getExchangePattern() != exchangePattern) |
79 | |
{ |
80 | 0 | throw new FlowConstructInvalidException( |
81 | |
MessageFactory.createStaticMessage("Inconsistent bridge outbound endpoint exchange pattern, expected " |
82 | |
+ exchangePattern |
83 | |
+ " but was " |
84 | |
+ outboundEndpoint.getExchangePattern()), this); |
85 | |
} |
86 | |
|
87 | 0 | if (transacted |
88 | |
&& ((outboundEndpoint.getTransactionConfig() == null) || (!outboundEndpoint.getTransactionConfig() |
89 | |
.isConfigured()))) |
90 | |
{ |
91 | 0 | throw new FlowConstructInvalidException( |
92 | |
MessageFactory.createStaticMessage("A transacted bridge requires a transacted outbound endpoint"), |
93 | |
this); |
94 | |
} |
95 | |
|
96 | 0 | if ((!transacted) && (outboundEndpoint.getTransactionConfig() != null) |
97 | |
&& (outboundEndpoint.getTransactionConfig().isConfigured())) |
98 | |
{ |
99 | 0 | throw new FlowConstructInvalidException( |
100 | |
MessageFactory.createStaticMessage("A non-transacted bridge requires a non-transacted outbound endpoint"), |
101 | |
this); |
102 | |
} |
103 | 0 | } |
104 | |
|
105 | |
private void validateInboundEndpoint(InboundEndpoint inboundEndpoint) |
106 | |
throws FlowConstructInvalidException |
107 | |
{ |
108 | 0 | if (inboundEndpoint.getExchangePattern() != exchangePattern) |
109 | |
{ |
110 | 0 | throw new FlowConstructInvalidException( |
111 | |
MessageFactory.createStaticMessage("Inconsistent bridge inbound endpoint exchange pattern, expected " |
112 | |
+ exchangePattern |
113 | |
+ " but was " |
114 | |
+ inboundEndpoint.getExchangePattern()), this); |
115 | |
} |
116 | |
|
117 | 0 | if (transacted |
118 | |
&& ((inboundEndpoint.getTransactionConfig() == null) || (!inboundEndpoint.getTransactionConfig() |
119 | |
.isConfigured()))) |
120 | |
{ |
121 | 0 | throw new FlowConstructInvalidException( |
122 | |
MessageFactory.createStaticMessage("A transacted bridge requires a transacted inbound endpoint"), |
123 | |
this); |
124 | |
} |
125 | |
|
126 | 0 | if ((!transacted) && (inboundEndpoint.getTransactionConfig() != null) |
127 | |
&& (inboundEndpoint.getTransactionConfig().isConfigured())) |
128 | |
{ |
129 | 0 | throw new FlowConstructInvalidException( |
130 | |
MessageFactory.createStaticMessage("A non-transacted bridge requires a non-transacted inbound endpoint"), |
131 | |
this); |
132 | |
} |
133 | 0 | } |
134 | |
} |