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