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 <hekon...@gmail.com> Authored: Thu Feb 20 15:39:01 2014 +0100 Committer: Henryk Konsek <hekon...@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); } }