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.management;
018
019import java.util.EventObject;
020
021import org.apache.camel.CamelContext;
022import org.apache.camel.CamelContextAware;
023import org.apache.camel.Endpoint;
024import org.apache.camel.Exchange;
025import org.apache.camel.Producer;
026import org.apache.camel.spi.CamelEvent;
027import org.apache.camel.support.EventNotifierSupport;
028import org.apache.camel.support.service.ServiceHelper;
029import org.apache.camel.util.ObjectHelper;
030import org.apache.camel.util.URISupport;
031
032/**
033 * A {@link org.apache.camel.spi.EventNotifier} which publishes the {@link EventObject} to some
034 * {@link org.apache.camel.Endpoint}.
035 * <p/>
036 * This notifier is only enabled when {@link CamelContext} is started. This avoids problems when
037 * sending notifications during start/shutdown of {@link CamelContext} which causes problems by
038 * sending those events to Camel routes by this notifier.
039 */
040public class PublishEventNotifier extends EventNotifierSupport implements CamelContextAware {
041
042    private CamelContext camelContext;
043    private Endpoint endpoint;
044    private String endpointUri;
045    private Producer producer;
046
047    public void notify(CamelEvent event) throws Exception {
048        // only notify when we are started
049        if (!isStarted()) {
050            log.debug("Cannot publish event as notifier is not started: {}", event);
051            return;
052        }
053
054        // only notify when camel context is running
055        if (!camelContext.getStatus().isStarted()) {
056            log.debug("Cannot publish event as CamelContext is not started: {}", event);
057            return;
058        }
059
060        Exchange exchange = producer.getEndpoint().createExchange();
061        exchange.getIn().setBody(event);
062
063        // make sure we don't send out events for this as well
064        // mark exchange as being published to event, to prevent creating new events
065        // for this as well (causing a endless flood of events)
066        exchange.setProperty(Exchange.NOTIFY_EVENT, Boolean.TRUE);
067        try {
068            producer.process(exchange);
069        } finally {
070            // and remove it when its done
071            exchange.removeProperty(Exchange.NOTIFY_EVENT);
072        }
073    }
074
075    public boolean isEnabled(CamelEvent event) {
076        return true;
077    }
078
079    public CamelContext getCamelContext() {
080        return camelContext;
081    }
082
083    public void setCamelContext(CamelContext camelContext) {
084        this.camelContext = camelContext;
085    }
086
087    public Endpoint getEndpoint() {
088        return endpoint;
089    }
090
091    public void setEndpoint(Endpoint endpoint) {
092        this.endpoint = endpoint;
093    }
094
095    public String getEndpointUri() {
096        return endpointUri;
097    }
098
099    public void setEndpointUri(String endpointUri) {
100        this.endpointUri = endpointUri;
101    }
102
103    @Override
104    protected void doStart() throws Exception {
105        ObjectHelper.notNull(camelContext, "camelContext", this);
106        if (endpoint == null && endpointUri == null) {
107            throw new IllegalArgumentException("Either endpoint or endpointUri must be configured");
108        }
109
110        if (endpoint == null) {
111            endpoint = camelContext.getEndpoint(endpointUri);
112        }
113
114        producer = endpoint.createProducer();
115        ServiceHelper.startService(producer);
116    }
117
118    @Override
119    protected void doStop() throws Exception {
120        ServiceHelper.stopService(producer);
121    }
122
123    @Override
124    public String toString() {
125        return "PublishEventNotifier[" + (endpoint != null ? endpoint : URISupport.sanitizeUri(endpointUri)) + "]";
126    }
127
128}