Coverage Report - org.apache.camel.component.event.EventEndpoint
 
Classes in this File Line Coverage Branch Coverage Complexity
EventEndpoint
89% 
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.event;
 18  
 
 19  
 import org.apache.camel.Exchange;
 20  
 import org.apache.camel.Processor;
 21  
 import org.apache.camel.Producer;
 22  
 import org.apache.camel.RuntimeCamelException;
 23  
 import org.apache.camel.impl.DefaultEndpoint;
 24  
 import org.apache.camel.impl.DefaultExchange;
 25  
 import org.apache.camel.impl.DefaultProducer;
 26  
 import org.apache.camel.processor.loadbalancer.LoadBalancer;
 27  
 import org.apache.camel.processor.loadbalancer.TopicLoadBalancer;
 28  
 
 29  
 import org.springframework.context.ApplicationContext;
 30  
 import org.springframework.context.ApplicationEvent;
 31  
 
 32  
 /**
 33  
  * An <a href="http://activemq.apache.org/camel/event.html">Event Endpoint</a>
 34  
  * for working with Spring ApplicationEvents
 35  
  * 
 36  
  * @version $Revision: 1.1 $
 37  
  */
 38  1
 public class EventEndpoint extends DefaultEndpoint<Exchange> {
 39  
     private final EventComponent component;
 40  
     private LoadBalancer loadBalancer;
 41  
 
 42  
     public EventEndpoint(String endpointUri, EventComponent component) {
 43  56
         super(endpointUri, component);
 44  56
         this.component = component;
 45  56
     }
 46  
 
 47  
     @Override
 48  
     public EventComponent getComponent() {
 49  1
         return component;
 50  
     }
 51  
 
 52  
     public ApplicationContext getApplicationContext() {
 53  1
         return getComponent().getApplicationContext();
 54  
     }
 55  
 
 56  
     public boolean isSingleton() {
 57  59
         return true;
 58  
     }
 59  
 
 60  
     public Exchange createExchange() {
 61  83
         return new DefaultExchange(getContext());
 62  
     }
 63  
 
 64  
     public Producer<Exchange> createProducer() throws Exception {
 65  1
         return new DefaultProducer<Exchange>(this) {
 66  1
             public void process(Exchange exchange) throws Exception {
 67  1
                 ApplicationEvent event = toApplicationEvent(exchange);
 68  1
                 getApplicationContext().publishEvent(event);
 69  1
             }
 70  
         };
 71  
     }
 72  
 
 73  
     public EventConsumer createConsumer(Processor processor) throws Exception {
 74  1
         return new EventConsumer(this, processor);
 75  
     }
 76  
 
 77  
     public void onApplicationEvent(ApplicationEvent event) {
 78  82
         Exchange exchange = createExchange();
 79  82
         exchange.getIn().setBody(event);
 80  
         try {
 81  82
             getLoadBalancer().process(exchange);
 82  0
         } catch (Exception e) {
 83  0
             throw new RuntimeCamelException(e);
 84  82
         }
 85  82
     }
 86  
 
 87  
     public LoadBalancer getLoadBalancer() {
 88  84
         if (loadBalancer == null) {
 89  53
             loadBalancer = createLoadBalancer();
 90  
         }
 91  84
         return loadBalancer;
 92  
     }
 93  
 
 94  
     public void setLoadBalancer(LoadBalancer loadBalancer) {
 95  0
         this.loadBalancer = loadBalancer;
 96  0
     }
 97  
 
 98  
     // Implementation methods
 99  
     // -------------------------------------------------------------------------
 100  
     public synchronized void consumerStarted(EventConsumer consumer) {
 101  1
         getLoadBalancer().addProcessor(consumer.getProcessor());
 102  1
     }
 103  
 
 104  
     public synchronized void consumerStopped(EventConsumer consumer) {
 105  1
         getLoadBalancer().removeProcessor(consumer.getProcessor());
 106  1
     }
 107  
 
 108  
     protected LoadBalancer createLoadBalancer() {
 109  53
         return new TopicLoadBalancer();
 110  
     }
 111  
 
 112  
     protected ApplicationEvent toApplicationEvent(Exchange exchange) {
 113  1
         ApplicationEvent event = exchange.getIn().getBody(ApplicationEvent.class);
 114  1
         if (event == null) {
 115  1
             event = new CamelEvent(this, exchange);
 116  
         }
 117  1
         return event;
 118  
     }
 119  
 }