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;
018    
019    import org.apache.camel.CamelContext;
020    import org.apache.camel.NoFactoryAvailableException;
021    import org.apache.camel.spi.DataFormat;
022    import org.apache.camel.spi.DataFormatResolver;
023    import org.apache.camel.spi.FactoryFinder;
024    
025    /**
026     * Default data format resolver
027     *
028     * @version 
029     */
030    public class DefaultDataFormatResolver implements DataFormatResolver {
031    
032        public static final String DATAFORMAT_RESOURCE_PATH = "META-INF/services/org/apache/camel/dataformat/";
033    
034        protected FactoryFinder dataformatFactory;
035    
036        public DataFormat resolveDataFormat(String name, CamelContext context) {
037            DataFormat dataFormat = lookup(context, name, DataFormat.class);
038            if (dataFormat == null) {
039                Class<?> type = null;
040                try {
041                    if (dataformatFactory == null) {
042                        dataformatFactory = context.getFactoryFinder(DATAFORMAT_RESOURCE_PATH);
043                    }
044                    type = dataformatFactory.findClass(name);
045                } catch (NoFactoryAvailableException e) {
046                    // ignore
047                } catch (Exception e) {
048                    throw new IllegalArgumentException("Invalid URI, no DataFormat registered for scheme: " + name, e);
049                }
050    
051                if (type == null) {
052                    type = context.getClassResolver().resolveClass(name);
053                }
054    
055                if (type != null) {
056                    if (DataFormat.class.isAssignableFrom(type)) {
057                        dataFormat = (DataFormat) context.getInjector().newInstance(type);
058                    } else {
059                        throw new IllegalArgumentException("Resolving dataformat: " + name + " detected type conflict: Not a DataFormat implementation. Found: " + type.getName());
060                    }
061                }
062            }
063    
064            return dataFormat;
065        }
066    
067        private static <T> T lookup(CamelContext context, String ref, Class<T> type) {
068            try {
069                return context.getRegistry().lookupByNameAndType(ref, type);
070            } catch (Exception e) {
071                // need to ignore not same type and return it as null
072                return null;
073            }
074        }
075    }