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 java.util.concurrent.ExecutionException;
020    import java.util.concurrent.Future;
021    
022    import org.apache.camel.Converter;
023    import org.apache.camel.Exchange;
024    import org.apache.camel.NoTypeConversionAvailableException;
025    import org.apache.camel.StreamCache;
026    import org.apache.camel.TypeConverter;
027    import org.apache.camel.util.ExchangeHelper;
028    import org.apache.camel.util.ObjectHelper;
029    import org.apache.commons.logging.Log;
030    import org.apache.commons.logging.LogFactory;
031    
032    /**
033     * Future type converter.
034     *
035     * @version $Revision: 772853 $
036     */
037    @Converter
038    public final class FutureTypeConverter implements TypeConverter {
039    
040        private static final Log LOG = LogFactory.getLog(FutureTypeConverter.class);
041    
042        private final TypeConverter converter;
043    
044        public FutureTypeConverter(TypeConverter converter) {
045            this.converter = converter;
046        }
047    
048        private <T> T doConvertTo(Class<T> type, Exchange exchange, Object value) throws Exception {
049            // do not convert to stream cache
050            if (StreamCache.class.isAssignableFrom(value.getClass())) {
051                return null;
052            }
053    
054            if (Future.class.isAssignableFrom(value.getClass())) {
055    
056                Future future = (Future) value;
057    
058                if (future.isCancelled()) {
059                    return null;
060                }
061    
062                // do some trace logging as the get is blocking until the response is ready
063                if (LOG.isTraceEnabled()) {
064                    LOG.trace("Getting future response");
065                }
066    
067                Object body;
068                try {
069                    body = future.get();
070                } catch (ExecutionException e) {
071                    if (e.getCause() instanceof Exception) {
072                        throw (Exception) e.getCause();
073                    } else {
074                        throw e;
075                    }
076                }
077                if (LOG.isTraceEnabled()) {
078                    LOG.trace("Got future response");
079                }
080    
081                if (body == null) {
082                    return null;
083                }
084    
085                // maybe from is already the type we want
086                if (type.isAssignableFrom(body.getClass())) {
087                    return type.cast(body);
088                } else if (body instanceof Exchange) {
089                    Exchange result = (Exchange) body;
090                    body = ExchangeHelper.extractResultBody(result, result.getPattern());
091                }
092    
093                // no then try to lookup a type converter
094                return converter.convertTo(type, exchange, body);
095            }
096    
097            return null;
098        }
099    
100        public <T> T convertTo(Class<T> type, Object value) {
101            return convertTo(type, null, value);
102        }
103    
104        public <T> T convertTo(Class<T> type, Exchange exchange, Object value) {
105            try {
106                return doConvertTo(type, exchange, value);
107            } catch (Exception e) {
108                throw ObjectHelper.wrapRuntimeCamelException(e);
109            }
110        }
111    
112        public <T> T mandatoryConvertTo(Class<T> type, Object value) throws NoTypeConversionAvailableException {
113            return mandatoryConvertTo(type, null, value);
114        }
115    
116        public <T> T mandatoryConvertTo(Class<T> type, Exchange exchange, Object value) throws NoTypeConversionAvailableException {
117            T answer;
118            try {
119                answer = doConvertTo(type, exchange, value);
120            } catch (Exception e) {
121                throw new NoTypeConversionAvailableException(value, type, e);
122            }
123    
124            if (answer == null) {
125                throw new NoTypeConversionAvailableException(value, type);
126            }
127    
128            return answer;
129        }
130    }