svn commit: r1030359 - in /camel/trunk/components/camel-core-osgi/src: main/java/org/apache/camel/core/osgi/ test/java/org/apache/camel/core/osgi/ test/java/org/apache/camel/core/osgi/other/

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r1030359 - in /camel/trunk/components/camel-core-osgi/src: main/java/org/apache/camel/core/osgi/ test/java/org/apache/camel/core/osgi/ test/java/org/apache/camel/core/osgi/other/

ningjiang
Author: ningjiang
Date: Wed Nov  3 08:59:51 2010
New Revision: 1030359

URL: http://svn.apache.org/viewvc?rev=1030359&view=rev
Log:
CAMEL-3302 OsgiPackageScanClassResolver should have Non-OSGi classloader check fallback

Added:
    camel/trunk/components/camel-core-osgi/src/test/java/org/apache/camel/core/osgi/other/
    camel/trunk/components/camel-core-osgi/src/test/java/org/apache/camel/core/osgi/other/MyOtherRouteBuilder.java   (with props)
    camel/trunk/components/camel-core-osgi/src/test/java/org/apache/camel/core/osgi/other/MyOtherTypeConverter.java   (with props)
Modified:
    camel/trunk/components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/OsgiPackageScanClassResolver.java
    camel/trunk/components/camel-core-osgi/src/test/java/org/apache/camel/core/osgi/CamelMockBundle.java
    camel/trunk/components/camel-core-osgi/src/test/java/org/apache/camel/core/osgi/OsgiPackageScanClassResolverTest.java

Modified: camel/trunk/components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/OsgiPackageScanClassResolver.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/OsgiPackageScanClassResolver.java?rev=1030359&r1=1030358&r2=1030359&view=diff
==============================================================================
--- camel/trunk/components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/OsgiPackageScanClassResolver.java (original)
+++ camel/trunk/components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/OsgiPackageScanClassResolver.java Wed Nov  3 08:59:51 2010
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.core.osgi;
 
+import java.lang.reflect.Method;
 import java.net.URL;
 import java.util.Collections;
 import java.util.Enumeration;
@@ -40,13 +41,42 @@ public class OsgiPackageScanClassResolve
     }
 
     public Set<ClassLoader> getClassLoaders() {
-        // now we just use bundle classloader to load the class
-        return Collections.<ClassLoader>singleton(new BundleDelegatingClassLoader(bundle));
+        // Added the BundleDelegatingClassLoader to load the class
+        Set<ClassLoader> classLoaders = super.getClassLoaders();
+        ClassLoader osgiLoader = new BundleDelegatingClassLoader(bundle);
+        classLoaders.add(osgiLoader);
+        return classLoaders;
     }
     
     public void find(PackageScanFilter test, String packageName, Set<Class<?>> classes) {
         packageName = packageName.replace('.', '/');
+        int classesSize = classes.size();
         loadImplementationsInBundle(test, packageName, classes);
+        if (classes.size() == classesSize) {
+            // Using the non-OSGi classloaders as a fallback
+            // this is necessary when use JBI packaging for servicemix-camel SU
+            // so that we get chance to use SU classloader to scan packages in the SU
+            if (log.isTraceEnabled()) {
+                log.trace("Using only regular classloaders");
+            }
+            for (ClassLoader classLoader : super.getClassLoaders()) {
+                if (!isOsgiClassloader(classLoader)) {
+                    find(test, packageName, classLoader, classes);
+                }
+            }  
+        }
+    }
+    
+    private static boolean isOsgiClassloader(ClassLoader loader) {
+        try {
+            Method mth = loader.getClass().getMethod("getBundle", new Class[] {});
+            if (mth != null) {
+                return true;
+            }
+        } catch (NoSuchMethodException e) {
+            // ignore its not an osgi loader
+        }
+        return false;
     }
     
     private void loadImplementationsInBundle(PackageScanFilter test, String packageName, Set<Class<?>> classes) {      

Modified: camel/trunk/components/camel-core-osgi/src/test/java/org/apache/camel/core/osgi/CamelMockBundle.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-core-osgi/src/test/java/org/apache/camel/core/osgi/CamelMockBundle.java?rev=1030359&r1=1030358&r2=1030359&view=diff
==============================================================================
--- camel/trunk/components/camel-core-osgi/src/test/java/org/apache/camel/core/osgi/CamelMockBundle.java (original)
+++ camel/trunk/components/camel-core-osgi/src/test/java/org/apache/camel/core/osgi/CamelMockBundle.java Wed Nov  3 08:59:51 2010
@@ -55,7 +55,7 @@ public class CamelMockBundle extends Moc
         }
         
     }
