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