Coverage Report - org.apache.camel.CamelTemplate
 
Classes in this File Line Coverage Branch Coverage Complexity
CamelTemplate
54% 
86% 
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;
 18  
 
 19  
 import java.util.HashMap;
 20  
 import java.util.Map;
 21  
 
 22  
 import org.apache.camel.impl.ServiceSupport;
 23  
 import org.apache.camel.util.ObjectHelper;
 24  
 import org.apache.camel.util.ProducerCache;
 25  
 
 26  
 /**
 27  
  * A client helper object (named like Spring's TransactionTemplate & JmsTemplate
 28  
  * et al) for working with Camel and sending {@link Message} instances in an
 29  
  * {@link Exchange} to an {@link Endpoint}.
 30  
  * 
 31  
  * @version $Revision: 563607 $
 32  
  */
 33  
 public class CamelTemplate<E extends Exchange> extends ServiceSupport implements ProducerTemplate<E> {
 34  
     private CamelContext context;
 35  213
     private ProducerCache<E> producerCache = new ProducerCache<E>();
 36  213
     private boolean useEndpointCache = true;
 37  213
     private Map<String, Endpoint<E>> endpointCache = new HashMap<String, Endpoint<E>>();
 38  
     private Endpoint<E> defaultEndpoint;
 39  
 
 40  213
     public CamelTemplate(CamelContext context) {
 41  213
         this.context = context;
 42  213
     }
 43  
 
 44  
     public CamelTemplate(CamelContext context, Endpoint defaultEndpoint) {
 45  0
         this(context);
 46  0
         this.defaultEndpoint = defaultEndpoint;
 47  0
     }
 48  
 
 49  
     /**
 50  
      * Sends the exchange to the given endpoint
 51  
      * 
 52  
      * @param endpointUri the endpoint URI to send the exchange to
 53  
      * @param exchange the exchange to send
 54  
      */
 55  
     public E send(String endpointUri, E exchange) {
 56  9
         Endpoint endpoint = resolveMandatoryEndpoint(endpointUri);
 57  9
         send(endpoint, exchange);
 58  9
         return exchange;
 59  
     }
 60  
 
 61  
     /**
 62  
      * Sends an exchange to an endpoint using a supplied
 63  
      * 
 64  
      * @{link Processor} to populate the exchange
 65  
      * 
 66  
      * @param endpointUri the endpoint URI to send the exchange to
 67  
      * @param processor the transformer used to populate the new exchange
 68  
      */
 69  
     public E send(String endpointUri, Processor processor) {
 70  102
         Endpoint endpoint = resolveMandatoryEndpoint(endpointUri);
 71  96
         return send(endpoint, processor);
 72  
     }
 73  
 
 74  
     /**
 75  
      * Sends the exchange to the given endpoint
 76  
      * 
 77  
      * @param endpoint the endpoint to send the exchange to
 78  
      * @param exchange the exchange to send
 79  
      */
 80  
     public E send(Endpoint<E> endpoint, E exchange) {
 81  9
         E convertedExchange = endpoint.toExchangeType(exchange);
 82  9
         producerCache.send(endpoint, convertedExchange);
 83  9
         return exchange;
 84  
     }
 85  
 
 86  
     /**
 87  
      * Sends an exchange to an endpoint using a supplied
 88  
      * 
 89  
      * @{link Processor} to populate the exchange
 90  
      * 
 91  
      * @param endpoint the endpoint to send the exchange to
 92  
      * @param processor the transformer used to populate the new exchange
 93  
      */
 94  
     public E send(Endpoint<E> endpoint, Processor processor) {
 95  585
         return producerCache.send(endpoint, processor);
 96  
     }
 97  
 
 98  
     /**
 99  
      * Send the body to an endpoint
 100  
      * 
 101  
      * @param endpoint
 102  
      * @param body = the payload
 103  
      * @return the result
 104  
      */
 105  
     public Object sendBody(Endpoint<E> endpoint, final Object body) {
 106  0
         E result = send(endpoint, new Processor() {
 107  0
             public void process(Exchange exchange) {
 108  0
                 Message in = exchange.getIn();
 109  0
                 in.setBody(body);
 110  0
             }
 111  
         });
 112  0
         return extractResultBody(result);
 113  
     }
 114  
 
 115  
     /**
 116  
      * Send the body to an endpoint
 117  
      * 
 118  
      * @param endpointUri
 119  
      * @param body = the payload
 120  
      * @return the result
 121  
      */
 122  
     public Object sendBody(String endpointUri, final Object body) {
 123  48
         E result = send(endpointUri, new Processor() {
 124  48
             public void process(Exchange exchange) {
 125  42
                 Message in = exchange.getIn();
 126  42
                 in.setBody(body);
 127  42
             }
 128  
         });
 129  42
         return extractResultBody(result);
 130  
     }
 131  
 
 132  
     /**
 133  
      * Sends the body to an endpoint with a specified header and header value
 134  
      * 
 135  
      * @param endpointUri the endpoint URI to send to
 136  
      * @param body the payload send
 137  
      * @param header the header name
 138  
      * @param headerValue the header value
 139  
      * @return the result
 140  
      */
 141  
     public Object sendBodyAndHeader(String endpointUri, final Object body, final String header,
 142  
                                     final Object headerValue) {
 143  462
         return sendBodyAndHeader(resolveMandatoryEndpoint(endpointUri), body, header, headerValue);
 144  
     }
 145  
 
 146  
     /**
 147  
      * Sends the body to an endpoint with a specified header and header value
 148  
      * 
 149  
      * @param endpoint the Endpoint to send to
 150  
      * @param body the payload send
 151  
      * @param header the header name
 152  
      * @param headerValue the header value
 153  
      * @return the result
 154  
      */
 155  
     public Object sendBodyAndHeader(Endpoint endpoint, final Object body, final String header,
 156  
                                     final Object headerValue) {
 157  462
         E result = send(endpoint, new Processor() {
 158  462
             public void process(Exchange exchange) {
 159  462
                 Message in = exchange.getIn();
 160  462
                 in.setHeader(header, headerValue);
 161  462
                 in.setBody(body);
 162  462
             }
 163  
         });
 164  462
         return extractResultBody(result);
 165  
     }
 166  
 
 167  
     /**
 168  
      * Sends the body to an endpoint with the specified headers and header
 169  
      * values
 170  
      * 
 171  
      * @param endpointUri the endpoint URI to send to
 172  
      * @param body the payload send
 173  
      * @return the result
 174  
      */
 175  
     public Object sendBodyAndHeaders(String endpointUri, final Object body, final Map<String, Object> headers) {
 176  0
         return sendBodyAndHeaders(resolveMandatoryEndpoint(endpointUri), body, headers);
 177  
     }
 178  
 
 179  
     /**
 180  
      * Sends the body to an endpoint with the specified headers and header
 181  
      * values
 182  
      * 
 183  
      * @param endpoint the endpoint URI to send to
 184  
      * @param body the payload send
 185  
      * @return the result
 186  
      */
 187  
     public Object sendBodyAndHeaders(Endpoint endpoint, final Object body, final Map<String, Object> headers) {
 188  0
         E result = send(endpoint, new Processor() {
 189  0
             public void process(Exchange exchange) {
 190  0
                 Message in = exchange.getIn();
 191  0
                 for (Map.Entry<String, Object> header : headers.entrySet()) {
 192  0
                     in.setHeader(header.getKey(), header.getValue());
 193  0
                 }
 194  0
                 in.setBody(body);
 195  0
             }
 196  
         });
 197  0
         return extractResultBody(result);
 198  
     }
 199  
 
 200  
     // Methods using the default endpoint
 201  
     // -----------------------------------------------------------------------
 202  
 
 203  
     /**
 204  
      * Sends the body to the default endpoint and returns the result content
 205  
      * 
 206  
      * @param body the body to send
 207  
      * @return the returned message body
 208  
      */
 209  
     public Object sendBody(Object body) {
 210  0
         return sendBody(getMandatoryDefaultEndpoint(), body);
 211  
     }
 212  
 
 213  
     /**
 214  
      * Sends the exchange to the default endpoint
 215  
      * 
 216  
      * @param exchange the exchange to send
 217  
      */
 218  
     public E send(E exchange) {
 219  0
         return send(getMandatoryDefaultEndpoint(), exchange);
 220  
     }
 221  
 
 222  
     /**
 223  
      * Sends an exchange to the default endpoint using a supplied
 224  
      * 
 225  
      * @{link Processor} to populate the exchange
 226  
      * 
 227  
      * @param processor the transformer used to populate the new exchange
 228  
      */
 229  
     public E send(Processor processor) {
 230  0
         return send(getMandatoryDefaultEndpoint(), processor);
 231  
     }
 232  
 
 233  
     public Object sendBodyAndHeader(Object body, String header, Object headerValue) {
 234  0
         return sendBodyAndHeader(getMandatoryDefaultEndpoint(), body, header, headerValue);
 235  
     }
 236  
 
 237  
     public Object sendBodyAndHeaders(Object body, Map<String, Object> headers) {
 238  0
         return sendBodyAndHeaders(getMandatoryDefaultEndpoint(), body, headers);
 239  
     }
 240  
 
 241  
     // Properties
 242  
     // -----------------------------------------------------------------------
 243  
     public Producer<E> getProducer(Endpoint<E> endpoint) {
 244  0
         return producerCache.getProducer(endpoint);
 245  
     }
 246  
 
 247  
     public CamelContext getContext() {
 248  0
         return context;
 249  
     }
 250  
 
 251  
     public Endpoint<E> getDefaultEndpoint() {
 252  0
         return defaultEndpoint;
 253  
     }
 254  
 
 255  
     public void setDefaultEndpoint(Endpoint<E> defaultEndpoint) {
 256  0
         this.defaultEndpoint = defaultEndpoint;
 257  0
     }
 258  
 
 259  
     /**
 260  
      * Sets the default endpoint to use if none is specified
 261  
      */
 262  
     public void setDefaultEndpointUri(String endpointUri) {
 263  0
         setDefaultEndpoint(getContext().getEndpoint(endpointUri));
 264  0
     }
 265  
 
 266  
     public boolean isUseEndpointCache() {
 267  573
         return useEndpointCache;
 268  
     }
 269  
 
 270  
     public void setUseEndpointCache(boolean useEndpointCache) {
 271  0
         this.useEndpointCache = useEndpointCache;
 272  0
     }
 273  
 
 274  
     // Implementation methods
 275  
     // -----------------------------------------------------------------------
 276  
 
 277  
     protected Endpoint resolveMandatoryEndpoint(String endpointUri) {
 278  573
         Endpoint endpoint = null;
 279  
 
 280  573
         if (isUseEndpointCache()) {
 281  573
             synchronized (endpointCache) {
 282  573
                 endpoint = endpointCache.get(endpointUri);
 283  573
                 if (endpoint == null) {
 284  180
                     endpoint = context.getEndpoint(endpointUri);
 285  177
                     if (endpoint != null) {
 286  174
                         endpointCache.put(endpointUri, endpoint);
 287  
                     }
 288  
                 }
 289  570
             }
 290  570
         } else {
 291  0
             endpoint = context.getEndpoint(endpointUri);
 292  
         }
 293  570
         if (endpoint == null) {
 294  3
             throw new NoSuchEndpointException(endpointUri);
 295  
         }
 296  567
         return endpoint;
 297  
     }
 298  
 
 299  
     protected Endpoint<E> getMandatoryDefaultEndpoint() {
 300  0
         Endpoint<E> answer = getDefaultEndpoint();
 301  0
         ObjectHelper.notNull(answer, "defaultEndpoint");
 302  0
         return answer;
 303  
     }
 304  
 
 305  
     protected void doStart() throws Exception {
 306  0
         producerCache.start();
 307  0
     }
 308  
 
 309  
     protected void doStop() throws Exception {
 310  0
         producerCache.stop();
 311  0
     }
 312  
 
 313  
     protected Object extractResultBody(E result) {
 314  504
         Object answer = null;
 315  504
         if (result != null) {
 316  504
             answer = result.getOut().getBody();
 317  504
             if (answer == null) {
 318  504
                 answer = result.getIn().getBody();
 319  
             }
 320  
         }
 321  504
         return answer;
 322  
     }
 323  
 }