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 $Revision: 781349 $
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 public EventEndpoint(String endpointUri) {
051 super(endpointUri);
052 }
053
054 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
055 this.applicationContext = applicationContext;
056 }
057
058 public ApplicationContext getApplicationContext() {
059 return applicationContext;
060 }
061
062 public boolean isSingleton() {
063 return true;
064 }
065
066 public Producer createProducer() throws Exception {
067 ObjectHelper.notNull(applicationContext, "applicationContext");
068 return new DefaultProducer(this) {
069 public void process(Exchange exchange) throws Exception {
070 ApplicationEvent event = toApplicationEvent(exchange);
071 applicationContext.publishEvent(event);
072 }
073 };
074 }
075
076 public EventConsumer createConsumer(Processor processor) throws Exception {
077 ObjectHelper.notNull(applicationContext, "applicationContext");
078 return new EventConsumer(this, processor);
079 }
080
081 public void onApplicationEvent(ApplicationEvent event) {
082 Exchange exchange = createExchange();
083 exchange.getIn().setBody(event);
084 try {
085 getLoadBalancer().process(exchange);
086 } catch (Exception e) {
087 throw wrapRuntimeCamelException(e);
088 }
089 }
090
091 public LoadBalancer getLoadBalancer() {
092 if (loadBalancer == null) {
093 loadBalancer = createLoadBalancer();
094 }
095 return loadBalancer;
096 }
097
098 public void setLoadBalancer(LoadBalancer loadBalancer) {
099 this.loadBalancer = loadBalancer;
100 }
101
102 // Implementation methods
103 // -------------------------------------------------------------------------
104 public synchronized void consumerStarted(EventConsumer consumer) {
105 getLoadBalancer().addProcessor(consumer.getProcessor());
106 }
107
108 public synchronized void consumerStopped(EventConsumer consumer) {
109 getLoadBalancer().removeProcessor(consumer.getProcessor());
110 }
111
112 protected LoadBalancer createLoadBalancer() {
113 return new TopicLoadBalancer();
114 }
115
116 protected ApplicationEvent toApplicationEvent(Exchange exchange) {
117 ApplicationEvent event = exchange.getIn().getBody(ApplicationEvent.class);
118 if (event != null) {
119 return event;
120 }
121 return new CamelEvent(this, exchange);
122 }
123 }