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