camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ningji...@apache.org
Subject svn commit: r752764 - in /camel/trunk: camel-core/src/main/java/org/apache/camel/impl/ components/camel-osgi/src/main/java/org/apache/camel/osgi/ components/camel-osgi/src/test/java/org/apache/camel/osgi/
Date Thu, 12 Mar 2009 03:55:39 GMT
Author: ningjiang
Date: Thu Mar 12 03:55:38 2009
New Revision: 752764

URL: http://svn.apache.org/viewvc?rev=752764&view=rev
Log:
CAMEL-1425 a little refactoring of the LanaguageResolver

Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultLanguageResolver.java
    camel/trunk/components/camel-osgi/src/main/java/org/apache/camel/osgi/OsgiLanguageResolver.java
    camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/OsgiLanguageResolverTest.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultLanguageResolver.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultLanguageResolver.java?rev=752764&r1=752763&r2=752764&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultLanguageResolver.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultLanguageResolver.java
Thu Mar 12 03:55:38 2009
@@ -16,12 +16,16 @@
  */
 package org.apache.camel.impl;
 
+import java.io.IOException;
+
 import org.apache.camel.CamelContext;
 import org.apache.camel.NoFactoryAvailableException;
 import org.apache.camel.NoSuchLanguageException;
 import org.apache.camel.spi.Language;
 import org.apache.camel.spi.LanguageResolver;
 import org.apache.camel.util.FactoryFinder;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 
 /**
  * Default language resolver that looks for language factories in <b>META-INF/services/org/apache/camel/language/</b>
and
@@ -29,15 +33,31 @@
  *
  * @version $Revision$
  */
