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.spi.UriEndpoint;
027import org.apache.camel.spi.UriPath;
028import org.apache.camel.util.ObjectHelper;
029import org.springframework.beans.BeansException;
030import org.springframework.context.ApplicationContext;
031import org.springframework.context.ApplicationContextAware;
032import org.springframework.context.ApplicationEvent;
033
034import static org.apache.camel.util.ObjectHelper.wrapRuntimeCamelException;
035
036/**
037 * The spring-event component allows to listen for Spring Application Events.
038 */
039@UriEndpoint(scheme = "spring-event", title = "Spring Event", syntax = "spring-event:name", consumerClass = EventConsumer.class, label = "spring,eventbus")
040public class EventEndpoint extends DefaultEndpoint implements ApplicationContextAware {
041    private LoadBalancer loadBalancer;
042    private ApplicationContext applicationContext;
043
044    @UriPath(description = "Name of endpoint")
045    private String name;
046
047    public EventEndpoint(String endpointUri, EventComponent component, String name) {
048        super(endpointUri, component);
049        this.applicationContext = component.getApplicationContext();
050        this.name = name;
051    }
052
053    /**
054     * <b>Note:</b> It is preferred to create endpoints using the associated
055     * component.
056     * @deprecated
057     */
058    @Deprecated
059    public EventEndpoint(String endpointUri) {
060        super(endpointUri);
061    }
062
063    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
064        this.applicationContext = applicationContext;
065    }
066
067    public ApplicationContext getApplicationContext() {
068        return applicationContext;
069    }
070
071    public String getName() {
072        return name;
073    }
074
075    public void setName(String name) {
076        this.name = name;
077    }
078
079    public boolean isSingleton() {
080        return true;
081    }
082
083    public Producer createProducer() throws Exception {
084        ObjectHelper.notNull(applicationContext, "applicationContext");
085        return new DefaultProducer(this) {
086            public void process(Exchange exchange) throws Exception {
087                ApplicationEvent event = toApplicationEvent(exchange);
088                applicationContext.publishEvent(event);
089            }
090        };
091    }
092
093    public EventConsumer createConsumer(Processor processor) throws Exception {
094        ObjectHelper.notNull(applicationContext, "applicationContext");
095        EventConsumer answer = new EventConsumer(this, processor);
096        configureConsumer(answer);
097        return answer;
098    }
099
100    public void onApplicationEvent(ApplicationEvent event) {
101        Exchange exchange = createExchange();
102        exchange.getIn().setBody(event);
103        try {
104            getLoadBalancer().process(exchange);
105        } catch (Exception e) {
106            throw wrapRuntimeCamelException(e);
107        }
108    }
109
110    public LoadBalancer getLoadBalancer() {
111        if (loadBalancer == null) {
112            loadBalancer = createLoadBalancer();
113        }
114        return loadBalancer;
115    }
116
117    public void setLoadBalancer(LoadBalancer loadBalancer) {
118        this.loadBalancer = loadBalancer;
119    }
120
121    @Override
122    public EventComponent getComponent() {
123        return (EventComponent) super.getComponent();
124    }
125
126    // Implementation methods
127    // -------------------------------------------------------------------------
128    public synchronized void consumerStarted(EventConsumer consumer) {
129        getComponent().consumerStarted(this);
130        getLoadBalancer().addProcessor(consumer.getProcessor());
131    }
132
133    public synchronized void consumerStopped(EventConsumer consumer) {
134        getComponent().consumerStopped(this);
135        getLoadBalancer().removeProcessor(consumer.getProcessor());
136    }
137
138    protected LoadBalancer createLoadBalancer() {
139        return new TopicLoadBalancer();
140    }
141
142    protected ApplicationEvent toApplicationEvent(Exchange exchange) {
143        ApplicationEvent event = exchange.getIn().getBody(ApplicationEvent.class);
144        if (event != null) {
145            return event;
146        }
147        return new CamelEvent(this, exchange);
148    }
149}