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 */
017package org.apache.camel.impl.converter;
018
019import java.util.concurrent.Future;
020
021import org.apache.camel.Exchange;
022import org.apache.camel.Message;
023import org.apache.camel.WrappedFile;
024import org.apache.camel.component.bean.BeanInvocation;
025import org.apache.camel.support.TypeConverterSupport;
026
027/**
028 * A simple converter that can convert any object to a String type by using the
029 * toString() method of the object.
030 */
031public class ToStringTypeConverter extends TypeConverterSupport {
032
033    @SuppressWarnings("unchecked")
034    @Override
035    public <T> T convertTo(Class<T> toType, Exchange exchange, Object value) {
036
037        // should not try to convert Message
038        if (Message.class.isAssignableFrom(value.getClass())) {
039            return (T) Void.TYPE;
040        }
041
042        // should not try to convert future
043        if (Future.class.isAssignableFrom(value.getClass())) {
044            return (T) Void.TYPE;
045        }
046
047        // should not try to convert bean invocations
048        if (BeanInvocation.class.isAssignableFrom(value.getClass())) {
049            return (T) Void.TYPE;
050        }
051
052        // should not try to convert files
053        if (WrappedFile.class.isAssignableFrom(value.getClass())) {
054            return (T) Void.TYPE;
055        }
056
057        if (toType.equals(String.class)) {
058            return (T) value.toString();
059        }
060        return null;
061    }
062
063}