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.CamelContext;
020    import org.apache.camel.Component;
021    import org.apache.camel.Exchange;
022    import org.apache.camel.PollingConsumer;
023    import org.apache.camel.Processor;
024    import org.apache.camel.Producer;
025    
026    /**
027     * An endpoint which allows exchanges to be sent into it which just invokes a
028     * given {@link Processor}. This component does not support the use of
029     * consumers.
030     *
031     * @version $Revision: 41409 $
032     */
033    public class ProcessorEndpoint extends DefaultPollingEndpoint<Exchange> {
034        private Processor processor;
035    
036        protected ProcessorEndpoint() {
037        }
038    
039        protected ProcessorEndpoint(String endpointUri) {
040            super(endpointUri);
041        }
042    
043        public ProcessorEndpoint(String endpointUri, CamelContext context, Processor processor) {
044            super(endpointUri, context);
045            this.processor = processor;
046        }
047    
048        public ProcessorEndpoint(String endpointUri, Component component, Processor processor) {
049            super(endpointUri, component);
050            this.processor = processor;
051        }
052    
053        public ProcessorEndpoint(String endpointUri, Processor processor) {
054            super(endpointUri);
055            this.processor = processor;
056        }
057    
058    
059        protected ProcessorEndpoint(String endpointUri, Component component) {
060            super(endpointUri, component);
061        }
062    
063        public Producer<Exchange> createProducer() throws Exception {
064            return new DefaultProducer<Exchange>(this) {
065                public void process(Exchange exchange) throws Exception {
066                    onExchange(exchange);
067                }
068            };
069        }
070    
071        @Override
072        public PollingConsumer<Exchange> createPollingConsumer() throws Exception {
073            return new ProcessorPollingConsumer(this, getProcessor());
074        }
075    
076        public Processor getProcessor() throws Exception {
077            if (processor == null) {
078                processor = createProcessor();
079            }
080            return processor;
081        }
082    
083        protected Processor createProcessor() throws Exception {
084            return new Processor() {
085                public void process(Exchange exchange) throws Exception {
086                    onExchange(exchange);
087                }
088            };
089        }
090    
091        protected void onExchange(Exchange exchange) throws Exception {
092            getProcessor().process(exchange);
093        }
094    
095        public boolean isSingleton() {
096            return true;
097        }
098    }