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 027 /** 028 * A default consumer useful for implementation inheritance. 029 * 030 * @version $Revision: 751221 $ 031 */ 032 public class DefaultConsumer extends ServiceSupport implements Consumer { 033 private final Endpoint endpoint; 034 private final Processor processor; 035 private AsyncProcessor asyncProcessor; 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 on " + endpoint; 046 } 047 048 public Endpoint getEndpoint() { 049 return endpoint; 050 } 051 052 public Processor getProcessor() { 053 return processor; 054 } 055 056 /** 057 * Provides an {@link AsyncProcessor} interface to the configured 058 * processor on the consumer. If the processor does not implement 059 * the interface, it will be adapted so that it does. 060 */ 061 public AsyncProcessor getAsyncProcessor() { 062 if (asyncProcessor == null) { 063 asyncProcessor = AsyncProcessorTypeConverter.convert(processor); 064 } 065 return asyncProcessor; 066 } 067 068 public ExceptionHandler getExceptionHandler() { 069 if (exceptionHandler == null) { 070 exceptionHandler = new LoggingExceptionHandler(getClass()); 071 } 072 return exceptionHandler; 073 } 074 075 public void setExceptionHandler(ExceptionHandler exceptionHandler) { 076 this.exceptionHandler = exceptionHandler; 077 } 078 079 protected void doStop() throws Exception { 080 ServiceHelper.stopServices(processor); 081 } 082 083 protected void doStart() throws Exception { 084 ServiceHelper.startServices(processor); 085 } 086 087 /** 088 * Handles the given exception using the {@link #getExceptionHandler()} 089 * 090 * @param t the exception to handle 091 */ 092 protected void handleException(Throwable t) { 093 Throwable newt = (t == null) ? new Throwable("Handling [null] exception") : t; 094 getExceptionHandler().handleException(newt); 095 } 096 }