001 /** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.camel.impl; 018 019 import org.apache.camel.Exchange; 020 import org.apache.camel.Message; 021 import org.apache.camel.util.UuidGenerator; 022 023 /** 024 * A base class for implementation inheritence providing the core 025 * {@link Message} body handling features but letting the derived class deal 026 * with headers. 027 * 028 * Unless a specific provider wishes to do something particularly clever with 029 * headers you probably want to just derive from {@link DefaultMessage} 030 * 031 * @version $Revision: 564677 $ 032 */ 033 public abstract class MessageSupport implements Message { 034 private static final UuidGenerator DEFALT_ID_GENERATOR = new UuidGenerator(); 035 private Exchange exchange; 036 private Object body; 037 private String messageId = DEFALT_ID_GENERATOR.generateId(); 038 039 public Object getBody() { 040 if (body == null) { 041 body = createBody(); 042 } 043 return body; 044 } 045 046 @SuppressWarnings({"unchecked" }) 047 public <T> T getBody(Class<T> type) { 048 Exchange e = getExchange(); 049 if (e != null) { 050 return e.getContext().getTypeConverter().convertTo(type, getBody()); 051 } 052 return (T)getBody(); 053 } 054 055 public void setBody(Object body) { 056 this.body = body; 057 } 058 059 public <T> void setBody(Object value, Class<T> type) { 060 Exchange e = getExchange(); 061 if (e != null) { 062 T v = e.getContext().getTypeConverter().convertTo(type, value); 063 if (v != null) { 064 value = v; 065 } 066 } 067 setBody(value); 068 } 069 070 public Message copy() { 071 Message answer = newInstance(); 072 answer.copyFrom(this); 073 return answer; 074 } 075 076 public void copyFrom(Message that) { 077 setMessageId(that.getMessageId()); 078 setBody(that.getBody()); 079 getHeaders().putAll(that.getHeaders()); 080 } 081 082 public Exchange getExchange() { 083 return exchange; 084 } 085 086 public void setExchange(Exchange exchange) { 087 this.exchange = exchange; 088 } 089 090 /** 091 * Returns a new instance 092 * 093 * @return 094 */ 095 public abstract Message newInstance(); 096 097 /** 098 * A factory method to allow a provider to lazily create the message body 099 * 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 return null; 106 } 107 108 /** 109 * @return the messageId 110 */ 111 public String getMessageId() { 112 return this.messageId; 113 } 114 115 /** 116 * @param messageId the messageId to set 117 */ 118 public void setMessageId(String messageId) { 119 this.messageId = messageId; 120 } 121 }