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.processor;
018
019 import java.util.ArrayList;
020 import java.util.List;
021
022 import org.apache.camel.AsyncCallback;
023 import org.apache.camel.AsyncProcessor;
024 import org.apache.camel.DelegateProcessor;
025 import org.apache.camel.Exchange;
026 import org.apache.camel.Navigate;
027 import org.apache.camel.Processor;
028 import org.apache.camel.support.ServiceSupport;
029 import org.apache.camel.util.AsyncProcessorConverterHelper;
030 import org.apache.camel.util.AsyncProcessorHelper;
031 import org.apache.camel.util.ServiceHelper;
032
033 /**
034 * A Delegate pattern which delegates processing to a nested {@link AsyncProcessor} which can
035 * be useful for implementation inheritance when writing an {@link org.apache.camel.spi.Policy}
036 * <p/>
037 * <b>Important:</b> This implementation <b>does</b> support the asynchronous routing engine.
038 * If you are implementing a EIP pattern please use this as the delegate.
039 *
040 * @version
041 * @see org.apache.camel.processor.DelegateProcessor
042 */
043 public class DelegateAsyncProcessor extends ServiceSupport implements DelegateProcessor, AsyncProcessor, Navigate<Processor> {
044 protected AsyncProcessor processor;
045
046 public DelegateAsyncProcessor() {
047 }
048
049 public DelegateAsyncProcessor(AsyncProcessor processor) {
050 if (processor == this) {
051 throw new IllegalArgumentException("Recursive DelegateAsyncProcessor!");
052 }
053 this.processor = processor;
054 }
055
056 public DelegateAsyncProcessor(Processor processor) {
057 this(AsyncProcessorConverterHelper.convert(processor));
058 }
059
060 @Override
061 public String toString() {
062 return "DelegateAsync[" + processor + "]";
063 }
064
065 public AsyncProcessor getProcessor() {
066 return processor;
067 }
068
069 public void setProcessor(AsyncProcessor processor) {
070 this.processor = processor;
071 }
072
073 public void setProcessor(Processor processor) {
074 this.processor = AsyncProcessorConverterHelper.convert(processor);
075 }
076
077 protected void doStart() throws Exception {
078 ServiceHelper.startServices(processor);
079 }
080
081 protected void doStop() throws Exception {
082 ServiceHelper.stopServices(processor);
083 }
084
085 public void process(Exchange exchange) throws Exception {
086 AsyncProcessorHelper.process(this, exchange);
087 }
088
089 public boolean process(final Exchange exchange, final AsyncCallback callback) {
090 return processNext(exchange, callback);
091 }
092
093 protected boolean processNext(Exchange exchange, AsyncCallback callback) {
094 if (processor == null) {
095 // no processor then we are done
096 callback.done(true);
097 return true;
098 }
099 return AsyncProcessorHelper.process(processor, exchange, callback);
100 }
101
102 public boolean hasNext() {
103 return processor != null;
104 }
105
106 public List<Processor> next() {
107 if (!hasNext()) {
108 return null;
109 }
110 List<Processor> answer = new ArrayList<Processor>(1);
111 answer.add(processor);
112 return answer;
113 }
114
115 }