Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
MessageSupport |
|
| 0.0;0 |
1 | /** |
|
2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
|
3 | * contributor license agreements. See the NOTICE file distributed with |
|
4 | * this work for additional information regarding copyright ownership. |
|
5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
|
6 | * (the "License"); you may not use this file except in compliance with |
|
7 | * the License. You may obtain a copy of the License at |
|
8 | * |
|
9 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
10 | * |
|
11 | * Unless required by applicable law or agreed to in writing, software |
|
12 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14 | * See the License for the specific language governing permissions and |
|
15 | * limitations under the License. |
|
16 | */ |
|
17 | package org.apache.camel.impl; |
|
18 | ||
19 | import org.apache.camel.Exchange; |
|
20 | import org.apache.camel.Message; |
|
21 | import org.apache.camel.util.UuidGenerator; |
|
22 | ||
23 | /** |
|
24 | * A base class for implementation inheritence providing the core |
|
25 | * {@link Message} body handling features but letting the derived class deal |
|
26 | * with headers. |
|
27 | * |
|
28 | * Unless a specific provider wishes to do something particularly clever with |
|
29 | * headers you probably want to just derive from {@link DefaultMessage} |
|
30 | * |
|
31 | * @version $Revision: 564677 $ |
|
32 | */ |
|
33 | 1500 | public abstract class MessageSupport implements Message { |
34 | 3 | private static final UuidGenerator DEFALT_ID_GENERATOR = new UuidGenerator(); |
35 | private Exchange exchange; |
|
36 | private Object body; |
|
37 | 1500 | private String messageId = DEFALT_ID_GENERATOR.generateId(); |
38 | ||
39 | public Object getBody() { |
|
40 | 2490 | if (body == null) { |
41 | 705 | body = createBody(); |
42 | } |
|
43 | 2490 | return body; |
44 | } |
|
45 | ||
46 | @SuppressWarnings({"unchecked" }) |
|
47 | public <T> T getBody(Class<T> type) { |
|
48 | 255 | Exchange e = getExchange(); |
49 | 255 | if (e != null) { |
50 | 255 | return e.getContext().getTypeConverter().convertTo(type, getBody()); |
51 | } |
|
52 | 0 | return (T)getBody(); |
53 | } |
|
54 | ||
55 | public void setBody(Object body) { |
|
56 | 963 | this.body = body; |
57 | 963 | } |
58 | ||
59 | public <T> void setBody(Object value, Class<T> type) { |
|
60 | 0 | Exchange e = getExchange(); |
61 | 0 | if (e != null) { |
62 | 0 | T v = e.getContext().getTypeConverter().convertTo(type, value); |
63 | 0 | if (v != null) { |
64 | 0 | value = v; |
65 | } |
|
66 | } |
|
67 | 0 | setBody(value); |
68 | 0 | } |
69 | ||
70 | public Message copy() { |
|
71 | 0 | Message answer = newInstance(); |
72 | 0 | answer.copyFrom(this); |
73 | 0 | return answer; |
74 | } |
|
75 | ||
76 | public void copyFrom(Message that) { |
|
77 | 222 | setMessageId(that.getMessageId()); |
78 | 222 | setBody(that.getBody()); |
79 | 222 | getHeaders().putAll(that.getHeaders()); |
80 | 222 | } |
81 | ||
82 | public Exchange getExchange() { |
|
83 | 318 | return exchange; |
84 | } |
|
85 | ||
86 | public void setExchange(Exchange exchange) { |
|
87 | 1500 | this.exchange = exchange; |
88 | 1500 | } |
89 | ||
90 | /** |
|
91 | * Returns a new instance |
|
92 | * |
|
93 | * @return |
|
94 | */ |
|
95 | public abstract Message newInstance(); |
|
96 | ||
97 | /** |
|
98 | * A factory method to allow a provider to lazily create the message body |
|
99 | * for inbound messages from other sources |
|
100 | * |
|
101 | * @return the value of the message body or null if there is no value |
|
102 | * available |
|
103 | */ |
|
104 | protected Object createBody() { |
|
105 | 672 | return null; |
106 | } |
|
107 | ||
108 | /** |
|
109 | * @return the messageId |
|
110 | */ |
|
111 | public String getMessageId() { |
|
112 | 240 | return this.messageId; |
113 | } |
|
114 | ||
115 | /** |
|
116 | * @param messageId the messageId to set |
|
117 | */ |
|
118 | public void setMessageId(String messageId) { |
|
119 | 222 | this.messageId = messageId; |
120 | 222 | } |
121 | } |