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 */
017package org.apache.camel.impl;
018
019import org.apache.camel.CamelContext;
020import org.apache.camel.Component;
021import org.apache.camel.Exchange;
022import org.apache.camel.PollingConsumer;
023import org.apache.camel.Processor;
024import 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 * <p/>
031 * <br/>Implementors beware that this endpoint creates producers and consumers which
032 * doesn't allow you full control of its lifecycle as a {@link org.apache.camel.Service}
033 * or {@link org.apache.camel.SuspendableService} would do.
034 * If your producers/consumers need more control of its lifecycle it is advised instead to extend
035 * {@link org.apache.camel.impl.DefaultEndpoint}, {@link org.apache.camel.impl.DefaultProducer}
036 * and {@link org.apache.camel.impl.DefaultConsumer}.
037 *
038 * @version 
039 */
040public class ProcessorEndpoint extends DefaultPollingEndpoint {
041    private Processor processor;
042
043    protected ProcessorEndpoint() {
044    }
045
046    @SuppressWarnings("deprecation")
047    public ProcessorEndpoint(String endpointUri, CamelContext context, Processor processor) {
048        super(endpointUri);
049        this.setCamelContext(context);
050        this.processor = processor;
051    }
052
053    public ProcessorEndpoint(String endpointUri, Component component, Processor processor) {
054        super(endpointUri, component);
055        this.processor = processor;
056    }
057
058    protected ProcessorEndpoint(String endpointUri, Component component) {
059        super(endpointUri, component);
060    }
061
062    @Deprecated
063    public ProcessorEndpoint(String endpointUri, Processor processor) {
064        super(endpointUri);
065        this.processor = processor;
066    }
067
068    public Producer createProducer() throws Exception {
069        return new DefaultProducer(this) {
070            public void process(Exchange exchange) throws Exception {
071                onExchange(exchange);
072            }
073        };
074    }
075
076    @Override
077    public PollingConsumer createPollingConsumer() throws Exception {
078        PollingConsumer answer = new ProcessorPollingConsumer(this, getProcessor());
079        configurePollingConsumer(answer);
080        return answer;
081    }
082
083    public Processor getProcessor() throws Exception {
084        if (processor == null) {
085            processor = createProcessor();
086        }
087        return processor;
088    }
089
090    public void setProcessor(Processor processor) {
091        this.processor = processor;
092    }
093
094    protected Processor createProcessor() throws Exception {
095        return new Processor() {
096            public void process(Exchange exchange) throws Exception {
097                onExchange(exchange);
098            }
099        };
100    }
101
102    protected void onExchange(Exchange exchange) throws Exception {
103        getProcessor().process(exchange);
104    }
105
106    public boolean isSingleton() {
107        return true;
108    }
109}