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.model.DataFormatDefinition;
022 import org.apache.camel.spi.DataFormat;
023 import org.apache.camel.spi.DataFormatResolver;
024 import org.apache.camel.spi.FactoryFinder;
025
026 /**
027 * Default data format resolver
028 *
029 * @version $Revision: 835732 $
030 */
031 public 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 DataFormat dataFormat = lookup(context, name, DataFormat.class);
039 if (dataFormat == null) {
040 Class<?> type = null;
041 try {
042 if (dataformatFactory == null) {
043 dataformatFactory = context.getFactoryFinder(DATAFORMAT_RESOURCE_PATH);
044 }
045 type = dataformatFactory.findClass(name);
046 } catch (NoFactoryAvailableException e) {
047 // ignore
048 } catch (Exception e) {
049 throw new IllegalArgumentException("Invalid URI, no DataFormat registered for scheme: " + name, e);
050 }
051
052 if (type == null) {
053 type = context.getClassResolver().resolveClass(name);
054 }
055
056 if (type != null) {
057 if (DataFormat.class.isAssignableFrom(type)) {
058 dataFormat = (DataFormat) context.getInjector().newInstance(type);
059 } else {
060 throw new IllegalArgumentException("Resolving dataformat: " + name + " detected type conflict: Not a DataFormat implementation. Found: " + type.getName());
061 }
062 }
063 }
064
065 return dataFormat;
066 }
067
068 public DataFormatDefinition resolveDataFormatDefinition(String name, CamelContext context) {
069 // lookup type and create the data format from it
070 DataFormatDefinition type = lookup(context, name, DataFormatDefinition.class);
071 if (type == null && context.getDataFormats() != null) {
072 type = context.getDataFormats().get(name);
073 }
074 return type;
075 }
076
077 private static <T> T lookup(CamelContext context, String ref, Class<T> type) {
078 try {
079 return context.getRegistry().lookup(ref, type);
080 } catch (Exception e) {
081 // need to ignore not same type and return it as null
082 return null;
083 }
084 }
085 }