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.AsyncProcessor;
020    import org.apache.camel.Consumer;
021    import org.apache.camel.Endpoint;
022    import org.apache.camel.Processor;
023    import org.apache.camel.impl.converter.AsyncProcessorTypeConverter;
024    import org.apache.camel.spi.ExceptionHandler;
025    import org.apache.camel.util.ServiceHelper;
026    import org.apache.commons.logging.Log;
027    import org.apache.commons.logging.LogFactory;
028    
029    /**
030     * A default consumer useful for implementation inheritance.
031     *
032     * @version $Revision: 21036 $
033     */
034    public class DefaultConsumer extends ServiceSupport implements Consumer {
035        protected final transient Log log = LogFactory.getLog(getClass());
036        private final Endpoint endpoint;
037        private final Processor processor;
038        private volatile AsyncProcessor asyncProcessor;
039        private ExceptionHandler exceptionHandler;
040    
041        public DefaultConsumer(Endpoint endpoint, Processor processor) {
042            this.endpoint = endpoint;
043            this.processor = processor;
044        }
045    
046        @Override
047        public String toString() {
048            return "Consumer[" + endpoint.getEndpointUri() + "]";
049        }
050    
051        public Endpoint getEndpoint() {
052            return endpoint;
053        }
054    
055        public Processor getProcessor() {
056            return processor;
057        }
058    
059        /**
060         * Provides an {@link org.apache.camel.AsyncProcessor} interface to the configured
061         * processor on the consumer. If the processor does not implement the interface,
062         * it will be adapted so that it does.
063         */
064        public synchronized AsyncProcessor getAsyncProcessor() {
065            if (asyncProcessor == null) {            
066                asyncProcessor = AsyncProcessorTypeConverter.convert(processor);
067            }
068            return asyncProcessor;
069        }
070    
071        public ExceptionHandler getExceptionHandler() {
072            if (exceptionHandler == null) {
073                exceptionHandler = new LoggingExceptionHandler(getClass());
074            }
075            return exceptionHandler;
076        }
077    
078        public void setExceptionHandler(ExceptionHandler exceptionHandler) {
079            this.exceptionHandler = exceptionHandler;
080        }
081    
082        protected void doStop() throws Exception {
083            if (log.isDebugEnabled()) {
084                log.debug("Stopping consumer: " + this);
085            }
086            ServiceHelper.stopServices(processor);
087        }
088    
089        protected void doStart() throws Exception {
090            if (log.isDebugEnabled()) {
091                log.debug("Starting consumer: " + this);
092            }
093            ServiceHelper.startServices(processor);
094        }
095    
096        /**
097         * Handles the given exception using the {@link #getExceptionHandler()}
098         * 
099         * @param t the exception to handle
100         */
101        protected void handleException(Throwable t) {
102            Throwable newt = (t == null) ? new IllegalArgumentException("Handling [null] exception") : t;
103            getExceptionHandler().handleException(newt);
104        }
105    }