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.util;
018
019import org.apache.camel.CamelContext;
020import org.apache.camel.Component;
021import org.apache.camel.spi.DataFormat;
022import org.apache.camel.spi.DataFormatFactory;
023import org.apache.camel.spi.Language;
024import org.slf4j.Logger;
025import org.slf4j.LoggerFactory;
026
027/**
028 * Some helper methods for new resolvers (like {@link org.apache.camel.spi.ComponentResolver}, {@link org.apache.camel.spi.DataFormatResolver}, etc.).
029 *
030 * @version
031 */
032public final class ResolverHelper {
033
034    public static final String COMPONENT_FALLBACK_SUFFIX = "-component";
035
036    public static final String DATA_FORMAT_FALLBACK_SUFFIX = "-dataformat";
037
038    public static final String DATA_FORMAT_FACTORY_FALLBACK_SUFFIX = "-dataformat-factory";
039
040    public static final String LANGUAGE_FALLBACK_SUFFIX = "-language";
041
042    private static final Logger LOG = LoggerFactory.getLogger(ResolverHelper.class);
043
044    private static final LookupExceptionHandler EXCEPTION_HANDLER = new LookupExceptionHandler();
045
046    /**
047     * Utility classes should not have a public constructor.
048     */
049    private ResolverHelper() {
050    }
051
052    public static Component lookupComponentInRegistryWithFallback(CamelContext context, String name) {
053        return lookupComponentInRegistryWithFallback(context, name, EXCEPTION_HANDLER);
054    }
055
056    public static Component lookupComponentInRegistryWithFallback(CamelContext context, String name, LookupExceptionHandler exceptionHandler) {
057        Object bean = lookupInRegistry(context, Component.class, false, exceptionHandler, name, name + COMPONENT_FALLBACK_SUFFIX);
058        if (bean != null) {
059            if (bean instanceof Component) {
060                return (Component) bean;
061            } else {
062                // let's use Camel's type conversion mechanism to convert things like CamelContext
063                // and other types into a valid Component
064                Component component = CamelContextHelper.convertTo(context, Component.class, bean);
065                if (component != null) {
066                    return component;
067                }
068            }
069        }
070
071        if (bean != null) {
072            LOG.debug("Found Component with incompatible class: {}", bean.getClass().getName());
073        }
074        return null;
075    }
076
077    public static DataFormat lookupDataFormatInRegistryWithFallback(CamelContext context, String name) {
078        return lookupDataFormatInRegistryWithFallback(context, name, EXCEPTION_HANDLER);
079    }
080
081    public static DataFormat lookupDataFormatInRegistryWithFallback(CamelContext context, String name, LookupExceptionHandler exceptionHandler) {
082        Object bean = lookupInRegistry(context, DataFormat.class, false, exceptionHandler, name, name + DATA_FORMAT_FALLBACK_SUFFIX);
083        if (bean instanceof DataFormat) {
084            return (DataFormat) bean;
085        }
086
087        if (bean != null) {
088            LOG.debug("Found DataFormat with incompatible class: {}", bean.getClass().getName());
089        }
090        return null;
091    }
092
093    public static DataFormatFactory lookupDataFormatFactoryInRegistryWithFallback(CamelContext context, String name) {
094        return lookupDataFormatFactoryInRegistryWithFallback(context, name, EXCEPTION_HANDLER);
095    }
096
097    public static DataFormatFactory lookupDataFormatFactoryInRegistryWithFallback(CamelContext context, String name, LookupExceptionHandler exceptionHandler) {
098        Object bean = lookupInRegistry(context, DataFormatFactory.class, false, exceptionHandler, name, name + DATA_FORMAT_FACTORY_FALLBACK_SUFFIX);
099        if (bean instanceof DataFormatFactory) {
100            return (DataFormatFactory) bean;
101        }
102
103        if (bean != null) {
104            LOG.debug("Found DataFormatFactory with incompatible class: {}", bean.getClass().getName());
105        }
106        return null;
107    }
108
109    public static Language lookupLanguageInRegistryWithFallback(CamelContext context, String name) {
110        return lookupLanguageInRegistryWithFallback(context, name, EXCEPTION_HANDLER);
111    }
112
113    public static Language lookupLanguageInRegistryWithFallback(CamelContext context, String name, LookupExceptionHandler exceptionHandler) {
114        Object bean = lookupInRegistry(context, Language.class, false, exceptionHandler, name, name + LANGUAGE_FALLBACK_SUFFIX);
115        if (bean instanceof Language) {
116            return (Language) bean;
117        }
118
119        if (bean != null) {
120            LOG.debug("Found Language with incompatible class: {}", bean.getClass().getName());
121        }
122        return null;
123    }
124
125
126    public static class LookupExceptionHandler {
127
128        public void handleException(Exception e, Logger log, String name) {
129            log.debug("Ignored error looking up bean: " + name, e);
130        }
131
132    }
133
134    private static Object lookupInRegistry(CamelContext context, Class<?> type, boolean lookupByNameAndType, LookupExceptionHandler exceptionHandler, String... names) {
135        for (String name : names) {
136            try {
137                Object bean;
138                if (lookupByNameAndType) {
139                    bean = context.getRegistry().lookupByNameAndType(name, type);
140                } else {
141                    bean = context.getRegistry().lookupByName(name);
142                }
143                LOG.debug("Lookup {} with name {} in registry. Found: {}", type.getSimpleName(), name, bean);
144                if (bean != null) {
145                    return bean;
146                }
147            } catch (Exception e) {
148                exceptionHandler.handleException(e, LOG, name);
149            }
150        }
151
152        return null;
153    }
154
155}