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 */
017package org.apache.camel.component.event;
018
019import org.apache.camel.Exchange;
020import org.apache.camel.Processor;
021import org.apache.camel.Producer;
022import org.apache.camel.impl.DefaultEndpoint;
023import org.apache.camel.impl.DefaultProducer;
024import org.apache.camel.processor.loadbalancer.LoadBalancer;
025import org.apache.camel.processor.loadbalancer.TopicLoadBalancer;
026import org.apache.camel.util.ObjectHelper;
027import org.springframework.beans.BeansException;
028import org.springframework.context.ApplicationContext;
029import org.springframework.context.ApplicationContextAware;
030import org.springframework.context.ApplicationEvent;
031
032import 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 */
041public 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        EventConsumer answer = new EventConsumer(this, processor);
085        configureConsumer(answer);
086        return answer;
087    }
088
089    public void onApplicationEvent(ApplicationEvent event) {
090        Exchange exchange = createExchange();
091        exchange.getIn().setBody(event);
092        try {
093            getLoadBalancer().process(exchange);
094        } catch (Exception e) {
095            throw wrapRuntimeCamelException(e);
096        }
097    }
098
099    public LoadBalancer getLoadBalancer() {
100        if (loadBalancer == null) {
101            loadBalancer = createLoadBalancer();
102        }
103        return loadBalancer;
104    }
105
106    public void setLoadBalancer(LoadBalancer loadBalancer) {
107        this.loadBalancer = loadBalancer;
108    }
109
110    @Override
111    public EventComponent getComponent() {
112        return (EventComponent) super.getComponent();
113    }
114
115    // Implementation methods
116    // -------------------------------------------------------------------------
117    public synchronized void consumerStarted(EventConsumer consumer) {
118        getComponent().consumerStarted(this);
119        getLoadBalancer().addProcessor(consumer.getProcessor());
120    }
121
122    public synchronized void consumerStopped(EventConsumer consumer) {
123        getComponent().consumerStopped(this);
124        getLoadBalancer().removeProcessor(consumer.getProcessor());
125    }
126
127    protected LoadBalancer createLoadBalancer() {
128        return new TopicLoadBalancer();
129    }
130
131    protected ApplicationEvent toApplicationEvent(Exchange exchange) {
132        ApplicationEvent event = exchange.getIn().getBody(ApplicationEvent.class);
133        if (event != null) {
134            return event;
135        }
136        return new CamelEvent(this, exchange);
137    }
138}