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