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