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.Processor;
021    import org.apache.camel.Producer;
022    import org.apache.camel.impl.DefaultEndpoint;
023    import org.apache.camel.impl.DefaultProducer;
024    import org.apache.camel.processor.loadbalancer.LoadBalancer;
025    import org.apache.camel.processor.loadbalancer.TopicLoadBalancer;
026    import org.apache.camel.util.ObjectHelper;
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    
035    /**
036     * An <a href="http://camel.apache.org/event.html">Event Endpoint</a>
037     * for working with Spring ApplicationEvents
038     *
039     * @version 
040     */
041    public class EventEndpoint extends DefaultEndpoint implements ApplicationContextAware {
042        private LoadBalancer loadBalancer;
043        private ApplicationContext applicationContext;
044    
045        public EventEndpoint(String endpointUri, EventComponent component) {
046            super(endpointUri, component);
047            this.applicationContext = component.getApplicationContext();
048        }
049    
050        /**
051         * <b>Note:</b> It is preferred to create endpoints using the associated
052         * component.
053         * @param endpointUri
054         */
055        @Deprecated
056        public EventEndpoint(String endpointUri) {
057            super(endpointUri);
058        }
059    
060        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
061            this.applicationContext = applicationContext;
062        }
063    
064        public ApplicationContext getApplicationContext() {
065            return applicationContext;
066        }
067    
068        public boolean isSingleton() {
069            return true;
070        }
071    
072        public Producer createProducer() throws Exception {
073            ObjectHelper.notNull(applicationContext, "applicationContext");
074            return new DefaultProducer(this) {
075                public void process(Exchange exchange) throws Exception {
076                    ApplicationEvent event = toApplicationEvent(exchange);
077                    applicationContext.publishEvent(event);
078                }
079            };
080        }
081    
082        public EventConsumer createConsumer(Processor processor) throws Exception {
083            ObjectHelper.notNull(applicationContext, "applicationContext");
084            return new EventConsumer(this, processor);
085        }
086    
087        public void onApplicationEvent(ApplicationEvent event) {
088            Exchange exchange = createExchange();
089            exchange.getIn().setBody(event);
090            try {
091                getLoadBalancer().process(exchange);
092            } catch (Exception e) {
093                throw wrapRuntimeCamelException(e);
094            }
095        }
096    
097        public LoadBalancer getLoadBalancer() {
098            if (loadBalancer == null) {
099                loadBalancer = createLoadBalancer();
100            }
101            return loadBalancer;
102        }
103    
104        public void setLoadBalancer(LoadBalancer loadBalancer) {
105            this.loadBalancer = loadBalancer;
106        }
107    
108        // Implementation methods
109        // -------------------------------------------------------------------------
110        public synchronized void consumerStarted(EventConsumer consumer) {
111            getLoadBalancer().addProcessor(consumer.getProcessor());
112        }
113    
114        public synchronized void consumerStopped(EventConsumer consumer) {
115            getLoadBalancer().removeProcessor(consumer.getProcessor());
116        }
117    
118        protected LoadBalancer createLoadBalancer() {
119            return new TopicLoadBalancer();
120        }
121    
122        protected ApplicationEvent toApplicationEvent(Exchange exchange) {
123            ApplicationEvent event = exchange.getIn().getBody(ApplicationEvent.class);
124            if (event != null) {
125                return event;
126            }
127            return new CamelEvent(this, exchange);
128        }
129    }