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.lang.reflect.Method;
020
021import org.apache.camel.Exchange;
022import org.apache.camel.RuntimeCamelException;
023import org.apache.camel.spi.TypeConverterRegistry;
024import org.apache.camel.support.TypeConverterSupport;
025import org.apache.camel.util.ObjectHelper;
026
027/**
028 * A {@link org.apache.camel.TypeConverter} implementation which instantiates an object
029 * so that an instance method can be used as a fallback type converter
030 *
031 * @version 
032 */
033public class InstanceMethodFallbackTypeConverter extends TypeConverterSupport {
034    private final CachingInjector<?> injector;
035    private final Method method;
036    private final boolean useExchange;
037    private final TypeConverterRegistry registry;
038    private final boolean allowNull;
039
040    @Deprecated
041    public InstanceMethodFallbackTypeConverter(CachingInjector<?> injector, Method method, TypeConverterRegistry registry) {
042        this(injector, method, registry, false);
043    }
044
045    public InstanceMethodFallbackTypeConverter(CachingInjector<?> injector, Method method, TypeConverterRegistry registry, boolean allowNull) {
046        this.injector = injector;
047        this.method = method;
048        this.useExchange = method.getParameterCount() == 4;
049        this.registry = registry;
050        this.allowNull = allowNull;
051    }
052
053    @Override
054    public String toString() {
055        return "InstanceMethodFallbackTypeConverter: " + method;
056    }
057
058    @Override
059    public boolean allowNull() {
060        return allowNull;
061    }
062
063    @SuppressWarnings("unchecked")
064    public <T> T convertTo(Class<T> type, Exchange exchange, Object value) {
065        Object instance = injector.newInstance();
066        if (instance == null) {
067            throw new RuntimeCamelException("Could not instantiate an instance of: " + type.getCanonicalName());
068        }
069        return useExchange
070            ? (T)ObjectHelper.invokeMethod(method, instance, type, exchange, value, registry) : (T)ObjectHelper
071                .invokeMethod(method, instance, type, value, registry);
072    }
073
074}