1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package org.apache.camel.impl; |
18 |
|
|
19 |
|
import java.util.HashMap; |
20 |
|
import java.util.Map; |
21 |
|
|
22 |
|
import org.apache.camel.CamelContext; |
23 |
|
import org.apache.camel.Exchange; |
24 |
|
import org.apache.camel.Message; |
25 |
|
import org.apache.camel.util.UuidGenerator; |
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
public class DefaultExchange implements Exchange { |
33 |
3 |
private static final UuidGenerator DEFAULT_ID_GENERATOR = new UuidGenerator(); |
34 |
|
protected final CamelContext context; |
35 |
|
private Map<String, Object> properties; |
36 |
|
private Message in; |
37 |
|
private Message out; |
38 |
|
private Message fault; |
39 |
|
private Throwable exception; |
40 |
861 |
private String exchangeId = DefaultExchange.DEFAULT_ID_GENERATOR.generateId(); |
41 |
|
|
42 |
861 |
public DefaultExchange(CamelContext context) { |
43 |
861 |
this.context = context; |
44 |
861 |
} |
45 |
|
|
46 |
|
@Override |
47 |
|
public String toString() { |
48 |
561 |
return "Exchange[" + in + "]"; |
49 |
|
} |
50 |
|
|
51 |
|
public Exchange copy() { |
52 |
69 |
Exchange exchange = newInstance(); |
53 |
69 |
exchange.copyFrom(this); |
54 |
69 |
return exchange; |
55 |
|
} |
56 |
|
|
57 |
|
public void copyFrom(Exchange exchange) { |
58 |
111 |
if (exchange == this) { |
59 |
0 |
return; |
60 |
|
} |
61 |
111 |
setProperties(safeCopy(exchange.getProperties())); |
62 |
|
|
63 |
|
|
64 |
|
|
65 |
111 |
safeCopy(getIn(), exchange, exchange.getIn()); |
66 |
111 |
Message copyOut = exchange.getOut(); |
67 |
111 |
if (copyOut != null) { |
68 |
111 |
safeCopy(getOut(true), exchange, copyOut); |
69 |
|
} |
70 |
111 |
Message copyFault = exchange.getFault(); |
71 |
111 |
if (copyFault != null) { |
72 |
0 |
safeCopy(getFault(), exchange, copyFault); |
73 |
|
} |
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
|
80 |
111 |
setException(exchange.getException()); |
81 |
111 |
} |
82 |
|
|
83 |
|
private static void safeCopy(Message message, Exchange exchange, Message that) { |
84 |
222 |
if (message != null) { |
85 |
222 |
message.copyFrom(that); |
86 |
|
} |
87 |
222 |
} |
88 |
|
|
89 |
|
private static Map<String, Object> safeCopy(Map<String, Object> properties) { |
90 |
111 |
if (properties == null) { |
91 |
0 |
return null; |
92 |
|
} |
93 |
111 |
return new HashMap<String, Object>(properties); |
94 |
|
} |
95 |
|
|
96 |
|
private static Message safeCopy(Exchange exchange, Message message) { |
97 |
0 |
if (message == null) { |
98 |
0 |
return null; |
99 |
|
} |
100 |
0 |
Message answer = message.copy(); |
101 |
0 |
if (answer instanceof MessageSupport) { |
102 |
0 |
MessageSupport messageSupport = (MessageSupport) answer; |
103 |
0 |
messageSupport.setExchange(exchange); |
104 |
|
} |
105 |
0 |
return answer; |
106 |
|
} |
107 |
|
|
108 |
|
public Exchange newInstance() { |
109 |
60 |
return new DefaultExchange(context); |
110 |
|
} |
111 |
|
|
112 |
|
public CamelContext getContext() { |
113 |
546 |
return context; |
114 |
|
} |
115 |
|
|
116 |
|
public Object getProperty(String name) { |
117 |
57 |
if (properties != null) { |
118 |
54 |
return properties.get(name); |
119 |
|
} |
120 |
3 |
return null; |
121 |
|
} |
122 |
|
|
123 |
|
public <T> T getProperty(String name, Class<T> type) { |
124 |
48 |
Object value = getProperty(name); |
125 |
48 |
return getContext().getTypeConverter().convertTo(type, value); |
126 |
|
} |
127 |
|
|
128 |
|
public void setProperty(String name, Object value) { |
129 |
72 |
getProperties().put(name, value); |
130 |
72 |
} |
131 |
|
|
132 |
|
public Map<String, Object> getProperties() { |
133 |
186 |
if (properties == null) { |
134 |
108 |
properties = new HashMap<String, Object>(); |
135 |
|
} |
136 |
186 |
return properties; |
137 |
|
} |
138 |
|
|
139 |
|
public void setProperties(Map<String, Object> properties) { |
140 |
111 |
this.properties = properties; |
141 |
111 |
} |
142 |
|
|
143 |
|
public Message getIn() { |
144 |
2745 |
if (in == null) { |
145 |
774 |
in = createInMessage(); |
146 |
774 |
configureMessage(in); |
147 |
|
} |
148 |
2745 |
return in; |
149 |
|
} |
150 |
|
|
151 |
|
public void setIn(Message in) { |
152 |
57 |
this.in = in; |
153 |
57 |
configureMessage(in); |
154 |
57 |
} |
155 |
|
|
156 |
|
public Message getOut() { |
157 |
702 |
return getOut(true); |
158 |
|
} |
159 |
|
|
160 |
|
public Message getOut(boolean lazyCreate) { |
161 |
813 |
if (out == null && lazyCreate) { |
162 |
669 |
out = createOutMessage(); |
163 |
669 |
configureMessage(out); |
164 |
|
} |
165 |
813 |
return out; |
166 |
|
} |
167 |
|
|
168 |
|
public void setOut(Message out) { |
169 |
0 |
this.out = out; |
170 |
0 |
configureMessage(out); |
171 |
0 |
} |
172 |
|
|
173 |
|
public Throwable getException() { |
174 |
129 |
return exception; |
175 |
|
} |
176 |
|
|
177 |
|
public void setException(Throwable exception) { |
178 |
135 |
this.exception = exception; |
179 |
135 |
} |
180 |
|
|
181 |
|
public Message getFault() { |
182 |
111 |
return fault; |
183 |
|
} |
184 |
|
|
185 |
|
public void setFault(Message fault) { |
186 |
0 |
this.fault = fault; |
187 |
0 |
configureMessage(fault); |
188 |
0 |
} |
189 |
|
|
190 |
|
public String getExchangeId() { |
191 |
0 |
return exchangeId; |
192 |
|
} |
193 |
|
|
194 |
|
public void setExchangeId(String id) { |
195 |
0 |
this.exchangeId = id; |
196 |
0 |
} |
197 |
|
|
198 |
|
|
199 |
|
|
200 |
|
|
201 |
|
protected Message createInMessage() { |
202 |
774 |
return new DefaultMessage(); |
203 |
|
} |
204 |
|
|
205 |
|
|
206 |
|
|
207 |
|
|
208 |
|
protected Message createOutMessage() { |
209 |
669 |
return new DefaultMessage(); |
210 |
|
} |
211 |
|
|
212 |
|
|
213 |
|
|
214 |
|
|
215 |
|
protected void configureMessage(Message message) { |
216 |
1500 |
if (message instanceof MessageSupport) { |
217 |
1500 |
MessageSupport messageSupport = (MessageSupport)message; |
218 |
1500 |
messageSupport.setExchange(this); |
219 |
|
} |
220 |
1500 |
} |
221 |
|
} |