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