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