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