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.CamelContext; 020 import org.apache.camel.Exchange; 021 import org.apache.camel.Message; 022 import org.apache.camel.NoTypeConversionAvailableException; 023 import org.apache.camel.TypeConverter; 024 import org.apache.camel.impl.converter.DefaultTypeConverter; 025 import org.apache.camel.util.UuidGenerator; 026 027 /** 028 * A base class for implementation inheritence providing the core 029 * {@link Message} body handling features but letting the derived class deal 030 * with headers. 031 * 032 * Unless a specific provider wishes to do something particularly clever with 033 * headers you probably want to just derive from {@link DefaultMessage} 034 * 035 * @version $Revision: 749956 $ 036 */ 037 public abstract class MessageSupport implements Message { 038 private static final UuidGenerator DEFALT_ID_GENERATOR = new UuidGenerator(); 039 private Exchange exchange; 040 private Object body; 041 private String messageId; 042 043 public Object getBody() { 044 if (body == null) { 045 body = createBody(); 046 } 047 return body; 048 } 049 050 @SuppressWarnings("unchecked") 051 public <T> T getBody(Class<T> type) { 052 return getBody(type, getBody()); 053 } 054 055 @SuppressWarnings("unchecked") 056 protected <T> T getBody(Class<T> type, Object body) { 057 Exchange e = getExchange(); 058 if (e != null) { 059 CamelContext camelContext = e.getContext(); 060 if (camelContext != null) { 061 boolean tryConvert = true; 062 TypeConverter converter = camelContext.getTypeConverter(); 063 // if its the default type converter then use a performance shortcut to check if it can convert it 064 // this is faster than getting throwing and catching NoTypeConversionAvailableException 065 // the StreamCachingInterceptor will attempt to convert the payload to a StremCache for caching purpose 066 // so we get invoked on each node the exchange passes. So this is a little performance optimization 067 // to avoid the excessive exception handling 068 if (body != null && converter instanceof DefaultTypeConverter) { 069 DefaultTypeConverter defaultTypeConverter = (DefaultTypeConverter) converter; 070 // we can only check if there is no converter meaning we have tried to convert it beforehand 071 // and then knows for sure there is no converter possible 072 tryConvert = !defaultTypeConverter.hasNoConverterFor(type, body.getClass()); 073 } 074 if (tryConvert) { 075 try { 076 // lets first try converting the body itself first 077 // as for some types like InputStream v Reader its more efficient to do the transformation 078 // from the body itself as its got efficient implementations of them, before trying the 079 // message 080 return converter.convertTo(type, e, body); 081 } catch (NoTypeConversionAvailableException ex) { 082 // ignore 083 } 084 } 085 // fallback to the message itself 086 return converter.convertTo(type, this); 087 } 088 } 089 return (T)getBody(); 090 } 091 092 public void setBody(Object body) { 093 this.body = body; 094 } 095 096 public <T> void setBody(Object value, Class<T> type) { 097 Exchange e = getExchange(); 098 if (e != null) { 099 T v = e.getContext().getTypeConverter().convertTo(type, e, value); 100 if (v != null) { 101 value = v; 102 } 103 } 104 setBody(value); 105 } 106 107 public Message copy() { 108 Message answer = newInstance(); 109 answer.copyFrom(this); 110 return answer; 111 } 112 113 public void copyFrom(Message that) { 114 setMessageId(that.getMessageId()); 115 setBody(that.getBody()); 116 getHeaders().putAll(that.getHeaders()); 117 getAttachments().putAll(that.getAttachments()); 118 } 119 120 public Exchange getExchange() { 121 return exchange; 122 } 123 124 public void setExchange(Exchange exchange) { 125 this.exchange = exchange; 126 } 127 128 /** 129 * Returns a new instance 130 */ 131 public abstract Message newInstance(); 132 133 /** 134 * A factory method to allow a provider to lazily create the message body 135 * for inbound messages from other sources 136 * 137 * @return the value of the message body or null if there is no value 138 * available 139 */ 140 protected Object createBody() { 141 return null; 142 } 143 144 public String getMessageId() { 145 if (messageId == null) { 146 messageId = createMessageId(); 147 } 148 return this.messageId; 149 } 150 151 public void setMessageId(String messageId) { 152 this.messageId = messageId; 153 } 154 155 /** 156 * Lets allow implementations to auto-create a messageId 157 */ 158 protected String createMessageId() { 159 return DEFALT_ID_GENERATOR.generateId(); 160 } 161 }