You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2016/07/28 07:11:58 UTC
[6/9] camel git commit: Added new blueprint resolvers
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 <ni...@gmail.com>
Authored: Wed Jul 27 19:15:18 2016 +0200
Committer: Claus Ibsen <da...@apache.org>
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;
+ }
+ }
+
+}