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 DelegateSyncProcessor
042     * @see org.apache.camel.processor.DelegateProcessor
043     */
044    public class DelegateAsyncProcessor extends ServiceSupport implements DelegateProcessor, AsyncProcessor, Navigate<Processor> {
045        protected AsyncProcessor processor;
046    
047        public DelegateAsyncProcessor() {
048        }
049    
050        public DelegateAsyncProcessor(AsyncProcessor processor) {
051            if (processor == this) {
052                throw new IllegalArgumentException("Recursive DelegateAsyncProcessor!");
053            }
054            this.processor = processor;
055        }
056    
057        public DelegateAsyncProcessor(Processor processor) {
058            this(AsyncProcessorConverterHelper.convert(processor));
059        }
060    
061        @Override
062        public String toString() {
063            return "DelegateAsync[" + processor + "]";
064        }
065    
066        public AsyncProcessor getProcessor() {
067            return processor;
068        }
069    
070        public void setProcessor(AsyncProcessor processor) {
071            this.processor = processor;
072        }
073    
074        public void setProcessor(Processor processor) {
075            this.processor = AsyncProcessorConverterHelper.convert(processor);
076        }
077    
078        protected void doStart() throws Exception {
079            ServiceHelper.startServices(processor);
080        }
081    
082        protected void doStop() throws Exception {
083            ServiceHelper.stopServices(processor);
084        }
085    
086        public void process(Exchange exchange) throws Exception {
087            AsyncProcessorHelper.process(this, exchange);
088        }
089    
090        public boolean process(final Exchange exchange, final AsyncCallback callback) {
091            return processor.process(exchange, callback);
092        }
093    
094        /**
095         * @deprecated use {@link #process(org.apache.camel.Exchange, org.apache.camel.AsyncCallback)} instead
096         */
097        @Deprecated
098        protected boolean processNext(Exchange exchange, AsyncCallback callback) {
099            throw new UnsupportedOperationException("This method is deprecated, use process(Exchange, AsyncCallback) instead");
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    }