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 2009/01/30 08:47:39 UTC

svn commit: r739199 - in /camel/trunk/camel-core/src/main/java/org/apache/camel: impl/DefaultComponentResolver.java util/FactoryFinder.java util/ObjectHelper.java

Author: davsclaus
Date: Fri Jan 30 07:47:39 2009
New Revision: 739199

URL: http://svn.apache.org/viewvc?rev=739199&view=rev
Log:
CAMEL-1301: Fixed NPE in factory finder and improved ObjectHelper.loadClass

Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultComponentResolver.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/util/FactoryFinder.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultComponentResolver.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultComponentResolver.java?rev=739199&r1=739198&r2=739199&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultComponentResolver.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultComponentResolver.java Fri Jan 30 07:47:39 2009
@@ -61,12 +61,12 @@
         } catch (Throwable e) {
             throw new IllegalArgumentException("Invalid URI, no Component registered for scheme: " + name, e);
         }
-        if (LOG.isDebugEnabled()) {
-            LOG.debug("Found component: " + name + " via type: " + type.getName() + " via: " + COMPONENT_FACTORY.getPath() + name);
-        }
         if (type == null) {
             return null;
         }
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("Found component: " + name + " via type: " + type.getName() + " via: " + COMPONENT_FACTORY.getPath() + name);
+        }
         if (Component.class.isAssignableFrom(type)) {
             return (Component) context.getInjector().newInstance(type);
         } else {

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/util/FactoryFinder.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/FactoryFinder.java?rev=739199&r1=739198&r2=739199&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/util/FactoryFinder.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/util/FactoryFinder.java Fri Jan 30 07:47:39 2009
@@ -106,7 +106,9 @@
         Class clazz = classMap.get(propertyPrefix + key);
         if (clazz == null) {
             clazz = newInstance(doFindFactoryProperties(key), propertyPrefix);
-            classMap.put(propertyPrefix + key, clazz);
+            if (clazz != null) {
+                classMap.put(propertyPrefix + key, clazz);
+            }
         }
         return clazz;
     }

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java?rev=739199&r1=739198&r2=739199&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java Fri Jan 30 07:47:39 2009
@@ -41,7 +41,7 @@
 
 /**
  * A number of useful helper methods for working with Objects
- * 
+ *
  * @version $Revision$
  */
 public final class ObjectHelper {
@@ -260,7 +260,7 @@
     /**
      * Removes any starting characters on the given text which match the given
      * character
-     * 
+     *
      * @param text the string
      * @param ch the initial characters to remove
      * @return either the original string or the new substring
@@ -364,7 +364,7 @@
      * Returns the predicate matching boolean on a {@link List} result set where
      * if the first element is a boolean its value is used otherwise this method
      * returns true if the collection is not empty
-     * 
+     *
      * @return <tt>true</tt> if the first element is a boolean and its value
      *         is true or if the list is non empty
      */
@@ -385,7 +385,7 @@
     /**
      * A helper method to access a system property, catching any security
      * exceptions
-     * 
+     *
      * @param name the name of the system property required
      * @param defaultValue the default value to use if the property is not
      *                available or a security exception prevents access
@@ -407,7 +407,7 @@
     /**
      * A helper method to access a boolean system property, catching any
      * security exceptions
-     * 
+     *
      * @param name the name of the system property required
      * @param defaultValue the default value to use if the property is not
      *                available or a security exception prevents access
@@ -449,7 +449,7 @@
     /**
      * Attempts to load the given class name using the thread context class
      * loader or the class loader used to load this class
-     * 
+     *
      * @param name the name of the class to load
      * @return the class or null if it could not be loaded
      */
@@ -460,24 +460,51 @@
     /**
      * Attempts to load the given class name using the thread context class
      * loader or the given class loader
-     * 
+     *
      * @param name the name of the class to load
      * @param loader the class loader to use after the thread context class
      *                loader
      * @return the class or null if it could not be loaded
      */
     public static Class<?> loadClass(String name, ClassLoader loader) {
-        ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
-        if (contextClassLoader != null) {
-            try {
-                return contextClassLoader.loadClass(name);
-            } catch (ClassNotFoundException e) {
-                try {
-                    return loader.loadClass(name);
-                } catch (ClassNotFoundException e1) {
-                    LOG.debug("Could not find class: " + name + ". Reason: " + e);
-                }
+        // try context class loader first
+        Class clazz = doLoadClass(name, Thread.currentThread().getContextClassLoader());
+        if (clazz == null) {
+            // then the provided loader
+            clazz = doLoadClass(name, loader);
+        }
+        if (clazz == null) {
+            // and fallback to the loader the loaded the ObjectHelper class
+            clazz = doLoadClass(name, ObjectHelper.class.getClassLoader());
+        }
+
+        if (clazz == null) {
+            LOG.warn("Could not find class: " + name);
+        }
+
+        return clazz;
+    }
+
+    /**
+     * Loads the given class with the provided classloader (may be null).
+     * Will ignore any class not found and return null.
+     *
+     * @param name    the name of the class to load
+     * @param loader  a provided loader (may be null)
+     * @return the class, or null if it could not be loaded
+     */
+    private static Class<?> doLoadClass(String name, ClassLoader loader) {
+        ObjectHelper.notEmpty(name, "name");
+        if (loader == null) {
+            return null;
+        }
+        try {
+            return loader.loadClass(name);
+        } catch (ClassNotFoundException e) {
+            if (LOG.isTraceEnabled()) {
+                LOG.trace("Can not load class: " + name + " using classloader: " + loader, e);
             }
+
         }
         return null;
     }
@@ -485,7 +512,7 @@
     /**
      * Attempts to load the given resource as a stream using the thread context
      * class loader or the class loader used to load this class
-     * 
+     *
      * @param name the name of the resource to load
      * @return the stream or null if it could not be loaded
      */
@@ -506,7 +533,7 @@
     /**
      * A helper method to invoke a method via reflection and wrap any exceptions
      * as {@link RuntimeCamelException} instances
-     * 
+     *
      * @param method the method to invoke
      * @param instance the object instance (or null for static methods)
      * @param parameters the parameters to the method
@@ -524,7 +551,7 @@
 
     /**
      * Returns a list of methods which are annotated with the given annotation
-     * 
+     *
      * @param type the type to reflect on
      * @param annotationType the annotation type
      * @return a list of the methods found
@@ -536,7 +563,7 @@
 
     /**
      * Returns a list of methods which are annotated with the given annotation
-     * 
+     *
      * @param type the type to reflect on
      * @param annotationType the annotation type
      * @param checkMetaAnnotations check for meta annotations
@@ -560,7 +587,7 @@
 
     /**
      * Checks if a Class or Method are annotated with the given annotation
-     * 
+     *
      * @param elem the Class or Method to reflect on
      * @param annotationType the annotation type
      * @param checkMetaAnnotations check for meta annotations
@@ -585,7 +612,7 @@
 
     /**
      * Turns the given object arrays into a meaningful string
-     * 
+     *
      * @param objects an array of objects or null
      * @return a meaningful string
      */
@@ -682,7 +709,7 @@
     /**
      * Closes the given resource if it is available, logging any closing
      * exceptions to the given log
-     * 
+     *
      * @param closeable the object to close
      * @param name the name of the resource
      * @param log the log to use when reporting closure warnings
@@ -799,7 +826,7 @@
     /**
      * Wraps the caused exception in a {@link RuntimeCamelException} if its not
      * already such an exception.
-     * 
+     *
      * @param e the caused exception
      * @return the wrapper exception
      */