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 java.util.List;
020    
021    import org.apache.camel.Consumer;
022    import org.apache.camel.Endpoint;
023    import org.apache.camel.Navigate;
024    import org.apache.camel.Processor;
025    import org.apache.camel.Service;
026    import org.apache.camel.management.InstrumentationProcessor;
027    import org.apache.camel.spi.RouteContext;
028    
029    /**
030     * A {@link DefaultRoute} which starts with an
031     * <a href="http://camel.apache.org/event-driven-consumer.html">Event Driven Consumer</a>
032     *
033     * @version $Revision: 830861 $
034     */
035    public class EventDrivenConsumerRoute extends DefaultRoute {
036        private final Processor processor;
037        private Consumer consumer;
038    
039        public EventDrivenConsumerRoute(RouteContext routeContext, Endpoint endpoint, Processor processor) {
040            super(routeContext, endpoint);
041            this.processor = processor;
042        }
043    
044        @Override
045        public String toString() {
046            return "EventDrivenConsumerRoute[" + getEndpoint() + " -> " + processor + "]";
047        }
048    
049        public Processor getProcessor() {
050            return processor;
051        }
052    
053        /**
054         * Factory method to lazily create the complete list of services required for this route
055         * such as adding the processor or consumer
056         */
057        @Override
058        protected void addServices(List<Service> services) throws Exception {
059            Endpoint endpoint = getEndpoint();
060            consumer = endpoint.createConsumer(processor);
061            if (consumer != null) {
062                services.add(consumer);
063            }
064            Processor processor = getProcessor();
065            if (processor instanceof Service) {
066                services.add((Service)processor);
067            }
068        }
069    
070        @SuppressWarnings("unchecked")
071        public Navigate<Processor> navigate() {
072            Processor answer = getProcessor();
073            // skip the instrumentation processor if this route was wrapped by one
074            if (answer instanceof InstrumentationProcessor) {
075                answer = ((InstrumentationProcessor) answer).getProcessor();
076            }
077    
078            if (answer instanceof Navigate) {
079                return (Navigate) answer;
080            }
081            return null;
082        }
083    
084        public Consumer getConsumer() {
085            return consumer;
086        }
087    }