Hi Henryk,

I don’t think using TCCL by default can address the activatorClass loading 
issue.

Usually TCCL is set to be the application bundle class loader, if the 
application bundle doesn’t import the org.apache.camel.osgi package, the 
PlatformHelper.isInOsgiEnvironment() will return false even the application 
bundle is in OSGi environment.

Class.forName uses the caller class loader to load the class, it should be OK 
if the caller class and Activator are in the same OSGi bundle.

My suggestion is update the PlatforHelper.isInOsgiEnvironment() to load the 
Activator(org.apache.camel.impl.osgi.Activator) class with the class loader of 
PlatforHelper and leave the SpringNameSpaceHandler as the way it used to be.

--  
Willem Jiang

Red Hat, Inc.
Web: http://www.redhat.com
Blog: http://willemjiang.blogspot.com(http://willemjiang.blogspot.com/) 
(English)
http://jnn.iteye.com(http://jnn.javaeye.com/) (Chinese)
Twitter: willemjiang  
Weibo: 姜宁willem



On February 19, 2014 at 5:16:31 AM, hekon...@apache.org (hekon...@apache.org) 
wrote:
>  
> Repository: camel
> Updated Branches:
> refs/heads/master e10f04b50 -> 8b9d4c625
>  
>  
> [CAMEL-7218] Added possibility to explictly specify class  
> loader for OSGi checks.
>  
>  
> Project: http://git-wip-us.apache.org/repos/asf/camel/repo  
> Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/8b9d4c62  
> Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/8b9d4c62  
> Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/8b9d4c62  
>  
> Branch: refs/heads/master
> Commit: 8b9d4c625202455309a84c8ca492177ea28b2327
> Parents: e10f04b
> Author: Henryk Konsek  
> Authored: Tue Feb 18 22:15:20 2014 +0100
> Committer: Henryk Konsek  
> Committed: Tue Feb 18 22:16:13 2014 +0100
>  
> ----------------------------------------------------------------------  
> .../src/main/java/org/apache/camel/util/PlatformHelper.java  
> | 9 +++++++--
> .../apache/camel/spring/handler/CamelNamespaceHandler.java  
> | 2 +-
> 2 files changed, 8 insertions(+), 3 deletions(-)
> ----------------------------------------------------------------------  
>  
>  
> http://git-wip-us.apache.org/repos/asf/camel/blob/8b9d4c62/camel-core/src/main/java/org/apache/camel/util/PlatformHelper.java
>   
> ----------------------------------------------------------------------  
> diff --git 
> a/camel-core/src/main/java/org/apache/camel/util/PlatformHelper.java  
> b/camel-core/src/main/java/org/apache/camel/util/PlatformHelper.java  
> index 438af11..258248c 100644
> --- a/camel-core/src/main/java/org/apache/camel/util/PlatformHelper.java  
> +++ b/camel-core/src/main/java/org/apache/camel/util/PlatformHelper.java  
> @@ -37,13 +37,14 @@ public final class PlatformHelper {
> * Determine whether Camel is running in the OSGi environment.  
> Current implementation tries to load Camel activator
> * bundle (using reflection API and class loading) to determine  
> if the code is executed in the OSGi environment.
> *
> + * @param classLoader caller class loader to be used to load Camel  
> Bundle Activator
> * @return true if caller is running in the OSGi environment, false  
> otherwise
> */
> - public static boolean isInOsgiEnvironment() {
> + public static boolean isInOsgiEnvironment(ClassLoader  
> classLoader) {
> try {
> // Try to load the BundleActivator first
> Class.forName("org.osgi.framework.BundleActivator");  
> - Class activatorClass = 
> currentThread().getContextClassLoader().loadClass("org.apache.camel.osgi.Activator");
>   
> + Class activatorClass = 
> classLoader.loadClass("org.apache.camel.osgi.Activator");  
> Method getBundleMethod = activatorClass.getDeclaredMethod("getBundle");  
> Object bundle = getBundleMethod.invoke(null);
> return bundle != null;
> @@ -53,4 +54,8 @@ public final class PlatformHelper {
> }
> }
>  
> + public static boolean isInOsgiEnvironment() {
> + return isInOsgiEnvironment(currentThread().getContextClassLoader());  
> + }
> +
> }
>  
> http://git-wip-us.apache.org/repos/asf/camel/blob/8b9d4c62/components/camel-spring/src/main/java/org/apache/camel/spring/handler/CamelNamespaceHandler.java
>   
> ----------------------------------------------------------------------  
> diff --git 
> a/components/camel-spring/src/main/java/org/apache/camel/spring/handler/CamelNamespaceHandler.java
>   
> b/components/camel-spring/src/main/java/org/apache/camel/spring/handler/CamelNamespaceHandler.java
>   
> index 9a035c8..6535e69 100644
> --- 
> a/components/camel-spring/src/main/java/org/apache/camel/spring/handler/CamelNamespaceHandler.java
>   
> +++ 
> b/components/camel-spring/src/main/java/org/apache/camel/spring/handler/CamelNamespaceHandler.java
>   
> @@ -128,7 +128,7 @@ public class CamelNamespaceHandler extends  
> NamespaceHandlerSupport {
> Class cl = CamelContextFactoryBean.class;
> // These code will try to detected if we are in the OSGi environment.  
> // If so, camel will use the OSGi version of CamelContextFactoryBean  
> to create the CamelContext.
> - if (PlatformHelper.isInOsgiEnvironment()) {
> + if (PlatformHelper.isInOsgiEnvironment(getClass().getClassLoader()))  
> {
> try {
> cl = Class.forName("org.apache.camel.osgi.CamelContextFactoryBean");  
> LOG.info("OSGi environment detected.");
>  
>  

Reply via email to