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