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 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    @Deprecated
059    public Exchange createExchange(Exchange exchange) {
060        return endpoint.createExchange(exchange);
061    }
062
063    /**
064     * This implementation will delegate to the endpoint {@link org.apache.camel.Endpoint#isSingleton()}
065     */
066    public boolean isSingleton() {
067        return endpoint.isSingleton();
068    }
069
070    protected void doStart() throws Exception {
071        // log at debug level for singletons, for prototype scoped log at trace level to not spam logs
072        if (isSingleton()) {
073            log.debug("Starting producer: {}", this);
074        } else {
075            log.trace("Starting producer: {}", this);
076        }
077    }
078
079    protected void doStop() throws Exception {
080        // log at debug level for singletons, for prototype scoped log at trace level to not spam logs
081        if (isSingleton()) {
082            log.debug("Stopping producer: {}", this);
083        } else {
084            log.trace("Stopping producer: {}", this);
085        }
086    }
087}