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 java.util.LinkedHashSet;
020 import java.util.Map;
021 import java.util.Set;
022
023 import org.apache.camel.impl.DefaultComponent;
024 import org.slf4j.Logger;
025 import org.slf4j.LoggerFactory;
026 import org.springframework.beans.BeansException;
027 import org.springframework.context.ApplicationContext;
028 import org.springframework.context.ApplicationContextAware;
029 import org.springframework.context.ApplicationEvent;
030 import org.springframework.context.ConfigurableApplicationContext;
031
032 /**
033 * An <a href="http://camel.apache.org/event.html">Event Component</a>
034 * for working with Spring ApplicationEvents
035 *
036 * @version
037 */
038 public class EventComponent extends DefaultComponent implements ApplicationContextAware {
039 private static final Logger LOG = LoggerFactory.getLogger(EventComponent.class);
040 private ApplicationContext applicationContext;
041 private final Set<EventEndpoint> endpoints = new LinkedHashSet<EventEndpoint>();
042
043 public EventComponent() {
044 }
045
046 public EventComponent(ApplicationContext applicationContext) {
047 setApplicationContext(applicationContext);
048 }
049
050 public ApplicationContext getApplicationContext() {
051 return applicationContext;
052 }
053
054 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
055 this.applicationContext = applicationContext;
056 }
057
058 public ConfigurableApplicationContext getConfigurableApplicationContext() {
059 ApplicationContext applicationContext = getApplicationContext();
060 if (applicationContext instanceof ConfigurableApplicationContext) {
061 return (ConfigurableApplicationContext)applicationContext;
062 } else {
063 throw new IllegalArgumentException("Class: " + applicationContext.getClass().getName() + " is not an instanceof ConfigurableApplicationContext.");
064 }
065 }
066
067 protected EventEndpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
068 EventEndpoint answer = new EventEndpoint(uri, this);
069 setProperties(answer, parameters);
070 return answer;
071 }
072
073 protected void consumerStarted(EventEndpoint endpoint) {
074 endpoints.add(endpoint);
075 }
076
077 protected void consumerStopped(EventEndpoint endpoint) {
078 endpoints.remove(endpoint);
079 }
080
081 public void onApplicationEvent(ApplicationEvent event) {
082 // broadcast to the endpoints in use
083 for (EventEndpoint endpoint : endpoints) {
084 try {
085 endpoint.onApplicationEvent(event);
086 } catch (Exception e) {
087 LOG.warn("Error on application event " + event + ". This exception will be ignored.", e);
088 }
089 }
090 }
091
092 @Override
093 protected void doStop() throws Exception {
094 endpoints.clear();
095 super.doStop();
096 }
097 }