camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [3/9] camel git commit: Changed language resolver
Date Thu, 28 Jul 2016 07:11:55 GMT
Changed language resolver


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/ded7e742
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/ded7e742
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/ded7e742

Branch: refs/heads/master
Commit: ded7e742687d8d632093ff623d60f0fd9f3a18b1
Parents: ef8edc8
Author: Nicola Ferraro <ni.ferraro@gmail.com>
Authored: Wed Jul 27 16:57:22 2016 +0200
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Thu Jul 28 08:41:34 2016 +0200

----------------------------------------------------------------------
 .../camel/impl/DefaultComponentResolver.java    |  4 +-
 .../camel/impl/DefaultLanguageResolver.java     | 47 ++++++++-----
 .../impl/DefaultCamelContextResolverTest.java   | 74 ++++++++++++++++++--
 3 files changed, 99 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/ded7e742/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 abce3af..53c8b79 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
@@ -98,11 +98,11 @@ public class DefaultComponentResolver implements ComponentResolver {
             }
 
             if (bean != null) {
-                break;
+                return bean;
             }
         }
 
-        return bean;
+        return null;
     }
 
     private Class<?> findComponent(String name, CamelContext context) throws ClassNotFoundException,
IOException {

http://git-wip-us.apache.org/repos/asf/camel/blob/ded7e742/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 161e1d9..9fe1c42 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
@@ -29,7 +29,7 @@ import org.slf4j.LoggerFactory;
  * Default language resolver that looks for language factories in <b>META-INF/services/org/apache/camel/language/</b>
and
  * language resolvers in <b>META-INF/services/org/apache/camel/language/resolver/</b>.
  *
- * @version 
+ * @version
  */
 public class DefaultLanguageResolver implements LanguageResolver {
     public static final String LANGUAGE_RESOURCE_PATH = "META-INF/services/org/apache/camel/language/";
@@ -42,20 +42,11 @@ public class DefaultLanguageResolver implements LanguageResolver {
 
     public Language resolveLanguage(String name, CamelContext context) {
         // lookup in registry first
-        Object bean = null;
-        try {
-            bean = context.getRegistry().lookupByName(name);
-            if (bean != null) {
-                getLog().debug("Found language: {} in registry: {}", name, bean);
-            }
-        } catch (Exception e) {
-            if (getLog().isDebugEnabled()) {
-                getLog().debug("Ignored error looking up bean: " + name + ". Error: " + e);
-            }
-        }
+        Object bean = lookupInRegistry(context, name, name + "-language");
+
         if (bean != null) {
             if (bean instanceof Language) {
-                return (Language)bean;
+                return (Language) bean;
             }
             // we do not throw the exception here and try to auto create a Language from
META-INF
         }
@@ -71,7 +62,7 @@ public class DefaultLanguageResolver implements LanguageResolver {
 
         if (type != null) {
             if (Language.class.isAssignableFrom(type)) {
-                return (Language)context.getInjector().newInstance(type);
+                return (Language) context.getInjector().newInstance(type);
             } else {
                 throw new IllegalArgumentException("Resolving language: " + name + " detected
type conflict: Not a Language implementation. Found: " + type.getName());
             }
@@ -81,6 +72,28 @@ 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);
+                if (bean != null) {
+                    getLog().debug("Found language: {} in registry: {}", name, bean);
+                }
+            } catch (Exception e) {
+                if (getLog().isDebugEnabled()) {
+                    getLog().debug("Ignored error looking up bean: " + name + ". Error: "
+ e);
+                }
+            }
+
+            if (bean != null) {
+                return bean;
+            }
+        }
+
+        return null;
+    }
+
     protected Language noSpecificLanguageFound(String name, CamelContext context) {
         Class<?> type = null;
         try {
@@ -94,7 +107,7 @@ public class DefaultLanguageResolver implements LanguageResolver {
         }
         if (type != null) {
             if (LanguageResolver.class.isAssignableFrom(type)) {
-                LanguageResolver resolver = (LanguageResolver)context.getInjector().newInstance(type);
+                LanguageResolver resolver = (LanguageResolver) context.getInjector().newInstance(type);
                 return resolver.resolveLanguage(name, context);
             } else {
                 throw new IllegalArgumentException("Resolving language: " + name + " detected
type conflict: Not a LanguageResolver implementation. Found: " + type.getName());
@@ -102,14 +115,14 @@ public class DefaultLanguageResolver implements LanguageResolver {
         }
         throw new NoSuchLanguageException(name);
     }
-    
+
     protected Class<?> findLanguage(String name, CamelContext context) throws Exception
{
         if (languageFactory == null) {
             languageFactory = context.getFactoryFinder(LANGUAGE_RESOURCE_PATH);
         }
         return languageFactory.findClass(name);
     }
-    
+
     protected Class<?> findLanguageResolver(String name, CamelContext context) throws
Exception {
         if (languageResolver == null) {
             languageResolver = context.getFactoryFinder(LANGUAGE_RESOLVER_RESOURCE_PATH);

http://git-wip-us.apache.org/repos/asf/camel/blob/ded7e742/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelContextResolverTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelContextResolverTest.java
b/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelContextResolverTest.java
index fb11a19..2284d9c 100644
--- a/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelContextResolverTest.java
+++ b/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelContextResolverTest.java
@@ -24,9 +24,13 @@ import org.apache.camel.CamelContext;
 import org.apache.camel.Component;
 import org.apache.camel.Endpoint;
 import org.apache.camel.Exchange;
+import org.apache.camel.Expression;
+import org.apache.camel.NoSuchLanguageException;
+import org.apache.camel.Predicate;
 import org.apache.camel.spi.DataFormat;
-import org.junit.After;
-import org.junit.Before;
+import org.apache.camel.spi.Language;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
 import org.junit.Test;
 
 import static org.junit.Assert.assertFalse;
@@ -40,10 +44,10 @@ import static org.junit.Assert.assertTrue;
  */
 public class DefaultCamelContextResolverTest {
 
-    private CamelContext context;
+    private static CamelContext context;
 
-    @Before
-    public void createContext() throws Exception {
+    @BeforeClass
+    public static void createContext() throws Exception {
         SimpleRegistry registry = new SimpleRegistry();
         context = new DefaultCamelContext(registry);
 
@@ -53,6 +57,9 @@ public class DefaultCamelContextResolverTest {
         // Add a data format using its fallback name
         registry.put("green-dataformat", new SampleDataFormat(true));
 
+        // Add a language using its fallback name
+        registry.put("green-language", new SampleLanguage(true));
+
         // Add a component using both names
         registry.put("yellow", new SampleComponent(false));
         registry.put("yellow-component", new SampleComponent(true));
@@ -61,11 +68,15 @@ public class DefaultCamelContextResolverTest {
         registry.put("red", new SampleDataFormat(false));
         registry.put("red-dataformat", new SampleDataFormat(true));
 
+        // Add a language using both names
+        registry.put("blue", new SampleLanguage(false));
+        registry.put("blue-language", new SampleLanguage(true));
+
         context.start();
     }
 
-    @After
-    public void destroyContext() throws Exception {
+    @AfterClass
+    public static void destroyContext() throws Exception {
         context.stop();
         context = null;
     }
@@ -103,6 +114,22 @@ public class DefaultCamelContextResolverTest {
     }
 
     @Test
+    public void testLanguageWithFallbackNames() throws Exception {
+        Language language = context.resolveLanguage("green");
+        assertNotNull("Language not found in registry", language);
+        assertTrue("Wrong instance type of the Language", language instanceof SampleLanguage);
+        assertTrue("Here we should have the fallback Language", ((SampleLanguage) language).isFallback());
+    }
+
+    @Test
+    public void testLanguageWithBothNames() throws Exception {
+        Language language = context.resolveLanguage("blue");
+        assertNotNull("Language not found in registry", language);
+        assertTrue("Wrong instance type of the Language", language instanceof SampleLanguage);
+        assertFalse("Here we should NOT have the fallback Language", ((SampleLanguage) language).isFallback());
+    }
+
+    @Test
     public void testNullLookupComponent() throws Exception {
         Component component = context.getComponent("xxxxxxxxx");
         assertNull("Non-existent Component should be null", component);
@@ -114,6 +141,11 @@ public class DefaultCamelContextResolverTest {
         assertNull("Non-existent DataFormat should be null", dataFormat);
     }
 
+    @Test(expected = NoSuchLanguageException.class)
+    public void testNullLookupLanguage() throws Exception {
+        context.resolveLanguage("xxxxxxxxx");
+    }
+
     public static class SampleComponent extends DefaultComponent {
 
         private boolean fallback;
@@ -163,4 +195,32 @@ public class DefaultCamelContextResolverTest {
         }
     }
 
+    public static class SampleLanguage implements Language {
+
+        private boolean fallback;
+
+        public SampleLanguage(boolean fallback) {
+            this.fallback = fallback;
+        }
+
+        @Override
+        public Predicate createPredicate(String expression) {
+            throw new UnsupportedOperationException("Should not be called");
+        }
+
+        @Override
+        public Expression createExpression(String expression) {
+            throw new UnsupportedOperationException("Should not be called");
+        }
+
+        public boolean isFallback() {
+            return fallback;
+        }
+
+        public void setFallback(boolean fallback) {
+            this.fallback = fallback;
+        }
+    }
+
+
 }


Mime
View raw message