Coverage Report - org.apache.camel.util.ProducerCache
 
Classes in this File Line Coverage Branch Coverage Complexity
ProducerCache
68% 
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.util;
 18  
 
 19  
 import java.util.HashMap;
 20  
 import java.util.Map;
 21  
 
 22  
 import org.apache.camel.Endpoint;
 23  
 import org.apache.camel.Exchange;
 24  
 import org.apache.camel.FailedToCreateProducerException;
 25  
 import org.apache.camel.Processor;
 26  
 import org.apache.camel.Producer;
 27  
 import org.apache.camel.RuntimeCamelException;
 28  
 import org.apache.camel.impl.ServiceSupport;
 29  
 import org.apache.commons.logging.Log;
 30  
 import org.apache.commons.logging.LogFactory;
 31  
 
 32  
 /**
 33  
  * @version $Revision: 563607 $
 34  
  */
 35  468
 public class ProducerCache<E extends Exchange> extends ServiceSupport {
 36  3
     private static final Log LOG = LogFactory.getLog(ProducerCache.class);
 37  
 
 38  468
     private Map<String, Producer<E>> producers = new HashMap<String, Producer<E>>();
 39  
 
 40  
     public synchronized Producer<E> getProducer(Endpoint<E> endpoint) {
 41  603
         String key = endpoint.getEndpointUri();
 42  603
         Producer<E> answer = producers.get(key);
 43  603
         if (answer == null) {
 44  
             try {
 45  189
                 answer = endpoint.createProducer();
 46  189
                 answer.start();
 47  0
             } catch (Exception e) {
 48  0
                 throw new FailedToCreateProducerException(endpoint, e);
 49  189
             }
 50  189
             producers.put(key, answer);
 51  
         }
 52  603
         return answer;
 53  
     }
 54  
 
 55  
     /**
 56  
      * Sends the exchange to the given endpoint
 57  
      * 
 58  
      * @param endpoint the endpoint to send the exchange to
 59  
      * @param exchange the exchange to send
 60  
      */
 61  
     public void send(Endpoint<E> endpoint, E exchange) {
 62  
         try {
 63  9
             Producer<E> producer = getProducer(endpoint);
 64  9
             producer.process(exchange);
 65  0
         } catch (Exception e) {
 66  0
             throw new RuntimeCamelException(e);
 67  9
         }
 68  9
     }
 69  
 
 70  
     /**
 71  
      * Sends an exchange to an endpoint using a supplied
 72  
      * 
 73  
      * @{link Processor} to populate the exchange
 74  
      * 
 75  
      * @param endpoint the endpoint to send the exchange to
 76  
      * @param processor the transformer used to populate the new exchange
 77  
      */
 78  
     public E send(Endpoint<E> endpoint, Processor processor) {
 79  
         try {
 80  585
             Producer<E> producer = getProducer(endpoint);
 81  585
             E exchange = producer.createExchange();
 82  
 
 83  
             // lets populate using the processor callback
 84  585
             processor.process(exchange);
 85  
 
 86  
             // now lets dispatch
 87  585
             if (LOG.isDebugEnabled()) {
 88  0
                 LOG.debug(">>>> " + endpoint + " " + exchange);
 89  
             }
 90  585
             producer.process(exchange);
 91  585
             return exchange;
 92  0
         } catch (Exception e) {
 93  0
             throw new RuntimeCamelException(e);
 94  
         }
 95  
     }
 96  
 
 97  
     protected void doStop() throws Exception {
 98  0
         ServiceHelper.stopServices(producers.values());
 99  0
     }
 100  
 
 101  
     protected void doStart() throws Exception {
 102  0
     }
 103  
 }