Coverage Report - org.apache.camel.component.direct.DirectEndpoint
 
Classes in this File Line Coverage Branch Coverage Complexity
DirectEndpoint
88% 
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.component.direct;
 18  
 
 19  
 import java.util.concurrent.CopyOnWriteArrayList;
 20  
 
 21  
 import org.apache.camel.Consumer;
 22  
 import org.apache.camel.Exchange;
 23  
 import org.apache.camel.Processor;
 24  
 import org.apache.camel.Producer;
 25  
 import org.apache.camel.impl.DefaultConsumer;
 26  
 import org.apache.camel.impl.DefaultEndpoint;
 27  
 import org.apache.camel.impl.DefaultExchange;
 28  
 import org.apache.camel.impl.DefaultProducer;
 29  
 import org.apache.camel.util.ProducerCache;
 30  
 import org.apache.commons.logging.Log;
 31  
 import org.apache.commons.logging.LogFactory;
 32  
 
 33  
 /**
 34  
  * Represents a direct endpoint that synchronously invokes the consumers of the endpoint when a producer 
 35  
  * sends a message to it.
 36  
  *
 37  
  * @org.apache.xbean.XBean
 38  
  * @version $Revision: 519973 $
 39  
  */
 40  73
 public class DirectEndpoint<E extends Exchange> extends DefaultEndpoint<E> {
 41  1
     private static final Log log = LogFactory.getLog(DirectEndpoint.class);
 42  
 
 43  38
         private final CopyOnWriteArrayList<DefaultConsumer<E>> consumers = new CopyOnWriteArrayList<DefaultConsumer<E>>();
 44  
         
 45  38
         boolean allowMultipleConsumers=true;        
 46  
         
 47  
     public DirectEndpoint(String uri, DirectComponent<E> component) {
 48  38
         super(uri, component);
 49  38
     }
 50  
 
 51  
     public Producer createProducer() throws Exception {
 52  38
         return new DefaultProducer(this) {
 53  38
             public void process(Exchange exchange) throws Exception {
 54  48
                     DirectEndpoint.this.process(exchange);
 55  48
             }
 56  
         };            
 57  
     }
 58  
 
 59  
     protected void process(Exchange exchange) throws Exception {
 60  48
             if (consumers.isEmpty()) {
 61  1
                     log.warn("No consumers available on " + this + " for " + exchange);
 62  1
             }
 63  
             else {
 64  47
                     for (DefaultConsumer<E> consumer : consumers) {
 65  47
                                 consumer.getProcessor().process(exchange);
 66  47
                         }
 67  
             }
 68  48
     }
 69  
 
 70  
         public Consumer<E> createConsumer(Processor processor) throws Exception {
 71  37
                 return new DefaultConsumer<E>(this, processor) {
 72  
                         @Override
 73  
                         public void start() throws Exception {
 74  37
                                 if( !allowMultipleConsumers && !consumers.isEmpty() )
 75  0
                                         throw new IllegalStateException("Endpoint "+getEndpointUri()+" only allows 1 active consumer but you attempted to start a 2nd consumer.");
 76  
                                 
 77  37
                                 consumers.add(this);
 78  37
                                 super.start();
 79  37
                         }
 80  
                         
 81  
                         @Override
 82  37
                         public void stop() throws Exception {
 83  36
                                 super.stop();
 84  36
                                 consumers.remove(this);
 85  36
                         }
 86  
                 };
 87  
     }
 88  
 
 89  
     public E createExchange() {
 90  
             // How can we create a specific Exchange if we are generic??
 91  
             // perhaps it would be better if we did not implement this. 
 92  41
         return (E) new DefaultExchange(getContext());
 93  
     }
 94  
 
 95  
         public boolean isAllowMultipleConsumers() {
 96  0
                 return allowMultipleConsumers;
 97  
         }
 98  
         public void setAllowMultipleConsumers(boolean allowMutlipleConsumers) {
 99  0
                 this.allowMultipleConsumers = allowMutlipleConsumers;
 100  0
         }
 101  
 
 102  
         public boolean isSingleton() {
 103  38
                 return true;
 104  
         }
 105  
 
 106  
 }