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.DataFormatFactory; 023import org.apache.camel.spi.DataFormatResolver; 024import org.apache.camel.spi.FactoryFinder; 025import org.apache.camel.util.ResolverHelper; 026 027/** 028 * Default data format resolver 029 * 030 * @version 031 */ 032public class DefaultDataFormatResolver implements DataFormatResolver { 033 public static final String DATAFORMAT_RESOURCE_PATH = "META-INF/services/org/apache/camel/dataformat/"; 034 035 private FactoryFinder dataformatFactory; 036 037 @Override 038 public DataFormat resolveDataFormat(String name, CamelContext context) { 039 // lookup in registry first 040 DataFormat dataFormat = ResolverHelper.lookupDataFormatInRegistryWithFallback(context, name); 041 042 if (dataFormat == null) { 043 // If not found in the registry, try to create a new instance using 044 // a DataFormatFactory or from resources 045 dataFormat = createDataFormat(name, context); 046 } 047 048 return dataFormat; 049 } 050 051 @Override 052 public DataFormat createDataFormat(String name, CamelContext context) { 053 DataFormat dataFormat = null; 054 055 // lookup in registry first 056 DataFormatFactory dataFormatFactory = ResolverHelper.lookupDataFormatFactoryInRegistryWithFallback(context, name); 057 if (dataFormatFactory != null) { 058 dataFormat = dataFormatFactory.newInstance(); 059 } 060 061 if (dataFormat == null) { 062 dataFormat = createDataFormatFromResource(name, context); 063 } 064 065 return dataFormat; 066 } 067 068 private DataFormat createDataFormatFromResource(String name, CamelContext context) { 069 DataFormat dataFormat = null; 070 071 Class<?> type = null; 072 try { 073 if (dataformatFactory == null) { 074 dataformatFactory = context.getFactoryFinder(DATAFORMAT_RESOURCE_PATH); 075 } 076 type = dataformatFactory.findClass(name); 077 } catch (NoFactoryAvailableException e) { 078 // ignore 079 } catch (Exception e) { 080 throw new IllegalArgumentException("Invalid URI, no DataFormat registered for scheme: " + name, e); 081 } 082 083 if (type == null) { 084 type = context.getClassResolver().resolveClass(name); 085 } 086 087 if (type != null) { 088 if (DataFormat.class.isAssignableFrom(type)) { 089 dataFormat = (DataFormat) context.getInjector().newInstance(type); 090 } else { 091 throw new IllegalArgumentException("Resolving dataformat: " + name + " detected type conflict: Not a DataFormat implementation. Found: " + type.getName()); 092 } 093 } 094 095 return dataFormat; 096 } 097 098}