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.DefaultExchange;
025 import org.apache.camel.impl.DefaultProducer;
026 import org.apache.camel.processor.loadbalancer.LoadBalancer;
027 import org.apache.camel.processor.loadbalancer.TopicLoadBalancer;
028
029 import org.springframework.context.ApplicationContext;
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: 35332 $
037 */
038 public class EventEndpoint extends DefaultEndpoint<Exchange> {
039 private final EventComponent component;
040 private LoadBalancer loadBalancer;
041
042 public EventEndpoint(String endpointUri, EventComponent component) {
043 super(endpointUri, component);
044 this.component = component;
045 }
046
047 @Override
048 public EventComponent getComponent() {
049 return component;
050 }
051
052 public ApplicationContext getApplicationContext() {
053 return getComponent().getApplicationContext();
054 }
055
056 public boolean isSingleton() {
057 return true;
058 }
059
060 public Producer<Exchange> createProducer() throws Exception {
061 return new DefaultProducer<Exchange>(this) {
062 public void process(Exchange exchange) throws Exception {
063 ApplicationEvent event = toApplicationEvent(exchange);
064 getApplicationContext().publishEvent(event);
065 }
066 };
067 }
068
069 public EventConsumer createConsumer(Processor processor) throws Exception {
070 return new EventConsumer(this, processor);
071 }
072
073 public void onApplicationEvent(ApplicationEvent event) {
074 Exchange exchange = createExchange();
075 exchange.getIn().setBody(event);
076 try {
077 getLoadBalancer().process(exchange);
078 } catch (Exception e) {
079 throw new RuntimeCamelException(e);
080 }
081 }
082
083 public LoadBalancer getLoadBalancer() {
084 if (loadBalancer == null) {
085 loadBalancer = createLoadBalancer();
086 }
087 return loadBalancer;
088 }
089
090 public void setLoadBalancer(LoadBalancer loadBalancer) {
091 this.loadBalancer = loadBalancer;
092 }
093
094 // Implementation methods
095 // -------------------------------------------------------------------------
096 public synchronized void consumerStarted(EventConsumer consumer) {
097 getLoadBalancer().addProcessor(consumer.getProcessor());
098 }
099
100 public synchronized void consumerStopped(EventConsumer consumer) {
101 getLoadBalancer().removeProcessor(consumer.getProcessor());
102 }
103
104 protected LoadBalancer createLoadBalancer() {
105 return new TopicLoadBalancer();
106 }
107
108 protected ApplicationEvent toApplicationEvent(Exchange exchange) {
109 ApplicationEvent event = exchange.getIn().getBody(ApplicationEvent.class);
110 if (event == null) {
111 event = new CamelEvent(this, exchange);
112 }
113 return event;
114 }
115 }