Refactoring core
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/8ec0cd11 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/8ec0cd11 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/8ec0cd11 Branch: refs/heads/master Commit: 8ec0cd119974459754da041b370b6fb822b11634 Parents: ded7e74 Author: Nicola Ferraro <ni.ferr...@gmail.com> Authored: Wed Jul 27 18:32:14 2016 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Thu Jul 28 08:41:34 2016 +0200 ---------------------------------------------------------------------- .../camel/impl/DefaultComponentResolver.java | 37 +---- .../camel/impl/DefaultDataFormatResolver.java | 19 +-- .../camel/impl/DefaultLanguageResolver.java | 19 +-- .../org/apache/camel/util/ResolverHelper.java | 136 +++++++++++++++++++ 4 files changed, 150 insertions(+), 61 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/8ec0cd11/camel-core/src/main/java/org/apache/camel/impl/DefaultComponentResolver.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/impl/DefaultComponentResolver.java b/camel-core/src/main/java/org/apache/camel/impl/DefaultComponentResolver.java index 53c8b79..bee54e4 100644 --- a/camel-core/src/main/java/org/apache/camel/impl/DefaultComponentResolver.java +++ b/camel-core/src/main/java/org/apache/camel/impl/DefaultComponentResolver.java @@ -23,7 +23,7 @@ import org.apache.camel.Component; import org.apache.camel.NoFactoryAvailableException; import org.apache.camel.spi.ComponentResolver; import org.apache.camel.spi.FactoryFinder; -import org.apache.camel.util.CamelContextHelper; +import org.apache.camel.util.ResolverHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -45,20 +45,9 @@ public class DefaultComponentResolver implements ComponentResolver { public Component resolveComponent(String name, CamelContext context) { // lookup in registry first - Object bean = lookupInRegistry(context, name, name + "-component"); - - if (bean != null) { - if (bean instanceof Component) { - return (Component) bean; - } else { - // lets use Camel's type conversion mechanism to convert things like CamelContext - // and other types into a valid Component - Component component = CamelContextHelper.convertTo(context, Component.class, bean); - if (component != null) { - return component; - } - } - // we do not throw the exception here and try to auto create a component + Component componentReg = ResolverHelper.lookupComponentInRegistryWithFallback(context, name); + if (componentReg != null) { + return componentReg; } // not in registry then use component factory @@ -87,24 +76,6 @@ public class DefaultComponentResolver implements ComponentResolver { } } - private Object lookupInRegistry(CamelContext context, String... names) { - Object bean = null; - for (String name : names) { - try { - bean = context.getRegistry().lookupByName(name); - getLog().debug("Lookup component with name {} in registry. Found: {}", name, bean); - } catch (Exception e) { - getLog().debug("Ignored error looking up bean: " + name, e); - } - - if (bean != null) { - return bean; - } - } - - return null; - } - private Class<?> findComponent(String name, CamelContext context) throws ClassNotFoundException, IOException { if (factoryFinder == null) { factoryFinder = context.getFactoryFinder(RESOURCE_PATH); http://git-wip-us.apache.org/repos/asf/camel/blob/8ec0cd11/camel-core/src/main/java/org/apache/camel/impl/DefaultDataFormatResolver.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/impl/DefaultDataFormatResolver.java b/camel-core/src/main/java/org/apache/camel/impl/DefaultDataFormatResolver.java index 5a20fd1..d777546 100644 --- a/camel-core/src/main/java/org/apache/camel/impl/DefaultDataFormatResolver.java +++ b/camel-core/src/main/java/org/apache/camel/impl/DefaultDataFormatResolver.java @@ -21,6 +21,7 @@ import org.apache.camel.NoFactoryAvailableException; import org.apache.camel.spi.DataFormat; import org.apache.camel.spi.DataFormatResolver; import org.apache.camel.spi.FactoryFinder; +import org.apache.camel.util.ResolverHelper; /** * Default data format resolver @@ -34,7 +35,9 @@ public class DefaultDataFormatResolver implements DataFormatResolver { protected FactoryFinder dataformatFactory; public DataFormat resolveDataFormat(String name, CamelContext context) { - DataFormat dataFormat = lookup(context, DataFormat.class, name, name + "-dataformat"); + // lookup in registry first + DataFormat dataFormat = ResolverHelper.lookupDataFormatInRegistryWithFallback(context, name); + if (dataFormat == null) { Class<?> type = null; try { @@ -64,18 +67,4 @@ public class DefaultDataFormatResolver implements DataFormatResolver { return dataFormat; } - private static <T> T lookup(CamelContext context, Class<T> type, String... names) { - for (String name : names) { - try { - T bean = context.getRegistry().lookupByNameAndType(name, type); - if (bean != null) { - return bean; - } - } catch (Exception e) { - // need to ignore not same type - } - } - // return null if anything goes wrong - return null; - } } http://git-wip-us.apache.org/repos/asf/camel/blob/8ec0cd11/camel-core/src/main/java/org/apache/camel/impl/DefaultLanguageResolver.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/impl/DefaultLanguageResolver.java b/camel-core/src/main/java/org/apache/camel/impl/DefaultLanguageResolver.java index 9fe1c42..e77b4ea 100644 --- a/camel-core/src/main/java/org/apache/camel/impl/DefaultLanguageResolver.java +++ b/camel-core/src/main/java/org/apache/camel/impl/DefaultLanguageResolver.java @@ -22,6 +22,7 @@ import org.apache.camel.NoSuchLanguageException; import org.apache.camel.spi.FactoryFinder; import org.apache.camel.spi.Language; import org.apache.camel.spi.LanguageResolver; +import org.apache.camel.util.ResolverHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -42,13 +43,9 @@ public class DefaultLanguageResolver implements LanguageResolver { public Language resolveLanguage(String name, CamelContext context) { // lookup in registry first - Object bean = lookupInRegistry(context, name, name + "-language"); - - if (bean != null) { - if (bean instanceof Language) { - return (Language) bean; - } - // we do not throw the exception here and try to auto create a Language from META-INF + Language languageReg = ResolverHelper.lookupLanguageInRegistryWithFallback(context, name); + if (languageReg != null) { + return languageReg; } Class<?> type = null; @@ -73,22 +70,18 @@ public class DefaultLanguageResolver implements LanguageResolver { } private Object lookupInRegistry(CamelContext context, String... names) { - Object bean = null; for (String name : names) { try { - bean = context.getRegistry().lookupByName(name); + Object bean = context.getRegistry().lookupByName(name); if (bean != null) { getLog().debug("Found language: {} in registry: {}", name, bean); + return bean; } } catch (Exception e) { if (getLog().isDebugEnabled()) { getLog().debug("Ignored error looking up bean: " + name + ". Error: " + e); } } - - if (bean != null) { - return bean; - } } return null; http://git-wip-us.apache.org/repos/asf/camel/blob/8ec0cd11/camel-core/src/main/java/org/apache/camel/util/ResolverHelper.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/util/ResolverHelper.java b/camel-core/src/main/java/org/apache/camel/util/ResolverHelper.java new file mode 100644 index 0000000..a972c4d --- /dev/null +++ b/camel-core/src/main/java/org/apache/camel/util/ResolverHelper.java @@ -0,0 +1,136 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.util; + +import org.apache.camel.CamelContext; +import org.apache.camel.Component; +import org.apache.camel.spi.DataFormat; +import org.apache.camel.spi.Language; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Some helper methods for new resolvers (like {@link org.apache.camel.spi.ComponentResolver}, {@link org.apache.camel.spi.DataFormatResolver}, etc.). + * + * @version + */ +public final class ResolverHelper { + + public static final String COMPONENT_FALLBACK_SUFFIX = "-component"; + + public static final String DATA_FORMAT_FALLBACK_SUFFIX = "-dataformat"; + + public static final String LANGUAGE_FALLBACK_SUFFIX = "-language"; + + private static final Logger LOG = LoggerFactory.getLogger(ResolverHelper.class); + + private static final LookupExceptionHandler EXCEPTION_HANDLER = new LookupExceptionHandler(); + + /** + * Utility classes should not have a public constructor. + */ + private ResolverHelper() { + } + + public static Component lookupComponentInRegistryWithFallback(CamelContext context, String name) { + return lookupComponentInRegistryWithFallback(context, name, EXCEPTION_HANDLER); + } + + public static Component lookupComponentInRegistryWithFallback(CamelContext context, String name, LookupExceptionHandler exceptionHandler) { + Object bean = lookupInRegistry(context, Component.class, false, exceptionHandler, name, name + COMPONENT_FALLBACK_SUFFIX); + if (bean != null) { + if (bean instanceof Component) { + return (Component) bean; + } else { + // let's use Camel's type conversion mechanism to convert things like CamelContext + // and other types into a valid Component + Component component = CamelContextHelper.convertTo(context, Component.class, bean); + if (component != null) { + return component; + } + } + } + + if (bean != null) { + LOG.debug("Found Component with incompatible class: {}", bean.getClass().getName()); + } + return null; + } + + public static DataFormat lookupDataFormatInRegistryWithFallback(CamelContext context, String name) { + return lookupDataFormatInRegistryWithFallback(context, name, EXCEPTION_HANDLER); + } + + public static DataFormat lookupDataFormatInRegistryWithFallback(CamelContext context, String name, LookupExceptionHandler exceptionHandler) { + Object bean = lookupInRegistry(context, DataFormat.class, false, exceptionHandler, name, name + DATA_FORMAT_FALLBACK_SUFFIX); + if (bean instanceof DataFormat) { + return (DataFormat) bean; + } + + if (bean != null) { + LOG.debug("Found DataFormat with incompatible class: {}", bean.getClass().getName()); + } + return null; + } + + public static Language lookupLanguageInRegistryWithFallback(CamelContext context, String name) { + return lookupLanguageInRegistryWithFallback(context, name, EXCEPTION_HANDLER); + } + + public static Language lookupLanguageInRegistryWithFallback(CamelContext context, String name, LookupExceptionHandler exceptionHandler) { + Object bean = lookupInRegistry(context, Language.class, false, exceptionHandler, name, name + LANGUAGE_FALLBACK_SUFFIX); + if (bean instanceof Language) { + return (Language) bean; + } + + if (bean != null) { + LOG.debug("Found Language with incompatible class: {}", bean.getClass().getName()); + } + return null; + } + + + public static class LookupExceptionHandler { + + public void handleException(Exception e, Logger log, String name) { + log.debug("Ignored error looking up bean: " + name, e); + } + + } + + private static Object lookupInRegistry(CamelContext context, Class<?> type, boolean lookupByNameAndType, LookupExceptionHandler exceptionHandler, String... names) { + for (String name : names) { + try { + Object bean; + if (lookupByNameAndType) { + bean = context.getRegistry().lookupByNameAndType(name, type); + } else { + bean = context.getRegistry().lookupByName(name); + } + LOG.debug("Lookup {} with name {} in registry. Found: {}", type.getSimpleName(), name, bean); + if (bean != null) { + return bean; + } + } catch (Exception e) { + exceptionHandler.handleException(e, LOG, name); + } + } + + return null; + } + +}