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