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.management;
018
019 import java.util.EventObject;
020
021 import org.apache.camel.CamelContext;
022 import org.apache.camel.CamelContextAware;
023 import org.apache.camel.Endpoint;
024 import org.apache.camel.Exchange;
025 import org.apache.camel.Producer;
026 import org.apache.camel.util.ObjectHelper;
027 import org.apache.camel.util.ServiceHelper;
028
029 /**
030 * A {@link org.apache.camel.spi.EventNotifier} which publishes the {@link EventObject} to some
031 * {@link org.apache.camel.Endpoint}.
032 *
033 * @version $Revision: 897919 $
034 */
035 public class PublishEventNotifier extends EventNotifierSupport implements CamelContextAware {
036
037 private CamelContext camelContext;
038 private Endpoint endpoint;
039 private String endpointUri;
040 private Producer producer;
041
042 public void notify(EventObject event) throws Exception {
043 Exchange exchange = producer.createExchange();
044 exchange.getIn().setBody(event);
045
046 producer.process(exchange);
047 }
048
049 public boolean isEnabled(EventObject event) {
050 return true;
051 }
052
053 public CamelContext getCamelContext() {
054 return camelContext;
055 }
056
057 public void setCamelContext(CamelContext camelContext) {
058 this.camelContext = camelContext;
059 }
060
061 public Endpoint getEndpoint() {
062 return endpoint;
063 }
064
065 public void setEndpoint(Endpoint endpoint) {
066 this.endpoint = endpoint;
067 }
068
069 public String getEndpointUri() {
070 return endpointUri;
071 }
072
073 public void setEndpointUri(String endpointUri) {
074 this.endpointUri = endpointUri;
075 }
076
077 @Override
078 protected void doStart() throws Exception {
079 ObjectHelper.notNull(camelContext, "camelContext", this);
080 if (endpoint == null && endpointUri == null) {
081 throw new IllegalArgumentException("Either endpoint or endpointUri must be configured");
082 }
083
084 if (endpoint == null) {
085 endpoint = camelContext.getEndpoint(endpointUri);
086 }
087
088 producer = endpoint.createProducer();
089 ServiceHelper.startService(producer);
090 }
091
092 @Override
093 protected void doStop() throws Exception {
094 ServiceHelper.stopService(producer);
095 }
096
097 @Override
098 public String toString() {
099 return "PublishEventNotifier[" + (endpoint != null ? endpoint.getEndpointUri() : endpointUri) + "]";
100 }
101
102 }