001    /**
002     *
003     * Licensed to the Apache Software Foundation (ASF) under one or more
004     * contributor license agreements.  See the NOTICE file distributed with
005     * this work for additional information regarding copyright ownership.
006     * The ASF licenses this file to You under the Apache License, Version 2.0
007     * (the "License"); you may not use this file except in compliance with
008     * the License.  You may obtain a copy of the License at
009     *
010     * http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.camel.component.processor;
019    
020    import org.apache.camel.Endpoint;
021    import org.apache.camel.Processor;
022    import org.apache.camel.Exchange;
023    import org.apache.camel.Consumer;
024    import org.apache.camel.Producer;
025    import org.apache.camel.Component;
026    import org.apache.camel.impl.DefaultEndpoint;
027    import org.apache.camel.impl.DefaultExchange;
028    import org.apache.camel.impl.DefaultProducer;
029    import org.apache.camel.processor.loadbalancer.LoadBalancer;
030    
031    /**
032     * A base class for creating {@link Endpoint} implementations from a {@link Processor}
033     *
034     * @version $Revision: 1.1 $
035     */
036    public class ProcessorEndpoint extends DefaultEndpoint<Exchange> {
037        private final Processor processor;
038        private final LoadBalancer loadBalancer;
039    
040        protected ProcessorEndpoint(String endpointUri, Component component, Processor processor, LoadBalancer loadBalancer) {
041            super(endpointUri, component);
042            this.processor = processor;
043            this.loadBalancer = loadBalancer;
044        }
045    
046        public Exchange createExchange() {
047            return new DefaultExchange(getContext());
048        }
049    
050        public Producer<Exchange> createProducer() throws Exception {
051            return new DefaultProducer<Exchange>(this) {
052                public void process(Exchange exchange) throws Exception {
053                    onExchange(exchange);
054                }
055            };
056        }
057    
058        public Consumer<Exchange> createConsumer(Processor processor) throws Exception {
059            return new ProcessorEndpointConsumer(this, processor);
060        }
061    
062        public Processor getProcessor() {
063            return processor;
064        }
065    
066        public LoadBalancer getLoadBalancer() {
067            return loadBalancer;
068        }
069    
070        protected void onExchange(Exchange exchange) throws Exception {
071            processor.process(exchange);
072    
073            // now lets output to the load balancer
074            loadBalancer.process(exchange);
075        }
076    
077            public boolean isSingleton() {
078                    return true;
079            }
080    }