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.Category;
020import org.apache.camel.Exchange;
021import org.apache.camel.Processor;
022import org.apache.camel.Producer;
023import org.apache.camel.processor.loadbalancer.LoadBalancer;
024import org.apache.camel.processor.loadbalancer.TopicLoadBalancer;
025import org.apache.camel.spi.UriEndpoint;
026import org.apache.camel.spi.UriPath;
027import org.apache.camel.support.DefaultEndpoint;
028import org.apache.camel.support.DefaultProducer;
029import org.apache.camel.util.ObjectHelper;
030import org.springframework.beans.BeansException;
031import org.springframework.context.ApplicationContext;
032import org.springframework.context.ApplicationContextAware;
033import org.springframework.context.ApplicationEvent;
034
035import static org.apache.camel.RuntimeCamelException.wrapRuntimeCamelException;
036
037/**
038 * Listen for Spring Application Events.
039 */
040@UriEndpoint(firstVersion = "1.4.0", scheme = "spring-event", title = "Spring Event", syntax = "spring-event:name", category = {Category.SPRING, Category.EVENTBUS})
041public class EventEndpoint extends DefaultEndpoint implements ApplicationContextAware {
042    private LoadBalancer loadBalancer;
043    private ApplicationContext applicationContext;
044
045    @UriPath(description = "Name of endpoint")
046    private String name;
047
048    public EventEndpoint(String endpointUri, EventComponent component, String name) {
049        super(endpointUri, component);
050        this.applicationContext = component.getApplicationContext();
051        this.name = name;
052    }
053
054    @Override
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 String getName() {
064        return name;
065    }
066
067    public void setName(String name) {
068        this.name = name;
069    }
070
071    @Override
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    @Override
083    public EventConsumer createConsumer(Processor processor) throws Exception {
084        ObjectHelper.notNull(applicationContext, "applicationContext");
085        EventConsumer answer = new EventConsumer(this, processor);
086        configureConsumer(answer);
087        return answer;
088    }
089
090    public void onApplicationEvent(ApplicationEvent event) {
091        Exchange exchange = createExchange();
092        exchange.getIn().setBody(event);
093        try {
094            getLoadBalancer().process(exchange);
095        } catch (Exception e) {
096            throw wrapRuntimeCamelException(e);
097        }
098    }
099
100    public LoadBalancer getLoadBalancer() {
101        if (loadBalancer == null) {
102            loadBalancer = createLoadBalancer();
103        }
104        return loadBalancer;
105    }
106
107    public void setLoadBalancer(LoadBalancer loadBalancer) {
108        this.loadBalancer = loadBalancer;
109    }
110
111    @Override
112    public EventComponent getComponent() {
113        return (EventComponent) super.getComponent();
114    }
115
116    // Implementation methods
117    // -------------------------------------------------------------------------
118    public synchronized void consumerStarted(EventConsumer consumer) {
119        getComponent().consumerStarted(this);
120        getLoadBalancer().addProcessor(consumer.getAsyncProcessor());
121    }
122
123    public synchronized void consumerStopped(EventConsumer consumer) {
124        getComponent().consumerStopped(this);
125        getLoadBalancer().removeProcessor(consumer.getAsyncProcessor());
126    }
127
128    protected LoadBalancer createLoadBalancer() {
129        return new TopicLoadBalancer();
130    }
131
132    protected ApplicationEvent toApplicationEvent(Exchange exchange) {
133        ApplicationEvent event = exchange.getIn().getBody(ApplicationEvent.class);
134        if (event != null) {
135            return event;
136        }
137        return new CamelEvent(this, exchange);
138    }
139}