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.processor;
018
019import org.apache.camel.AsyncProcessor;
020import org.apache.camel.Endpoint;
021import org.apache.camel.Exchange;
022import org.apache.camel.ExchangePattern;
023import org.apache.camel.Producer;
024import org.apache.camel.util.AsyncProcessorHelper;
025import org.apache.camel.util.ServiceHelper;
026
027/**
028 * Ensures a {@link Producer} is executed within an {@link org.apache.camel.spi.UnitOfWork}.
029 *
030 * @version 
031 */
032public final class UnitOfWorkProducer implements Producer {
033
034    private final Producer producer;
035    private final AsyncProcessor processor;
036
037    /**
038     * The producer which should be executed within an {@link org.apache.camel.spi.UnitOfWork}.
039     *
040     * @param producer the producer
041     */
042    public UnitOfWorkProducer(Producer producer) {
043        this.producer = producer;
044        // wrap in unit of work
045        CamelInternalProcessor internal = new CamelInternalProcessor(producer);
046        internal.addAdvice(new CamelInternalProcessor.UnitOfWorkProcessorAdvice(null));
047        this.processor = internal;
048    }
049
050    public Endpoint getEndpoint() {
051        return producer.getEndpoint();
052    }
053
054    public Exchange createExchange() {
055        return producer.createExchange();
056    }
057
058    public Exchange createExchange(ExchangePattern pattern) {
059        return producer.createExchange(pattern);
060    }
061
062    @Deprecated
063    public Exchange createExchange(Exchange exchange) {
064        return producer.createExchange(exchange);
065    }
066
067    public void process(final Exchange exchange) throws Exception {
068        AsyncProcessorHelper.process(processor, exchange);
069    }
070
071    public void start() throws Exception {
072        ServiceHelper.startService(processor);
073    }
074
075    public void stop() throws Exception {
076        ServiceHelper.stopService(processor);
077    }
078
079    public boolean isSingleton() {
080        return producer.isSingleton();
081    }
082
083    @Override
084    public String toString() {
085        return "UnitOfWork(" + producer + ")";
086    }
087}