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    @Override
048    public void notify(CamelEvent event) throws Exception {
049        // only notify when we are started
050        if (!isStarted()) {
051            log.debug("Cannot publish event as notifier is not started: {}", event);
052            return;
053        }
054
055        // only notify when camel context is running
056        if (!camelContext.getStatus().isStarted()) {
057            log.debug("Cannot publish event as CamelContext is not started: {}", event);
058            return;
059        }
060
061        Exchange exchange = producer.getEndpoint().createExchange();
062        exchange.getIn().setBody(event);
063
064        // make sure we don't send out events for this as well
065        // mark exchange as being published to event, to prevent creating new events
066        // for this as well (causing a endless flood of events)
067        exchange.setProperty(Exchange.NOTIFY_EVENT, Boolean.TRUE);
068        try {
069            producer.process(exchange);
070        } finally {
071            // and remove it when its done
072            exchange.removeProperty(Exchange.NOTIFY_EVENT);
073        }
074    }
075
076    @Override
077    public boolean isEnabled(CamelEvent event) {
078        return true;
079    }
080
081    @Override
082    public CamelContext getCamelContext() {
083        return camelContext;
084    }
085
086    @Override
087    public void setCamelContext(CamelContext camelContext) {
088        this.camelContext = camelContext;
089    }
090
091    public Endpoint getEndpoint() {
092        return endpoint;
093    }
094
095    public void setEndpoint(Endpoint endpoint) {
096        this.endpoint = endpoint;
097    }
098
099    public String getEndpointUri() {
100        return endpointUri;
101    }
102
103    public void setEndpointUri(String endpointUri) {
104        this.endpointUri = endpointUri;
105    }
106
107    @Override
108    protected void doStart() throws Exception {
109        ObjectHelper.notNull(camelContext, "camelContext", this);
110        if (endpoint == null && endpointUri == null) {
111            throw new IllegalArgumentException("Either endpoint or endpointUri must be configured");
112        }
113
114        if (endpoint == null) {
115            endpoint = camelContext.getEndpoint(endpointUri);
116        }
117
118        producer = endpoint.createProducer();
119        ServiceHelper.startService(producer);
120    }
121
122    @Override
123    protected void doStop() throws Exception {
124        ServiceHelper.stopService(producer);
125    }
126
127    @Override
128    public String toString() {
129        return "PublishEventNotifier[" + (endpoint != null ? endpoint : URISupport.sanitizeUri(endpointUri)) + "]";
130    }
131
132}