Coverage Report - org.apache.camel.impl.DefaultExchange
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultExchange
93% 
100% 
0
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.camel.impl;
 18  
 
 19  
 import org.apache.camel.CamelContext;
 20  
 import org.apache.camel.Exchange;
 21  
 import org.apache.camel.Message;
 22  
 import org.apache.camel.util.UuidGenerator;
 23  
 
 24  
 import java.util.HashMap;
 25  
 import java.util.Map;
 26  
 
 27  
 /**
 28  
  * A default implementation of {@link Exchange}
 29  
  *
 30  
  * @version $Revision: 550760 $
 31  
  */
 32  
 public class DefaultExchange implements Exchange {
 33  1
     private static final UuidGenerator defaultIdGenerator = new UuidGenerator();
 34  
     protected final CamelContext context;
 35  
     private Map<String, Object> headers;
 36  
     private Message in;
 37  
     private Message out;
 38  
     private Message fault;
 39  
     private Throwable exception;
 40  792
     private String exchangeId = DefaultExchange.defaultIdGenerator.generateId();
 41  
 
 42  792
     public DefaultExchange(CamelContext context) {
 43  792
         this.context = context;
 44  792
     }
 45  
 
 46  
     @Override
 47  
     public String toString() {
 48  85
         return "Exchange[" + in + "]";
 49  
     }
 50  
 
 51  
     public Exchange copy() {
 52  4
         Exchange exchange = newInstance();
 53  4
         exchange.copyFrom(this);
 54  4
         return exchange;
 55  
     }
 56  
 
 57  
     public void copyFrom(Exchange exchange) {
 58  17
         if (exchange == this) {
 59  5
             return;
 60  
         }
 61  12
         setHeaders(safeCopy(exchange.getProperties()));
 62  12
         setIn(safeCopy(exchange.getIn()));
 63  12
         setOut(safeCopy(exchange.getOut()));
 64  12
                setFault(safeCopy(exchange.getFault()));        
 65  12
         setException(exchange.getException());
 66  12
     }
 67  
 
 68  
     static private Map<String, Object> safeCopy(Map<String, Object> properties) {
 69  12
                 if(properties == null)
 70  0
                         return null;
 71  12
                 return new HashMap<String, Object>(properties);
 72  
         }
 73  
 
 74  
         static private Message safeCopy(Message message) {
 75  36
             if( message == null)
 76  12
                     return null;
 77  24
             return message.copy();
 78  
         }
 79  
 
 80  
         public Exchange newInstance() {
 81  4
         return new DefaultExchange(context);
 82  
     }
 83  
 
 84  
     public CamelContext getContext() {
 85  98
         return context;
 86  
     }
 87  
 
 88  
     public Object getProperty(String name) {
 89  1
         if (headers != null) {
 90  1
             return headers.get(name);
 91  
         }
 92  0
         return null;
 93  
     }
 94  
 
 95  
     public <T> T getProperty(String name, Class<T> type) {
 96  1
         Object value = getProperty(name);
 97  1
         return getContext().getTypeConverter().convertTo(type, value);
 98  
     }
 99  
 
 100  
     public void setProperty(String name, Object value) {
 101  3
         getProperties().put(name, value);
 102  3
     }
 103  
 
 104  
     public Map<String, Object> getProperties() {
 105  16
         if (headers == null) {
 106  9
             headers = new HashMap<String, Object>();
 107  
         }
 108  16
         return headers;
 109  
     }
 110  
 
 111  
     public void setHeaders(Map<String, Object> headers) {
 112  12
         this.headers = headers;
 113  12
     }
 114  
 
 115  
     public Message getIn() {
 116  929
         if (in == null) {
 117  62
             in = createInMessage();
 118  62
             configureMessage(in);
 119  
         }
 120  929
         return in;
 121  
     }
 122  
 
 123  
     public void setIn(Message in) {
 124  723
         this.in = in;
 125  723
         configureMessage(in);
 126  723
     }
 127  
 
 128  
     public Message getOut() {
 129  33
         return getOut(true);
 130  
     }
 131  
 
 132  
     public Message getOut(boolean lazyCreate) {
 133  33
         if (out == null && lazyCreate) {
 134  15
             out = createOutMessage();
 135  15
             configureMessage(out);
 136  
         }
 137  33
         return out;
 138  
     }
 139  
 
 140  
     public void setOut(Message out) {
 141  12
         this.out = out;
 142  12
         configureMessage(out);
 143  12
     }
 144  
 
 145  
     public Throwable getException() {
 146  17
         return exception;
 147  
     }
 148  
 
 149  
     public void setException(Throwable exception) {
 150  12
         this.exception = exception;
 151  12
     }
 152  
 
 153  
     public Message getFault() {
 154  12
         return fault;
 155  
     }
 156  
 
 157  
     public void setFault(Message fault) {
 158  12
         this.fault = fault;
 159  12
         configureMessage(fault);
 160  12
     }
 161  
 
 162  
     public String getExchangeId() {
 163  0
         return exchangeId;
 164  
     }
 165  
 
 166  
     public void setExchangeId(String id) {
 167  0
         this.exchangeId = id;
 168  0
     }
 169  
 
 170  
     /**
 171  
      * Factory method used to lazily create the IN message
 172  
      */
 173  
     protected Message createInMessage() {
 174  62
         return new DefaultMessage();
 175  
     }
 176  
 
 177  
     /**
 178  
      * Factory method to lazily create the OUT message
 179  
      */
 180  
     protected Message createOutMessage() {
 181  15
         return new DefaultMessage();
 182  
     }
 183  
 
 184  
     /**
 185  
      * Configures the message after it has been set on the exchange
 186  
      */
 187  
     protected void configureMessage(Message message) {
 188  824
         if (message instanceof MessageSupport) {
 189  812
             MessageSupport messageSupport = (MessageSupport) message;
 190  812
             messageSupport.setExchange(this);
 191  
         }
 192  824
     }
 193  
 }