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