-
+    
     public CamelMockBundle() {
         setClassLoader(getClass().getClassLoader());
     }
@@ -98,5 +98,23 @@ public class CamelMockBundle extends Moc
             return super.findEntries(path, filePattern, recurse);
         }
     }
+    
+    
+    
+    public Class loadClass(String name) throws ClassNotFoundException {
+        if (isLoadableClass(name)) {
+            return super.loadClass(name);
+        } else {
+            throw new ClassNotFoundException(name);
+        }
+    }
+
+    protected boolean isLoadableClass(String name) {
+        if (name.startsWith("org.apache.camel.core.osgi.other")) {
+            return false;
+        } else {
+            return true;
+        }
+    }
 
 }

Modified: camel/trunk/components/camel-core-osgi/src/test/java/org/apache/camel/core/osgi/OsgiPackageScanClassResolverTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-core-osgi/src/test/java/org/apache/camel/core/osgi/OsgiPackageScanClassResolverTest.java?rev=1030359&r1=1030358&r2=1030359&view=diff
==============================================================================
--- camel/trunk/components/camel-core-osgi/src/test/java/org/apache/camel/core/osgi/OsgiPackageScanClassResolverTest.java (original)
+++ camel/trunk/components/camel-core-osgi/src/test/java/org/apache/camel/core/osgi/OsgiPackageScanClassResolverTest.java Wed Nov  3 08:59:51 2010
@@ -21,6 +21,8 @@ import java.util.Set;
 
 import org.apache.camel.Converter;
 import org.apache.camel.RoutesBuilder;
+import org.apache.camel.core.osgi.other.MyOtherRouteBuilder;
+import org.apache.camel.core.osgi.other.MyOtherTypeConverter;
 import org.apache.camel.core.osgi.test.MyRouteBuilder;
 import org.apache.camel.core.osgi.test.MyTypeConverter;
 import org.junit.Test;
@@ -50,5 +52,28 @@ public class OsgiPackageScanClassResolve
         assertEquals("There should find a class", classes.size(), 1);
         assertTrue("Find a wrong class", classes.contains(MyRouteBuilder.class));
     }
+    
+    @Test
+    public void testOsgiResolverFindAnnotatedWithFallbackClassLoaderTest() throws IOException {
+        BundleContext context = getBundleContext();
+        assertNotNull("The BundleContext should not be null", context);
+        OsgiPackageScanClassResolver resolver  = new OsgiPackageScanClassResolver(context);
+            
+        String[] packageNames = {"org.apache.camel.core.osgi.other"};
+        Set<Class<?>> classes = resolver.findAnnotated(Converter.class, packageNames);
+        assertEquals("There should find a class", classes.size(), 1);
+        assertTrue("Find a wrong class", classes.contains(MyOtherTypeConverter.class));
+    }
+    
+    @Test
+    public void testOsgiResolverFindImplementationWithFallbackClassLoaderTest() {
+        BundleContext context = getBundleContext();
+        assertNotNull("The BundleContext should not be null", context);
+        OsgiPackageScanClassResolver resolver  = new OsgiPackageScanClassResolver(context);
+        String[] packageNames = {"org.apache.camel.core.osgi.other"};
+        Set<Class<?>> classes = resolver.findImplementations(RoutesBuilder.class, packageNames);
+        assertEquals("There should find a class", classes.size(), 1);
+        assertTrue("Find a wrong class", classes.contains(MyOtherRouteBuilder.class));
+    }
 
 }

