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