You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ni...@apache.org on 2011/08/26 14:25:01 UTC

svn commit: r1162083 - in /camel/trunk: camel-core/src/main/java/org/apache/camel/component/file/ camel-core/src/main/java/org/apache/camel/impl/ camel-core/src/main/java/org/apache/camel/spi/ components/camel-core-osgi/src/main/java/org/apache/camel/c...

Author: ningjiang
Date: Fri Aug 26 12:25:01 2011
New Revision: 1162083

URL: http://svn.apache.org/viewvc?rev=1162083&view=rev
Log:
CAMEL-4380 FactoryFinder should support to find the class with a right version

Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultFactoryFinder.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/spi/FactoryFinder.java
    camel/trunk/components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/OsgiFactoryFinder.java
    camel/trunk/components/camel-juel/src/main/java/org/apache/camel/language/juel/JuelExpression.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java?rev=1162083&r1=1162082&r2=1162083&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java Fri Aug 26 12:25:01 2011
@@ -181,7 +181,7 @@ public abstract class GenericFileEndpoin
         try {
             FactoryFinder finder = getCamelContext().getFactoryFinder("META-INF/services/org/apache/camel/component/");
             log.trace("Using FactoryFinder: {}", finder);
-            factory = finder.findClass(getScheme(), "strategy.factory.");
+            factory = finder.findClass(getScheme(), "strategy.factory.", CamelContext.class);
         } catch (ClassNotFoundException e) {
             log.trace("'strategy.factory.class' not found", e);
         } catch (IOException e) {

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultFactoryFinder.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultFactoryFinder.java?rev=1162083&r1=1162082&r2=1162083&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultFactoryFinder.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultFactoryFinder.java Fri Aug 26 12:25:01 2011
@@ -82,6 +82,11 @@ public class DefaultFactoryFinder implem
         return clazz;
     }
 
+    public Class<?> findClass(String key, String propertyPrefix, Class<?> clazz) throws ClassNotFoundException, IOException {
+        // Just ignore clazz which is only useful for OSGiFactoryFinder
+        return findClass(key, propertyPrefix);
+    }
+
     private Object newInstance(String key, String propertyPrefix) throws IllegalAccessException,
         InstantiationException, IOException, ClassNotFoundException {
         Class<?> clazz = findClass(key, propertyPrefix);

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/spi/FactoryFinder.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/spi/FactoryFinder.java?rev=1162083&r1=1162082&r2=1162083&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/spi/FactoryFinder.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/spi/FactoryFinder.java Fri Aug 26 12:25:01 2011
@@ -76,4 +76,16 @@ public interface FactoryFinder {
      * @throws java.io.IOException is thrown if loading the class or META-INF file not found
      */
     Class<?> findClass(String key, String propertyPrefix) throws ClassNotFoundException, IOException;
+
+    /**
+     * Finds the given factory class using the the key to lookup.
+     *
+     * @param key is the key to add to the path to find a text file containing the factory name
+     * @param propertyPrefix prefix on key
+     * @param clazz the class which is used for checking compatible
+     * @return the factory class
+     * @throws ClassNotFoundException is thrown if not found
+     * @throws java.io.IOException is thrown if loading the class or META-INF file not found
+     */
+    Class<?> findClass(String key, String propertyPrefix, Class<?> clazz) throws ClassNotFoundException, IOException;
 }

Modified: camel/trunk/components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/OsgiFactoryFinder.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/OsgiFactoryFinder.java?rev=1162083&r1=1162082&r2=1162083&view=diff
==============================================================================
--- camel/trunk/components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/OsgiFactoryFinder.java (original)
+++ camel/trunk/components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/OsgiFactoryFinder.java Fri Aug 26 12:25:01 2011
@@ -43,14 +43,14 @@ public class OsgiFactoryFinder extends D
     }
 
     @Override
-    public Class<?> findClass(String key, String propertyPrefix) throws ClassNotFoundException, IOException {
+    public Class<?> findClass(String key, String propertyPrefix, Class<?> checkClass) throws ClassNotFoundException, IOException {
         if (propertyPrefix == null) {
             propertyPrefix = "";
         }
 
         Class clazz = classMap.get(propertyPrefix + key);
         if (clazz == null) {
-            BundleEntry entry = getResource(key);
+            BundleEntry entry = getResource(key, checkClass);
             if (entry != null) {
                 URL url = entry.url;
                 InputStream in = url.openStream();
@@ -77,11 +77,21 @@ public class OsgiFactoryFinder extends D
 
         return clazz;
     }
-    
+
+    @Override
+    public Class<?> findClass(String key, String propertyPrefix) throws ClassNotFoundException, IOException {
+        return findClass(key, propertyPrefix, null);
+    }
+
     // As the META-INF of the Factory could not be export,
     // we need to go through the bundles to look for it
     // NOTE, the first found factory will be return
     public BundleEntry getResource(String name) {
+        return getResource(name, null);
+    }
+
+    // The clazz can make sure we get right version of class that we need
+    public BundleEntry getResource(String name, Class<?> clazz) {
         BundleEntry entry = null;
         Bundle[] bundles = null; 
         
@@ -90,7 +100,7 @@ public class OsgiFactoryFinder extends D
         URL url;
         for (Bundle bundle : bundles) {
             url = bundle.getEntry(getResourcePath() + name);
-            if (url != null) {
+            if (url != null && checkCompat(bundle, clazz)) {
                 entry = new BundleEntry();
                 entry.url = url;
                 entry.bundle = bundle;
@@ -101,4 +111,19 @@ public class OsgiFactoryFinder extends D
         return entry;
     }
 
+    private boolean checkCompat(Bundle bundle, Class clazz) {
+        if (clazz == null) {
+            return true;
+        }
+        // Check bundle compatibility
+        try {
+            if (bundle.loadClass(clazz.getName()) != clazz) {
+                return false;
+            }
+        } catch (Throwable t) {
+            return false;
+        }
+        return true;
+    }
+
 }

Modified: camel/trunk/components/camel-juel/src/main/java/org/apache/camel/language/juel/JuelExpression.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-juel/src/main/java/org/apache/camel/language/juel/JuelExpression.java?rev=1162083&r1=1162082&r2=1162083&view=diff
==============================================================================
--- camel/trunk/components/camel-juel/src/main/java/org/apache/camel/language/juel/JuelExpression.java (original)
+++ camel/trunk/components/camel-juel/src/main/java/org/apache/camel/language/juel/JuelExpression.java Fri Aug 26 12:25:01 2011
@@ -17,7 +17,6 @@
 package org.apache.camel.language.juel;
 
 import java.io.IOException;
-import java.util.Properties;
 
 import javax.el.ArrayELResolver;
 import javax.el.CompositeELResolver;
@@ -78,7 +77,7 @@ public class JuelExpression extends Expr
         if (expressionFactory == null && context != null) {
             try {
                 FactoryFinder finder = context.getFactoryFinder("META-INF/services/org/apache/camel/language/");
-                Class<?> clazz = finder.findClass("el", "impl.");
+                Class<?> clazz = finder.findClass("el", "impl.", ExpressionFactory.class);
                 if (clazz != null) {
                     expressionFactory = (ExpressionFactory)clazz.newInstance();
                 }