Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 396CC200B78 for ; Thu, 28 Jul 2016 09:11:55 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 3849C160A85; Thu, 28 Jul 2016 07:11:55 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 5BD65160AAC for ; Thu, 28 Jul 2016 09:11:54 +0200 (CEST) Received: (qmail 5857 invoked by uid 500); 28 Jul 2016 07:11:53 -0000 Mailing-List: contact commits-help@camel.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@camel.apache.org Delivered-To: mailing list commits@camel.apache.org Received: (qmail 5519 invoked by uid 99); 28 Jul 2016 07:11:53 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 28 Jul 2016 07:11:53 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 2EBEEE943B; Thu, 28 Jul 2016 07:11:53 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: davsclaus@apache.org To: commits@camel.apache.org Date: Thu, 28 Jul 2016 07:11:58 -0000 Message-Id: <5741efb661e14e00a702e632f4967da7@git.apache.org> In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [6/9] camel git commit: Added new blueprint resolvers archived-at: Thu, 28 Jul 2016 07:11:55 -0000 Added new blueprint resolvers Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/7e39e027 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/7e39e027 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/7e39e027 Branch: refs/heads/master Commit: 7e39e0278ec5235e0c51633dd713ae3b5b5fa622 Parents: 8caf3c0 Author: Nicola Ferraro Authored: Wed Jul 27 19:15:18 2016 +0200 Committer: Claus Ibsen Committed: Thu Jul 28 08:41:34 2016 +0200 ---------------------------------------------------------------------- .../blueprint/BlueprintComponentResolver.java | 32 +++--- .../BlueprintComponentResolverTest.java | 106 +++++++++++++++++++ 2 files changed, 120 insertions(+), 18 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/7e39e027/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintComponentResolver.java ---------------------------------------------------------------------- diff --git a/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintComponentResolver.java b/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintComponentResolver.java index 9ec35c3..3cc0021 100644 --- a/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintComponentResolver.java +++ b/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintComponentResolver.java @@ -20,7 +20,7 @@ import org.apache.camel.CamelContext; import org.apache.camel.Component; import org.apache.camel.core.osgi.OsgiComponentResolver; import org.apache.camel.spi.ComponentResolver; -import org.apache.camel.util.CamelContextHelper; +import org.apache.camel.util.ResolverHelper; import org.osgi.framework.BundleContext; import org.osgi.service.blueprint.container.NoSuchComponentException; import org.slf4j.Logger; @@ -38,26 +38,22 @@ public class BlueprintComponentResolver extends OsgiComponentResolver { @Override public Component resolveComponent(String name, CamelContext context) throws Exception { - try { - Object bean = context.getRegistry().lookupByName(name); - if (bean instanceof Component) { - LOG.debug("Found component: {} in registry: {}", name, bean); - 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; + + Component componentReg = ResolverHelper.lookupComponentInRegistryWithFallback(context, name, new ResolverHelper.LookupExceptionHandler() { + @Override + public void handleException(Exception e, Logger log, String name) { + if (getException(NoSuchComponentException.class, e) != null) { + // if the caused error is NoSuchComponentException then that can be expected so ignore + } else { + LOG.trace("Ignored error looking up bean: " + name + " due: " + e.getMessage(), e); } } - } catch (Exception e) { - if (getException(NoSuchComponentException.class, e) != null) { - // if the caused error is NoSuchComponentException then that can be expected so ignore - } else { - LOG.trace("Ignored error looking up bean: " + name + " due: " + e.getMessage(), e); - } + }); + + if (componentReg != null) { + return componentReg; } + try { Object bean = context.getRegistry().lookupByName(".camelBlueprint.componentResolver." + name); if (bean instanceof ComponentResolver) { http://git-wip-us.apache.org/repos/asf/camel/blob/7e39e027/components/camel-blueprint/src/test/java/org/apache/camel/blueprint/BlueprintComponentResolverTest.java ---------------------------------------------------------------------- diff --git a/components/camel-blueprint/src/test/java/org/apache/camel/blueprint/BlueprintComponentResolverTest.java b/components/camel-blueprint/src/test/java/org/apache/camel/blueprint/BlueprintComponentResolverTest.java new file mode 100644 index 0000000..5d30fc4 --- /dev/null +++ b/components/camel-blueprint/src/test/java/org/apache/camel/blueprint/BlueprintComponentResolverTest.java @@ -0,0 +1,106 @@ +/** + * 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.blueprint; + +import org.apache.camel.CamelContext; +import org.apache.camel.Component; +import org.apache.camel.ComponentConfiguration; +import org.apache.camel.Endpoint; +import org.apache.camel.EndpointConfiguration; +import org.apache.camel.impl.DefaultCamelContext; +import org.apache.camel.impl.SimpleRegistry; +import org.apache.camel.test.junit4.TestSupport; +import org.junit.Test; + +public class BlueprintComponentResolverTest extends TestSupport { + + @Test + public void testOsgiResolverFindComponentFallbackTest() throws Exception { + SimpleRegistry registry = new SimpleRegistry(); + registry.put("allstar-component", new SampleComponent(true)); + + CamelContext camelContext = new DefaultCamelContext(registry); + + BlueprintComponentResolver resolver = new BlueprintComponentResolver(null); + Component component = resolver.resolveComponent("allstar", camelContext); + assertNotNull("We should find the super component", component); + assertTrue("We should get the super component here", component instanceof SampleComponent); + } + + @Test + public void testOsgiResolverFindLanguageDoubleFallbackTest() throws Exception { + SimpleRegistry registry = new SimpleRegistry(); + registry.put("allstar", new SampleComponent(false)); + registry.put("allstar-component", new SampleComponent(true)); + + CamelContext camelContext = new DefaultCamelContext(registry); + + BlueprintComponentResolver resolver = new BlueprintComponentResolver(null); + Component component = resolver.resolveComponent("allstar", camelContext); + assertNotNull("We should find the super component", component); + assertTrue("We should get the super component here", component instanceof SampleComponent); + assertFalse("We should NOT find the fallback component", ((SampleComponent) component).isFallback()); + } + + private static class SampleComponent implements Component { + + private boolean fallback; + + public SampleComponent(boolean fallback) { + this.fallback = fallback; + } + + @Override + public void setCamelContext(CamelContext camelContext) { + throw new UnsupportedOperationException("Should not be called"); + } + + @Override + public CamelContext getCamelContext() { + throw new UnsupportedOperationException("Should not be called"); + } + + @Override + public Endpoint createEndpoint(String uri) throws Exception { + throw new UnsupportedOperationException("Should not be called"); + } + + @Override + public boolean useRawUri() { + throw new UnsupportedOperationException("Should not be called"); + } + + @Override + public EndpointConfiguration createConfiguration(String uri) throws Exception { + throw new UnsupportedOperationException("Should not be called"); + } + + @Override + public ComponentConfiguration createComponentConfiguration() { + throw new UnsupportedOperationException("Should not be called"); + } + + public boolean isFallback() { + return fallback; + } + + public void setFallback(boolean fallback) { + this.fallback = fallback; + } + } + +}