Added: camel/trunk/components/camel-core-osgi/src/test/java/org/apache/camel/core/osgi/other/MyOtherRouteBuilder.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-core-osgi/src/test/java/org/apache/camel/core/osgi/other/MyOtherRouteBuilder.java?rev=1030359&view=auto
==============================================================================
--- camel/trunk/components/camel-core-osgi/src/test/java/org/apache/camel/core/osgi/other/MyOtherRouteBuilder.java (added)
+++ camel/trunk/components/camel-core-osgi/src/test/java/org/apache/camel/core/osgi/other/MyOtherRouteBuilder.java Wed Nov  3 08:59:51 2010
@@ -0,0 +1,28 @@
+/**
+ * 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.core.osgi.other;
+
+import org.apache.camel.builder.RouteBuilder;
+
+public class MyOtherRouteBuilder extends RouteBuilder {
+
+    @Override
+    public void configure() throws Exception {
+        // Do nothing here
+    }
+
+}

Propchange: camel/trunk/components/camel-core-osgi/src/test/java/org/apache/camel/core/osgi/other/MyOtherRouteBuilder.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-core-osgi/src/test/java/org/apache/camel/core/osgi/other/MyOtherRouteBuilder.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: camel/trunk/components/camel-core-osgi/src/test/java/org/apache/camel/core/osgi/other/MyOtherTypeConverter.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-core-osgi/src/test/java/org/apache/camel/core/osgi/other/MyOtherTypeConverter.java?rev=1030359&view=auto
==============================================================================
--- camel/trunk/components/camel-core-osgi/src/test/java/org/apache/camel/core/osgi/other/MyOtherTypeConverter.java (added)
+++ camel/trunk/components/camel-core-osgi/src/test/java/org/apache/camel/core/osgi/other/MyOtherTypeConverter.java Wed Nov  3 08:59:51 2010
@@ -0,0 +1,78 @@
+/**
+ * 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.core.osgi.other;
+
+import org.apache.camel.Converter;
+import org.apache.camel.Exchange;
+import org.apache.camel.FallbackConverter;
+import org.apache.camel.TypeConverter;
+import org.apache.camel.component.file.GenericFile;
+import org.apache.camel.spi.TypeConverterRegistry;
+
+@Converter
+public final class MyOtherTypeConverter {
+
+    /**
+     * Utility classes should not have a public constructor.
+     */
+    private MyOtherTypeConverter() {
+    }
+    
+    /**
+     * Converts the given value to a boolean, handling strings or Boolean
+     * objects; otherwise returning false if the value could not be converted to
+     * a boolean
+     */
+    @Converter
+    public static boolean toBool(Object value) {
+        Boolean answer = null;    
+        if (value instanceof String) {
+            answer = Boolean.valueOf((String)value);
+        }
+        if (value instanceof Boolean) {
+            answer = (Boolean) value;
+        }
+        if (answer != null) {
+            return answer.booleanValue();
+        }
+        return false;
+    }
+    
+    @FallbackConverter
+    public static Object convertTo(Class<?> type, Exchange exchange, Object value, TypeConverterRegistry registry) {
+        // use a fallback type converter so we can convert the embedded body if the value is GenericFile
+        if (GenericFile.class.isAssignableFrom(value.getClass())) {
+            GenericFile file = (GenericFile) value;
+            Class<?> from = file.getBody().getClass();
+
+            // maybe from is already the type we want
+            if (from.isAssignableFrom(type)) {
+                return file.getBody();
+            }
+            // no then try to lookup a type converter
+            TypeConverter tc = registry.lookup(type, from);
+            if (tc != null) {
+                Object body = file.getBody();
+                return tc.convertTo(type, exchange, body);
+            }
+        }
+        
+        return null;
+    }
+    
+
+}

Propchange: camel/trunk/components/camel-core-osgi/src/test/java/org/apache/camel/core/osgi/other/MyOtherTypeConverter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-core-osgi/src/test/java/org/apache/camel/core/osgi/other/MyOtherTypeConverter.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date