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