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.impl.converter;
018    
019    import org.apache.camel.AsyncCallback;
020    import org.apache.camel.AsyncProcessor;
021    import org.apache.camel.Exchange;
022    import org.apache.camel.Processor;
023    import org.apache.camel.Service;
024    import org.apache.camel.TypeConverter;
025    import org.apache.camel.processor.DelegateProcessor;
026    
027    /**
028     * A simple converter that can convert any Processor to an AsyncProcessor.
029     * Processing will still occur synchronously but it will provide the required
030     * notifications that the caller expects.
031     * 
032     * @version $Revision: 21640 $
033     */
034    public class AsyncProcessorTypeConverter implements TypeConverter {
035    
036        public static final class ProcessorToAsynProcessorBridge extends DelegateProcessor implements AsyncProcessor {
037    
038            private ProcessorToAsynProcessorBridge(Processor processor) {
039                super(processor);
040            }
041    
042            public boolean process(Exchange exchange, AsyncCallback callback) {
043                try {
044                    processor.process(exchange);
045                } catch (Throwable e) {
046                    exchange.setException(e);
047                }
048                // false means processing of the exchange asynchronously,
049                callback.done(true);
050                return true;
051            }
052        }
053    
054        public <T> T convertTo(Class<T> toType, Object value) {
055            if (value != null) {
056                if (toType.equals(AsyncProcessor.class)) {
057                    if (value instanceof AsyncProcessor) {
058                        return toType.cast(value);
059                    } else if (value instanceof Processor) {
060                        // Provide an async bridge to the regular processor.
061                        final Processor processor = (Processor)value;
062                        return toType.cast(new ProcessorToAsynProcessorBridge(processor));
063                    }
064                }
065            }
066            return null;
067        }
068    
069        public static AsyncProcessor convert(Processor value) {
070            if (value instanceof AsyncProcessor) {
071                return (AsyncProcessor)value;
072            }
073            return new ProcessorToAsynProcessorBridge(value);
074        }
075    }