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