CAMEL-16711: components - Move OSGi dependenct code to camel-karaf
diff --git a/components/camel-jcache-osgi/pom.xml b/components/camel-jcache-osgi/pom.xml
new file mode 100644
index 0000000..5353ad0
--- /dev/null
+++ b/components/camel-jcache-osgi/pom.xml
@@ -0,0 +1,66 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    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.
+
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+
+    <modelVersion>4.0.0</modelVersion>
+
+    <parent>
+        <groupId>org.apache.camel.karaf</groupId>
+        <artifactId>components</artifactId>
+        <version>3.11.0-SNAPSHOT</version>
+    </parent>
+
+    <artifactId>camel-jcache-osgi</artifactId>
+    <packaging>jar</packaging>
+    <name>Camel Karaf :: JCache</name>
+    <description>Camel JCache support</description>
+
+    <properties>
+        <camel.osgi.activator>
+            org.apache.camel.component.jcache.osgi.Activator
+        </camel.osgi.activator>
+        <camel.osgi.import>
+            !org.apache.chemistry.opencmis.client.runtime,*
+        </camel.osgi.import>
+
+        <firstVersion>2.17.0</firstVersion>
+        <label>cache,datagrid,clustering</label>
+        <title>JCache</title>
+    </properties>
+
+    <dependencies>
+
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-jcache</artifactId>
+        </dependency>
+
+        <!-- osgi support -->
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>osgi.core</artifactId>
+            <scope>provided</scope>
+            <optional>true</optional>
+            <version>6.0.0</version>
+        </dependency>
+
+    </dependencies>
+
+</project>
diff --git a/components/camel-jcache-osgi/src/generated/resources/META-INF/services/org/apache/camel/other.properties b/components/camel-jcache-osgi/src/generated/resources/META-INF/services/org/apache/camel/other.properties
new file mode 100644
index 0000000..bf0422d
--- /dev/null
+++ b/components/camel-jcache-osgi/src/generated/resources/META-INF/services/org/apache/camel/other.properties
@@ -0,0 +1,7 @@
+# Generated by camel build tools - do NOT edit this file!
+name=jcache-osgi
+groupId=org.apache.camel.karaf
+artifactId=camel-jcache-osgi
+version=3.11.0-SNAPSHOT
+projectName=Camel Karaf :: JCache
+projectDescription=Camel JCache support
diff --git a/components/camel-jcache-osgi/src/generated/resources/jcache-osgi.json b/components/camel-jcache-osgi/src/generated/resources/jcache-osgi.json
new file mode 100644
index 0000000..8df2253
--- /dev/null
+++ b/components/camel-jcache-osgi/src/generated/resources/jcache-osgi.json
@@ -0,0 +1,15 @@
+{
+  "other": {
+    "kind": "other",
+    "name": "jcache-osgi",
+    "title": "JCache",
+    "description": "Camel JCache support",
+    "deprecated": false,
+    "firstVersion": "2.17.0",
+    "label": "cache,datagrid,clustering",
+    "supportLevel": "Stable",
+    "groupId": "org.apache.camel.karaf",
+    "artifactId": "camel-jcache-osgi",
+    "version": "3.11.0-SNAPSHOT"
+  }
+}
diff --git a/components/camel-jcache-osgi/src/main/java/org/apache/camel/component/jcache/osgi/Activator.java b/components/camel-jcache-osgi/src/main/java/org/apache/camel/component/jcache/osgi/Activator.java
new file mode 100644
index 0000000..09b78c5
--- /dev/null
+++ b/components/camel-jcache-osgi/src/main/java/org/apache/camel/component/jcache/osgi/Activator.java
@@ -0,0 +1,41 @@
+/*
+ * 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.component.jcache.osgi;
+
+import org.apache.camel.component.jcache.JCacheManagerFactory;
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceRegistration;
+
+public class Activator implements BundleActivator {
+    private ServiceRegistration<?> registration;
+
+    @Override
+    public void start(BundleContext context) throws Exception {
+        Object factory = new OSGiCacheManagerFactory();
+        registration = context.registerService(JCacheManagerFactory.class.getName(), factory, null);
+    }
+
+    @Override
+    public void stop(BundleContext context) throws Exception {
+        // release the reference
+        if (registration != null) {
+            context.ungetService(registration.getReference());
+        }
+    }
+
+}
diff --git a/components/camel-jcache-osgi/src/main/java/org/apache/camel/component/jcache/osgi/OSGiCacheManager.java b/components/camel-jcache-osgi/src/main/java/org/apache/camel/component/jcache/osgi/OSGiCacheManager.java
new file mode 100644
index 0000000..16e6359
--- /dev/null
+++ b/components/camel-jcache-osgi/src/main/java/org/apache/camel/component/jcache/osgi/OSGiCacheManager.java
@@ -0,0 +1,112 @@
+/*
+ * 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.component.jcache.osgi;
+
+import org.apache.camel.component.jcache.JCacheConfiguration;
+import org.apache.camel.component.jcache.JCacheHelper;
+import org.apache.camel.component.jcache.JCacheManager;
+import org.apache.camel.component.jcache.JCacheProvider;
+import org.apache.camel.component.jcache.JCacheProviders;
+import org.apache.camel.util.ObjectHelper;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.FrameworkUtil;
+import org.osgi.framework.wiring.BundleWiring;
+
+import javax.cache.Cache;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.URL;
+import java.util.Enumeration;
+
+public final class OSGiCacheManager<K, V> extends JCacheManager {
+    public OSGiCacheManager(JCacheConfiguration configuration) {
+        super(configuration);
+    }
+
+    @Override
+    public synchronized Cache<K, V> doGetCache(JCacheProvider provider) throws Exception {
+        final ClassLoader jcl = getClassLoader(provider.className());
+        final ClassLoader tccl = Thread.currentThread().getContextClassLoader();
+
+        try {
+            if (jcl != null) {
+                Thread.currentThread().setContextClassLoader(jcl);
+            }
+
+            Cache<K, V> cache = super.doGetCache(provider);
+            if (provider == JCacheProviders.hazelcast && jcl != null) {
+                cache = JCacheHelper.tcclProxy(cache, Cache.class, jcl);
+            }
+
+            return cache;
+        } finally {
+            Thread.currentThread().setContextClassLoader(tccl);
+        }
+    }
+
+    private ClassLoader getClassLoader(String providerName) throws Exception {
+        if (providerName == null || !getConfiguration().isLookupProviders()) {
+            return null;
+        }
+
+        final BundleContext bc = FrameworkUtil.getBundle(JCacheHelper.class).getBundleContext();
+        final ClassLoader bcl = bc.getBundle().adapt(BundleWiring.class).getClassLoader();
+        final ClassLoader acl = getConfiguration().getApplicationContextClassLoader();
+
+        for (final Bundle bundle : bc.getBundles()) {
+            URL spi = bundle.getResource("META-INF/services/javax.cache.spi.CachingProvider");
+            if (spi != null) {
+                try (BufferedReader in = new BufferedReader(new InputStreamReader(spi.openStream()))) {
+                    if (ObjectHelper.equal(providerName, in.readLine())) {
+                        return new ClassLoader(bcl) {
+                            @Override
+                            protected Class<?> findClass(String name) throws ClassNotFoundException {
+                                try {
+                                    return acl.loadClass(name);
+                                } catch (ClassNotFoundException e) {
+                                    return bundle.loadClass(name);
+                                }
+                            }
+
+                            @Override
+                            protected URL findResource(String name) {
+                                URL resource = acl.getResource(name);
+                                if (resource == null) {
+                                    resource = bundle.getResource(name);
+                                }
+                                return resource;
+                            }
+
+                            @Override
+                            protected Enumeration findResources(String name) throws IOException {
+                                try {
+                                    return acl.getResources(name);
+                                } catch (IOException e) {
+                                    return bundle.getResources(name);
+                                }
+                            }
+                        };
+                    }
+                }
+            }
+        }
+
+        return null;
+    }
+}
diff --git a/components/camel-jcache-osgi/src/main/java/org/apache/camel/component/jcache/osgi/OSGiCacheManagerFactory.java b/components/camel-jcache-osgi/src/main/java/org/apache/camel/component/jcache/osgi/OSGiCacheManagerFactory.java
new file mode 100644
index 0000000..7bbc85f
--- /dev/null
+++ b/components/camel-jcache-osgi/src/main/java/org/apache/camel/component/jcache/osgi/OSGiCacheManagerFactory.java
@@ -0,0 +1,31 @@
+/*
+ * 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.component.jcache.osgi;
+
+import org.apache.camel.component.jcache.JCacheConfiguration;
+import org.apache.camel.component.jcache.JCacheManager;
+import org.apache.camel.component.jcache.JCacheManagerFactory;
+
+public class OSGiCacheManagerFactory implements JCacheManagerFactory {
+
+    @Override
+    @SuppressWarnings("unchecked")
+    public <K, V> JCacheManager<K, V> createManager(JCacheConfiguration configuration) {
+        return new OSGiCacheManager<>(configuration);
+    }
+
+}
diff --git a/components/pom.xml b/components/pom.xml
index aa8f786..f06ad25 100644
--- a/components/pom.xml
+++ b/components/pom.xml
@@ -39,6 +39,7 @@
         <module>camel-cxf-blueprint</module>
         <module>camel-cxf-transport-blueprint</module>
         <module>camel-eventadmin</module>
+        <module>camel-jcache-osgi</module>
         <module>camel-kura</module>
         <module>camel-osgi-activator</module>
         <module>camel-paxlogging</module>
diff --git a/platforms/karaf/features/src/main/resources/features.xml b/platforms/karaf/features/src/main/resources/features.xml
index 93b40c8..844125f 100644
--- a/platforms/karaf/features/src/main/resources/features.xml
+++ b/platforms/karaf/features/src/main/resources/features.xml
@@ -1049,6 +1049,7 @@
     <feature version='${project.version}'>camel-core</feature>
     <bundle dependency='true'>mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.javax-cache-api/${jcache-bundle-version}</bundle>
     <bundle>mvn:org.apache.camel/camel-jcache/${project.version}</bundle>
+    <bundle>mvn:org.apache.camel.karaf/camel-jcache-osgi/${project.version}</bundle>
   </feature>
   <feature name='camel-jcr' version='${project.version}' start-level='50'>
     <feature version='${project.version}'>camel-core</feature>