You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@camel.apache.org by Claus Ibsen <cl...@gmail.com> on 2014/02/20 15:50:24 UTC

Re: git commit: [CAMEL-7218] Rely on OSGi FrameworkUtil instead of classloading magic.

-1 to this

We cannot have osgi imports in the camel-core source code.
This is ONLY allowed in that special osgi sub package.


On Thu, Feb 20, 2014 at 3:39 PM,  <he...@apache.org> wrote:
> Repository: camel
> Updated Branches:
>   refs/heads/master 9beec7470 -> a42a2ca95
>
>
> [CAMEL-7218] Rely on OSGi FrameworkUtil instead of classloading magic.
>
>
> Project: http://git-wip-us.apache.org/repos/asf/camel/repo
> Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/a42a2ca9
> Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/a42a2ca9
> Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/a42a2ca9
>
> Branch: refs/heads/master
> Commit: a42a2ca9542bfb83cf010168aeb8fd82f7b197bf
> Parents: 9beec74
> Author: Henryk Konsek <he...@gmail.com>
> Authored: Thu Feb 20 15:39:01 2014 +0100
> Committer: Henryk Konsek <he...@gmail.com>
> Committed: Thu Feb 20 15:39:01 2014 +0100
>
> ----------------------------------------------------------------------
>  .../org/apache/camel/util/PlatformHelper.java   | 33 +++++++++-----------
>  1 file changed, 15 insertions(+), 18 deletions(-)
> ----------------------------------------------------------------------
>
>
> http://git-wip-us.apache.org/repos/asf/camel/blob/a42a2ca9/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 f7c40f5..1b47956 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
> @@ -16,13 +16,12 @@
>   */
>  package org.apache.camel.util;
>
> -import java.lang.reflect.Method;
> -
> -import static java.lang.Thread.currentThread;
> -
> +import org.osgi.framework.Bundle;
>  import org.slf4j.Logger;
>  import org.slf4j.LoggerFactory;
>
> +import static org.osgi.framework.FrameworkUtil.getBundle;
> +
>  /**
>   * Utility dedicated for resolving runtime information related to the platform on which Camel is currently running.
>   */
> @@ -34,28 +33,26 @@ 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.
> +     * Determine whether Camel is running in the OSGi environment.
>       *
> -     * @param classLoader caller class loader to be used to load Camel Bundle Activator
> +     * @param classFromBundle class to be tested against being deployed into OSGi
>       * @return true if caller is running in the OSGi environment, false otherwise
>       */
> -    public static boolean isInOsgiEnvironment(ClassLoader classLoader) {
> -        try {
> -            // Try to load the BundleActivator first
> -            Class.forName("org.osgi.framework.BundleActivator");
> -            Class<?> activatorClass = classLoader.loadClass("org.apache.camel.impl.osgi.Activator");
> -            Method getBundleMethod = activatorClass.getDeclaredMethod("getBundle");
> -            Object bundle = getBundleMethod.invoke(null);
> -            return bundle != null;
> -        } catch (Throwable t) {
> -            LOG.trace("Cannot find class so assuming not running in OSGi container: " + t.getMessage());
> +    public static boolean isInOsgiEnvironment(Class classFromBundle) {
> +        Bundle bundle = getBundle(classFromBundle);
> +        if (bundle != null) {
> +            LOG.trace("Found OSGi bundle {} for class {} so assuming running in the OSGi container.",
> +                    bundle.getSymbolicName(), classFromBundle.getSimpleName());
> +            return true;
> +        } else {
> +            LOG.trace("Cannot find OSGi bundle for class {} so assuming not running in the OSGi container.",
> +                    classFromBundle.getSimpleName());
>              return false;
>          }
>      }
>
>      public static boolean isInOsgiEnvironment() {
> -        return isInOsgiEnvironment(PlatformHelper.class.getClassLoader());
> +        return isInOsgiEnvironment(PlatformHelper.class);
>      }
>
>  }
>



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
Email: cibsen@redhat.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen
Make your Camel applications look hawt, try: http://hawt.io

Re: git commit: [CAMEL-7218] Rely on OSGi FrameworkUtil instead of classloading magic.

Posted by Henryk Konsek <he...@gmail.com>.
> Oh damn, I thought we just import OSGi API as a dependency :( . I'll
> roll back to previous version and think what we can do without OSGi
> API.

I decided to switch back [1] to the very first version of the OSGi
detection method - analyzing the name of the Camel Context. It is
primitive and simplified however it will work for 99% of the user
cases. In the meantime I'll try to figure out the best way to detect
if code is deployed OSGi (without relying on the OSGi API).

Cheers.

[1] c34177d71974141076ab464843225fcd7b90a0a7

-- 
Henryk Konsek
http://henryk-konsek.blogspot.com

Re: git commit: [CAMEL-7218] Rely on OSGi FrameworkUtil instead of classloading magic.

Posted by Henryk Konsek <he...@gmail.com>.
> Yes, otherwise non OSGI ppl have class not found exception when
> org.apache.camel.util is loaded.
> So it must be in that sub package if you use osgi imported coded.

Oh damn, I thought we just import OSGi API as a dependency :( . I'll
roll back to previous version and think what we can do without OSGi
API.

Cheers.

-- 
Henryk Konsek
http://henryk-konsek.blogspot.com

Re: git commit: [CAMEL-7218] Rely on OSGi FrameworkUtil instead of classloading magic.

Posted by Claus Ibsen <cl...@gmail.com>.
On Thu, Feb 20, 2014 at 3:57 PM, Henryk Konsek <he...@gmail.com> wrote:
>> -1 to this
>>
>> We cannot have osgi imports in the camel-core source code.
>> This is ONLY allowed in that special osgi sub package.
>
> You mean I can rely on OSGi API only in org.apache.camel.impl.osgi?
>

Yes, otherwise non OSGI ppl have class not found exception when
org.apache.camel.util is loaded.
So it must be in that sub package if you use osgi imported coded.


> --
> Henryk Konsek
> http://henryk-konsek.blogspot.com



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
Email: cibsen@redhat.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen
Make your Camel applications look hawt, try: http://hawt.io

Re: git commit: [CAMEL-7218] Rely on OSGi FrameworkUtil instead of classloading magic.

Posted by Henryk Konsek <he...@gmail.com>.
> -1 to this
>
> We cannot have osgi imports in the camel-core source code.
> This is ONLY allowed in that special osgi sub package.

You mean I can rely on OSGi API only in org.apache.camel.impl.osgi?

-- 
Henryk Konsek
http://henryk-konsek.blogspot.com