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.Future;
020
021 import org.apache.camel.Converter;
022 import org.apache.camel.Exchange;
023 import org.apache.camel.StreamCache;
024 import org.apache.camel.TypeConversionException;
025 import org.apache.camel.TypeConverter;
026 import org.apache.camel.support.TypeConverterSupport;
027 import org.apache.camel.util.ExchangeHelper;
028 import org.slf4j.Logger;
029 import org.slf4j.LoggerFactory;
030
031 /**
032 * Future type converter.
033 *
034 * @version
035 */
036 @Converter
037 public final class FutureTypeConverter extends TypeConverterSupport {
038
039 private static final Logger LOG = LoggerFactory.getLogger(FutureTypeConverter.class);
040
041 private final TypeConverter converter;
042
043 public FutureTypeConverter(TypeConverter converter) {
044 this.converter = converter;
045 }
046
047 @Override
048 public <T> T convertTo(Class<T> type, Exchange exchange, Object value) {
049 try {
050 return doConvertTo(type, exchange, value);
051 } catch (Exception e) {
052 throw new TypeConversionException(value, type, e);
053 }
054 }
055
056 @SuppressWarnings("unchecked")
057 private <T> T doConvertTo(Class<T> type, Exchange exchange, Object value) throws Exception {
058 // do not convert to stream cache
059 if (StreamCache.class.isAssignableFrom(value.getClass())) {
060 return null;
061 }
062
063 if (Future.class.isAssignableFrom(value.getClass())) {
064
065 Future<?> future = (Future<?>) value;
066
067 if (future.isCancelled()) {
068 // return void to indicate its not possible to convert at this time
069 return (T) Void.TYPE;
070 }
071
072 // do some trace logging as the get is blocking until the response is ready
073 LOG.trace("Getting future response");
074
075 Object body = future.get();
076 LOG.trace("Got future response");
077
078 if (body == null) {
079 // return void to indicate its not possible to convert at this time
080 return (T) Void.TYPE;
081 }
082
083 // maybe from is already the type we want
084 if (type.isAssignableFrom(body.getClass())) {
085 return type.cast(body);
086 } else if (body instanceof Exchange) {
087 Exchange result = (Exchange) body;
088 body = ExchangeHelper.extractResultBody(result, result.getPattern());
089 }
090
091 // no then convert to the type
092 return converter.convertTo(type, exchange, body);
093 }
094
095 return null;
096 }
097
098 }