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.TypeConverter;
022    import org.apache.camel.util.UuidGenerator;
023    
024    /**
025     * A base class for implementation inheritence providing the core
026     * {@link Message} body handling features but letting the derived class deal
027     * with headers.
028     * 
029     * Unless a specific provider wishes to do something particularly clever with
030     * headers you probably want to just derive from {@link DefaultMessage}
031     * 
032     * @version $Revision: 644383 $
033     */
034    public abstract class MessageSupport implements Message {
035        private static final UuidGenerator DEFALT_ID_GENERATOR = new UuidGenerator();
036        private Exchange exchange;
037        private Object body;
038        private String messageId = DEFALT_ID_GENERATOR.generateId();
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                TypeConverter converter = e.getContext().getTypeConverter();
052                T answer = converter.convertTo(type, getBody());
053                if (answer == null) {
054                    // lets first try converting the message itself first
055                    // as for some types like InputStream v Reader its more efficient to do the transformation
056                    // from the Message itself as its got efficient implementations of them, before trying the
057                    // payload
058                    answer = converter.convertTo(type, this);
059                }
060                return answer;
061            }
062            return (T)getBody();
063        }
064    
065        public void setBody(Object body) {
066            this.body = body;
067        }
068    
069        public <T> void setBody(Object value, Class<T> type) {
070            Exchange e = getExchange();
071            if (e != null) {
072                T v = e.getContext().getTypeConverter().convertTo(type, value);
073                if (v != null) {
074                    value = v;
075                }
076            }
077            setBody(value);
078        }
079    
080        public Message copy() {
081            Message answer = newInstance();
082            answer.copyFrom(this);
083            return answer;
084        }
085    
086        public void copyFrom(Message that) {
087            setMessageId(that.getMessageId());
088            setBody(that.getBody());
089            getHeaders().putAll(that.getHeaders());
090        }
091    
092        public Exchange getExchange() {
093            return exchange;
094        }
095    
096        public void setExchange(Exchange exchange) {
097            this.exchange = exchange;
098        }
099    
100        /**
101         * Returns a new instance
102         */
103        public abstract Message newInstance();
104    
105        /**
106         * A factory method to allow a provider to lazily create the message body
107         * for inbound messages from other sources
108         * 
109         * @return the value of the message body or null if there is no value
110         *         available
111         */
112        protected Object createBody() {
113            return null;
114        }
115    
116        /**
117         * @return the messageId
118         */
119        public String getMessageId() {
120            return this.messageId;
121        }
122    
123        /**
124         * @param messageId the messageId to set
125         */
126        public void setMessageId(String messageId) {
127            this.messageId = messageId;
128        }
129    }