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.impl;
018
019 import org.apache.camel.Endpoint;
020 import org.apache.camel.Exchange;
021 import org.apache.camel.ExchangePattern;
022 import org.apache.camel.Producer;
023 import org.apache.camel.support.ServiceSupport;
024 import org.apache.camel.util.URISupport;
025 import org.slf4j.Logger;
026 import org.slf4j.LoggerFactory;
027
028 /**
029 * A default implementation of {@link Producer} for implementation inheritance.
030 *
031 * @version
032 */
033 public abstract class DefaultProducer extends ServiceSupport implements Producer {
034 protected final transient Logger log = LoggerFactory.getLogger(getClass());
035 private final Endpoint endpoint;
036
037 public DefaultProducer(Endpoint endpoint) {
038 this.endpoint = endpoint;
039 }
040
041 @Override
042 public String toString() {
043 return "Producer[" + URISupport.sanitizeUri(endpoint.getEndpointUri()) + "]";
044 }
045
046 public Endpoint getEndpoint() {
047 return endpoint;
048 }
049
050 public Exchange createExchange() {
051 return endpoint.createExchange();
052 }
053
054 public Exchange createExchange(ExchangePattern pattern) {
055 return endpoint.createExchange(pattern);
056 }
057
058 public Exchange createExchange(Exchange exchange) {
059 return endpoint.createExchange(exchange);
060 }
061
062 public boolean isSingleton() {
063 return true;
064 }
065
066 protected void doStart() throws Exception {
067 // log at debug level for singletons, for prototype scoped log at trace level to not spam logs
068 if (isSingleton()) {
069 log.debug("Starting producer: {}", this);
070 } else {
071 log.trace("Starting producer: {}", this);
072 }
073 }
074
075 protected void doStop() throws Exception {
076 // log at debug level for singletons, for prototype scoped log at trace level to not spam logs
077 if (isSingleton()) {
078 log.debug("Stopping producer: {}", this);
079 } else {
080 log.trace("Stopping producer: {}", this);
081 }
082 }
083 }