Repository: camel Updated Branches: refs/heads/master 814b6f713 -> 3dc47cc6c
CAMEL-10234: Improve OSGi camel-jcache support Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/3dc47cc6 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/3dc47cc6 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/3dc47cc6 Branch: refs/heads/master Commit: 3dc47cc6c91d9b3ecf4c83fe1d08fe46464e859d Parents: 814b6f7 Author: lburgazzoli <lburgazz...@gmail.com> Authored: Wed Aug 17 15:46:40 2016 +0200 Committer: lburgazzoli <lburgazz...@gmail.com> Committed: Wed Aug 17 15:46:40 2016 +0200 ---------------------------------------------------------------------- .../camel/component/jcache/JCacheHelper.java | 38 ++++++++++++++------ 1 file changed, 28 insertions(+), 10 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/3dc47cc6/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/JCacheHelper.java ---------------------------------------------------------------------- diff --git a/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/JCacheHelper.java b/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/JCacheHelper.java index bf591a7..48b3e7e 100644 --- a/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/JCacheHelper.java +++ b/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/JCacheHelper.java @@ -28,18 +28,18 @@ public final class JCacheHelper { } public static <K, V> JCacheManager<K, V> createManager(JCacheConfiguration configuration) { - try { - // Check if we are in an osgi container - Class.forName("org.osgi.framework.FrameworkUtil"); - Class<?> type = Class.forName("org.apache.camel.component.jcache.osgi.OSGiCacheManager"); - Constructor<?> ctor = type.getConstructor(JCacheConfiguration.class); + if (isOSGi()) { + try { + Class<?> type = Class.forName("org.apache.camel.component.jcache.osgi.OSGiCacheManager"); + Constructor<?> ctor = type.getConstructor(JCacheConfiguration.class); - return (JCacheManager<K, V>)ctor.newInstance(configuration); - } catch (ClassNotFoundException e) { - return new JCacheManager<>(configuration); - } catch (Exception e) { - throw new RuntimeCamelException(e); + return (JCacheManager<K, V>)ctor.newInstance(configuration); + } catch (Exception e) { + throw new RuntimeCamelException(e); + } } + + return new JCacheManager<>(configuration); } @SuppressWarnings("uncheked") @@ -60,4 +60,22 @@ public final class JCacheHelper { } ); } + + public static boolean isOSGi() { + try { + // Check if we are in an osgi container + Class<?> fu = Class.forName("org.osgi.framework.FrameworkUtil"); + if (fu != null) { + Method method = fu.getMethod("getBundle", Class.class); + if (method != null) { + return method.invoke(null, JCacheHelper.class) != null; + } + } + } catch (ClassNotFoundException e) { + } catch (Exception e) { + throw new RuntimeCamelException(e); + } + + return false; + } }