Repository: camel Updated Branches: refs/heads/master ca480add1 -> fc8a5ceba
[CAMEL-7218] Extracted OSGi-detection logic into utility class. Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/fc8a5ceb Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/fc8a5ceb Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/fc8a5ceb Branch: refs/heads/master Commit: fc8a5ceba8f82675ef8290bdc489eba73a836e40 Parents: ca480ad Author: Henryk Konsek <hekon...@gmail.com> Authored: Tue Feb 18 16:54:26 2014 +0100 Committer: Henryk Konsek <hekon...@gmail.com> Committed: Tue Feb 18 16:55:11 2014 +0100 ---------------------------------------------------------------------- .../org/apache/camel/util/PlatformHelper.java | 54 ++++++++++++++++++++ .../spring/handler/CamelNamespaceHandler.java | 24 ++++----- 2 files changed, 63 insertions(+), 15 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/fc8a5ceb/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 new file mode 100644 index 0000000..3fd0338 --- /dev/null +++ b/camel-core/src/main/java/org/apache/camel/util/PlatformHelper.java @@ -0,0 +1,54 @@ +/** + * 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.util; + +import java.lang.reflect.Method; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Utility dedicated for resolving runtime information related to the platform on which Camel is currently running. + */ +public final class PlatformHelper { + + private static final Logger LOG = LoggerFactory.getLogger(PlatformHelper.class); + + private 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. + * + * @return true if caller is running in the OSGi environment, false otherwise + */ + public static boolean isInOsgiEnvironment() { + try { + // Try to load the BundleActivator first + Class.forName("org.osgi.framework.BundleActivator"); + Class<?> activatorClass = Class.forName("org.apache.camel.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()); + return false; + } + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/fc8a5ceb/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 957c792..9a035c8 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 @@ -51,6 +51,7 @@ import org.apache.camel.spring.CamelThreadPoolFactoryBean; import org.apache.camel.spring.remoting.CamelProxyFactoryBean; import org.apache.camel.spring.remoting.CamelServiceExporter; import org.apache.camel.util.ObjectHelper; +import org.apache.camel.util.PlatformHelper; import org.apache.camel.util.spring.KeyStoreParametersFactoryBean; import org.apache.camel.util.spring.SSLContextParametersFactoryBean; import org.apache.camel.util.spring.SecureRandomParametersFactoryBean; @@ -124,27 +125,20 @@ public class CamelNamespaceHandler extends NamespaceHandlerSupport { parserMap.put("errorHandler", errorHandlerParser); // camel context - boolean osgi = false; 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. - try { - // Try to load the BundleActivator first - Class.forName("org.osgi.framework.BundleActivator"); - Class<?> c = Class.forName("org.apache.camel.osgi.Activator"); - Method mth = c.getDeclaredMethod("getBundle"); - Object bundle = mth.invoke(null); - if (bundle != null) { + if (PlatformHelper.isInOsgiEnvironment()) { + try { cl = Class.forName("org.apache.camel.osgi.CamelContextFactoryBean"); - osgi = true; + LOG.info("OSGi environment detected."); + } catch (ClassNotFoundException e) { + LOG.trace("Cannot find CamelContextFactoryBean class so assuming not running in OSGi container: " + e.getMessage()); } - } catch (Throwable t) { - // not running with camel-core-osgi so we fallback to the regular factory bean - LOG.trace("Cannot find class so assuming not running in OSGi container: " + t.getMessage()); + } else { + LOG.info("Non-OSGi environment detected."); } - if (osgi) { - LOG.info("OSGi environment detected."); - } + LOG.debug("Using {} as CamelContextBeanDefinitionParser", cl.getCanonicalName()); registerParser("camelContext", new CamelContextBeanDefinitionParser(cl)); }