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