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.component.directvm;
018    
019    import org.apache.camel.AsyncCallback;
020    import org.apache.camel.AsyncProcessor;
021    import org.apache.camel.CamelExchangeException;
022    import org.apache.camel.Exchange;
023    import org.apache.camel.impl.DefaultAsyncProducer;
024    import org.apache.camel.util.AsyncProcessorConverterHelper;
025    import org.apache.camel.util.AsyncProcessorHelper;
026    import org.slf4j.Logger;
027    import org.slf4j.LoggerFactory;
028    
029    /**
030     * The direct-vm producer
031     */
032    public class DirectVmProducer extends DefaultAsyncProducer {
033    
034        private static final transient Logger LOG = LoggerFactory.getLogger(DirectVmProducer.class);
035        private DirectVmEndpoint endpoint;
036    
037        public DirectVmProducer(DirectVmEndpoint endpoint) {
038            super(endpoint);
039            this.endpoint = endpoint;
040        }
041    
042        @Override
043        public void process(Exchange exchange) throws Exception {
044            // send to consumer
045            DirectVmConsumer consumer = endpoint.getComponent().getConsumer(endpoint);
046            if (consumer == null) {
047                LOG.warn("No consumers available on endpoint: " + endpoint + " to process: " + exchange);
048                throw new CamelExchangeException("No consumers available on endpoint: " + endpoint, exchange);
049            } else {
050                consumer.getProcessor().process(exchange);
051            }
052        }
053    
054        @Override
055        public boolean process(Exchange exchange, AsyncCallback callback) {
056            // send to consumer
057            DirectVmConsumer consumer = endpoint.getComponent().getConsumer(endpoint);
058            if (consumer == null) {
059                LOG.warn("No consumers available on endpoint: " + endpoint + " to process: " + exchange);
060                exchange.setException(new CamelExchangeException("No consumers available on endpoint: " + endpoint, exchange));
061                callback.done(true);
062                return true;
063            } else {
064                AsyncProcessor processor = AsyncProcessorConverterHelper.convert(consumer.getProcessor());
065                return AsyncProcessorHelper.process(processor, exchange, callback);
066            }
067        }
068    }