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 java.io.IOException;
020
021import org.apache.camel.CamelContext;
022import org.apache.camel.spi.FactoryFinder;
023import org.apache.camel.spi.HeadersMapFactory;
024import org.slf4j.Logger;
025import org.slf4j.LoggerFactory;
026
027/**
028 * Factory to create the {@link HeadersMapFactory} implementation to be used.
029 *
030 * @see HeadersMapFactory
031 */
032public class HeadersMapFactoryResolver {
033
034    public static final String RESOURCE_PATH = "META-INF/services/org/apache/camel/";
035
036    private static final Logger LOG = LoggerFactory.getLogger(HeadersMapFactoryResolver.class);
037
038    private FactoryFinder factoryFinder;
039
040    public HeadersMapFactory resolve(CamelContext context) {
041        // use factory finder to find a custom implementations
042        Class<?> type = null;
043        try {
044            type = findFactory("headers-map-factory", context);
045        } catch (Exception e) {
046            // ignore
047        }
048
049        if (type != null) {
050            if (LOG.isDebugEnabled()) {
051                LOG.debug("Found HeadersMapFactory: {} via: {}{}", type.getName(), factoryFinder.getResourcePath(), "headers-map-factory");
052            }
053            if (HeadersMapFactory.class.isAssignableFrom(type)) {
054                HeadersMapFactory answer = (HeadersMapFactory) context.getInjector().newInstance(type);
055                LOG.info("Detected and using custom HeadersMapFactory: {}", answer);
056                return answer;
057            } else {
058                throw new IllegalArgumentException("Type is not a HeadersMapFactory implementation. Found: " + type.getName());
059            }
060        }
061
062        // fallback to default
063        LOG.debug("Creating default HeadersMapFactory");
064        return new DefaultHeadersMapFactory();
065    }
066
067    private Class<?> findFactory(String name, CamelContext context) throws ClassNotFoundException, IOException {
068        if (factoryFinder == null) {
069            factoryFinder = context.getFactoryFinder(RESOURCE_PATH);
070        }
071        return factoryFinder.findClass(name);
072    }
073
074}
075