001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.component.event;
018    
019    import org.apache.camel.Exchange;
020    import org.apache.camel.NoTypeConversionAvailableException;
021    import org.apache.camel.Processor;
022    import org.apache.camel.Producer;
023    import org.apache.camel.impl.DefaultEndpoint;
024    import org.apache.camel.impl.DefaultProducer;
025    import org.apache.camel.processor.loadbalancer.LoadBalancer;
026    import org.apache.camel.processor.loadbalancer.TopicLoadBalancer;
027    import org.springframework.beans.BeansException;
028    import org.springframework.context.ApplicationContext;
029    import org.springframework.context.ApplicationContextAware;
030    import org.springframework.context.ApplicationEvent;
031    
032    import static org.apache.camel.util.ObjectHelper.wrapRuntimeCamelException;
033    /**
034     * An <a href="http://activemq.apache.org/camel/event.html">Event Endpoint</a>
035     * for working with Spring ApplicationEvents
036     *
037     * @version $Revision: 52200 $
038     */
039    public class EventEndpoint extends DefaultEndpoint<Exchange> implements ApplicationContextAware {
040        private LoadBalancer loadBalancer;
041        private ApplicationContext applicationContext;
042    
043        public EventEndpoint(String endpointUri, EventComponent component) {
044            super(endpointUri, component);
045            this.applicationContext = component.getApplicationContext();
046        }
047    
048        public EventEndpoint(String endpointUri) {
049            super(endpointUri);
050        }
051    
052        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
053            this.applicationContext = applicationContext;
054        }
055    
056        public ApplicationContext getApplicationContext() {
057            return applicationContext;
058        }
059    
060        public boolean isSingleton() {
061            return true;
062        }
063    
064        public Producer<Exchange> createProducer() throws Exception {
065            return new DefaultProducer<Exchange>(this) {
066                public void process(Exchange exchange) throws Exception {
067                    ApplicationEvent event = toApplicationEvent(exchange);
068                    getApplicationContext().publishEvent(event);
069                }
070            };
071        }
072    
073        public EventConsumer createConsumer(Processor processor) throws Exception {
074            return new EventConsumer(this, processor);
075        }
076    
077        public void onApplicationEvent(ApplicationEvent event) {
078            Exchange exchange = createExchange();
079            exchange.getIn().setBody(event);
080            try {
081                getLoadBalancer().process(exchange);
082            } catch (Exception e) {
083                throw wrapRuntimeCamelException(e);
084            }
085        }
086    
087        public LoadBalancer getLoadBalancer() {
088            if (loadBalancer == null) {
089                loadBalancer = createLoadBalancer();
090            }
091            return loadBalancer;
092        }
093    
094        public void setLoadBalancer(LoadBalancer loadBalancer) {
095            this.loadBalancer = loadBalancer;
096        }
097    
098        // Implementation methods
099        // -------------------------------------------------------------------------
100        public synchronized void consumerStarted(EventConsumer consumer) {
101            getLoadBalancer().addProcessor(consumer.getProcessor());
102        }
103    
104        public synchronized void consumerStopped(EventConsumer consumer) {
105            getLoadBalancer().removeProcessor(consumer.getProcessor());
106        }
107    
108        protected LoadBalancer createLoadBalancer() {
109            return new TopicLoadBalancer();
110        }
111    
112        protected ApplicationEvent toApplicationEvent(Exchange exchange) {
113            try {
114                ApplicationEvent event = exchange.getIn().getBody(ApplicationEvent.class);
115                if (event != null) {
116                    return event;
117                }
118            } catch (NoTypeConversionAvailableException ex) {
119                // ignore, handled below
120            }
121            return new CamelEvent(this, exchange);
122        }
123    }