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.Exchange;
022    import org.apache.camel.Processor;
023    import org.apache.camel.spi.ExceptionHandler;
024    import org.apache.camel.util.ServiceHelper;
025    
026    /**
027     * @version $Revision: 563607 $
028     */
029    public class DefaultConsumer<E extends Exchange> extends ServiceSupport implements Consumer<E> {
030        private Endpoint<E> endpoint;
031        private Processor processor;
032        private ExceptionHandler exceptionHandler;
033    
034        public DefaultConsumer(Endpoint<E> endpoint, Processor processor) {
035            this.endpoint = endpoint;
036            this.processor = processor;
037        }
038    
039        @Override
040        public String toString() {
041            return "Consumer on " + endpoint;
042        }
043    
044        public Endpoint<E> getEndpoint() {
045            return endpoint;
046        }
047    
048        public Processor getProcessor() {
049            return processor;
050        }
051    
052        public ExceptionHandler getExceptionHandler() {
053            if (exceptionHandler == null) {
054                exceptionHandler = new LoggingExceptionHandler(getClass());
055            }
056            return exceptionHandler;
057        }
058    
059        public void setExceptionHandler(ExceptionHandler exceptionHandler) {
060            this.exceptionHandler = exceptionHandler;
061        }
062    
063        protected void doStop() throws Exception {
064            ServiceHelper.stopServices(processor);
065        }
066    
067        protected void doStart() throws Exception {
068            ServiceHelper.startServices(processor);
069        }
070    
071        /**
072         * Handles the given exception using the {@link #getExceptionHandler()}
073         * 
074         * @param t the exception to handle
075         */
076        protected void handleException(Throwable t) {
077            getExceptionHandler().handleException(t);
078        }
079    }