-public class DefaultLanguageResolver implements LanguageResolver {
+public class DefaultLanguageResolver implements LanguageResolver {    
     protected static final FactoryFinder LANGUAGE_FACTORY = new FactoryFinder("META-INF/services/org/apache/camel/language/");
     protected static final FactoryFinder LANGUAGE_RESOLVER = new FactoryFinder("META-INF/services/org/apache/camel/language/resolver/");
-
+    private static final transient Log LOG = LogFactory.getLog(DefaultLanguageResolver.class);
+    
     @SuppressWarnings("unchecked")
     public Language resolveLanguage(String name, CamelContext context) {
+        Object bean = null;
+        try {
+            bean = context.getRegistry().lookup(name);
+            if (bean != null && LOG.isDebugEnabled()) {
+                LOG.debug("Found language: " + name + " in registry: " + bean);
+            }
+        } catch (Exception e) {
+            LOG.debug("Ignored error looking up bean: " + name + ". Error: " + e);
+        }
+        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
+        }
         Class type = null;
         try {
-            type = LANGUAGE_FACTORY.findClass(name);
+            type = findLanguage(name);
         } catch (NoFactoryAvailableException e) {
             // ignore
         } catch (Exception e) {
@@ -57,7 +77,7 @@
     protected Language noSpecificLanguageFound(String name, CamelContext context) {
         Class type = null;
         try {
-            type = LANGUAGE_RESOLVER.findClass("default");
+            type = findLanguageResolver("default");
         } catch (NoFactoryAvailableException e) {
             // ignore
         } catch (Exception e) {
@@ -73,4 +93,12 @@
         }
         throw new NoSuchLanguageException(name);
     }
+    
+    protected Class findLanguage(String name) throws Exception {
+        return LANGUAGE_FACTORY.findClass(name);
+    }
+    
+    protected Class findLanguageResolver(String name) throws Exception {
+        return LANGUAGE_RESOLVER.findClass("default");
+    }
 }

Modified: camel/trunk/components/camel-osgi/src/main/java/org/apache/camel/osgi/OsgiLanguageResolver.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-osgi/src/main/java/org/apache/camel/osgi/OsgiLanguageResolver.java?rev=752764&r1=752763&r2=752764&view=diff
==============================================================================
--- camel/trunk/components/camel-osgi/src/main/java/org/apache/camel/osgi/OsgiLanguageResolver.java
(original)
+++ camel/trunk/components/camel-osgi/src/main/java/org/apache/camel/osgi/OsgiLanguageResolver.java
Thu Mar 12 03:55:38 2009
@@ -20,76 +20,21 @@
 import org.apache.camel.Component;
 import org.apache.camel.NoFactoryAvailableException;
 import org.apache.camel.NoSuchLanguageException;
+import org.apache.camel.impl.DefaultLanguageResolver;
 import org.apache.camel.spi.Language;
 import org.apache.camel.spi.LanguageResolver;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
-public class OsgiLanguageResolver implements LanguageResolver {
+public class OsgiLanguageResolver extends DefaultLanguageResolver {
     private static final transient Log LOG = LogFactory.getLog(OsgiLanguageResolver.class);
-
-    public Language resolveLanguage(String name, CamelContext context) {
-        Object bean = null;
-        try {
-            bean = context.getRegistry().lookup(name);
-            if (bean != null && LOG.isDebugEnabled()) {
-                LOG.debug("Found language: " + name + " in registry: " + bean);
-            }
-        } catch (Exception e) {
-            LOG.debug("Ignored error looking up bean: " + name + ". Error: " + e);
-        }
-        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
OSGI bundle
-        }
-        // Check in OSGi bundles        
-        Class type = null;
-        try {
-            type = getLanaguage(name);
-        } catch (Throwable e) {
-            throw new IllegalArgumentException("Invalid URI, no Language registered for scheme
: " + name, e);
-        }
-        if (type != null) {
-            if (Language.class.isAssignableFrom(type)) {
-                return (Language)context.getInjector().newInstance(type);
-            } else {
-                throw new IllegalArgumentException("Type is not a Lanaguage implementation.
Found: "
-                                                   + type.getName());
-            }
-        } else {
-            return noSpecificLanguageFound(name, context);
-        }
-    }
-    
-    protected Language noSpecificLanguageFound(String name, CamelContext context) {
-        Class type = null;
-        try {
-            type = getLanaguageResolver("default");
-        } catch (NoFactoryAvailableException e) {
-            // ignore
-        } catch (Throwable e) {
-            throw new IllegalArgumentException("Invalid URI, no Language registered for scheme:
" + name, e);
-        }
-        if (type != null) {
-            if (LanguageResolver.class.isAssignableFrom(type)) {
-                LanguageResolver resolver = (LanguageResolver)context.getInjector().newInstance(type);
-                return resolver.resolveLanguage(name, context);
-            } else {
-                throw new IllegalArgumentException("Type is not a LanguageResolver implementation.
Found: " + type.getName());
-            }
-        }
-        throw new NoSuchLanguageException(name);
-    }
-
     
-
-    protected Class getLanaguage(String name) throws Exception {
+    @Override
+    protected Class findLanguage(String name) throws Exception {
         return Activator.getLanguage(name);
     }
     
-    protected Class getLanaguageResolver(String name) throws Exception {
+    protected Class findLanaguageResolver(String name) throws Exception {
         return Activator.getLanguageResolver(name);
     }
 

Modified: camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/OsgiLanguageResolverTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/OsgiLanguageResolverTest.java?rev=752764&r1=752763&r2=752764&view=diff
==============================================================================
--- camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/OsgiLanguageResolverTest.java
(original)
+++ camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/OsgiLanguageResolverTest.java
Thu Mar 12 03:55:38 2009
@@ -28,7 +28,7 @@
         BundleContext context = getActivator().getBundle().getBundleContext();        
         CamelContext camelContext = new DefaultCamelContext();
         OsgiLanguageResolver resolver = new OsgiLanguageResolver();
-        Language language = resolver.noSpecificLanguageFound("js", camelContext);
+        Language language = resolver.resolveLanguage("js", camelContext);
         assertNotNull("We should find js language", language);
     }
 }



Mime
View raw message