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.util;
018    
019    import java.util.HashMap;
020    import java.util.Map;
021    
022    import org.apache.camel.AsyncCallback;
023    import org.apache.camel.Endpoint;
024    import org.apache.camel.Exchange;
025    import org.apache.camel.ExchangePattern;
026    import org.apache.camel.FailedToCreateProducerException;
027    import org.apache.camel.Processor;
028    import org.apache.camel.Producer;
029    import org.apache.camel.RuntimeCamelException;
030    import org.apache.camel.impl.ServiceSupport;
031    import org.apache.camel.impl.converter.AsyncProcessorTypeConverter;
032    import org.apache.commons.logging.Log;
033    import org.apache.commons.logging.LogFactory;
034    
035    /**
036     * @version $Revision: 36635 $
037     */
038    public class ProducerCache<E extends Exchange> extends ServiceSupport {
039        private static final Log LOG = LogFactory.getLog(ProducerCache.class);
040    
041        private Map<String, Producer<E>> producers = new HashMap<String, Producer<E>>();
042    
043        public synchronized Producer<E> getProducer(Endpoint<E> endpoint) {
044            String key = endpoint.getEndpointUri();
045            Producer<E> answer = producers.get(key);
046            if (answer == null) {
047                try {
048                    answer = endpoint.createProducer();
049                    answer.start();
050                } catch (Exception e) {
051                    throw new FailedToCreateProducerException(endpoint, e);
052                }
053                producers.put(key, answer);
054            }
055            return answer;
056        }
057    
058        /**
059         * Sends the exchange to the given endpoint
060         *
061         * @param endpoint the endpoint to send the exchange to
062         * @param exchange the exchange to send
063         */
064        public void send(Endpoint<E> endpoint, E exchange) {
065            try {
066                Producer<E> producer = getProducer(endpoint);
067                producer.process(exchange);
068            } catch (Exception e) {
069                throw new RuntimeCamelException(e);
070            }
071        }
072    
073        /**
074         * Sends an exchange to an endpoint using a supplied
075         * {@link Processor} to populate the exchange
076         *
077         * @param endpoint the endpoint to send the exchange to
078         * @param processor the transformer used to populate the new exchange
079         */
080        public E send(Endpoint<E> endpoint, Processor processor) {
081            try {
082                Producer<E> producer = getProducer(endpoint);
083                E exchange = producer.createExchange();
084                return sendExchange(endpoint, producer, processor, exchange);
085            } catch (Exception e) {
086                throw new RuntimeCamelException(e);
087            }
088        }
089    
090        /**
091         * Sends an exchange to an endpoint using a supplied
092         * {@link Processor} to populate the exchange.  The callback
093         * will be called when the exchange is completed.
094         *
095         * @param endpoint the endpoint to send the exchange to
096         * @param processor the transformer used to populate the new exchange
097         */
098        public E send(Endpoint<E> endpoint, Processor processor, AsyncCallback callback) {
099            try {
100                Producer<E> producer = getProducer(endpoint);
101                E exchange = producer.createExchange();
102                boolean sync = sendExchange(endpoint, producer, processor, exchange, callback);
103                setProcessedSync(exchange, sync);
104                return exchange;
105            } catch (Exception e) {
106                throw new RuntimeCamelException(e);
107            }
108        }
109    
110        public static boolean isProcessedSync(Exchange exchange) {
111            Boolean rc = exchange.getProperty(ProducerCache.class.getName() + ".SYNC", Boolean.class);
112            return rc == null ? false : rc;
113        }
114    
115        public static void setProcessedSync(Exchange exchange, boolean b) {
116            exchange.setProperty(ProducerCache.class.getName() + ".SYNC", b ? Boolean.TRUE : Boolean.FALSE);
117        }
118    
119        /**
120         * Sends an exchange to an endpoint using a supplied
121         * {@link Processor} to populate the exchange
122         *
123         * @param endpoint the endpoint to send the exchange to
124         * @param pattern the message {@link ExchangePattern} such as
125         *   {@link ExchangePattern#InOnly} or {@link ExchangePattern#InOut}
126         * @param processor the transformer used to populate the new exchange
127         */
128        public E send(Endpoint<E> endpoint, ExchangePattern pattern, Processor processor) {
129            try {
130                Producer<E> producer = getProducer(endpoint);
131                E exchange = producer.createExchange(pattern);
132                return sendExchange(endpoint, producer, processor, exchange);
133            } catch (Exception e) {
134                throw new RuntimeCamelException(e);
135            }
136        }
137    
138    
139        protected E sendExchange(Endpoint<E> endpoint, Producer<E> producer, Processor processor, E exchange) throws Exception {
140            // lets populate using the processor callback
141            processor.process(exchange);
142    
143            // now lets dispatch
144            if (LOG.isDebugEnabled()) {
145                LOG.debug(">>>> " + endpoint + " " + exchange);
146            }
147            producer.process(exchange);
148            return exchange;
149        }
150    
151        protected boolean sendExchange(Endpoint<E> endpoint, Producer<E> producer, Processor processor, E exchange, AsyncCallback callback) throws Exception {
152            // lets populate using the processor callback
153            processor.process(exchange);
154    
155            // now lets dispatch
156            if (LOG.isDebugEnabled()) {
157                LOG.debug(">>>> " + endpoint + " " + exchange);
158            }
159            return AsyncProcessorTypeConverter.convert(producer).process(exchange, callback);
160        }
161    
162        protected void doStop() throws Exception {
163            ServiceHelper.stopServices(producers.values());
164        }
165    
166        protected void doStart() throws Exception {
167        }
168    }