CAMEL-16711: components - Move OSGi dependenct code to camel-karaf
diff --git a/components/camel-zookeeper-master-osgi/pom.xml b/components/camel-zookeeper-master-osgi/pom.xml
new file mode 100644
index 0000000..fa5f623
--- /dev/null
+++ b/components/camel-zookeeper-master-osgi/pom.xml
@@ -0,0 +1,62 @@
+<?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-zookeeper-master-osgi</artifactId>
+    <packaging>jar</packaging>
+    <name>Camel Karaf :: Zookeeper Master</name>
+    <description>Camel Zookeeper Master Support</description>
+
+    <properties>
+        <camel.osgi.activator>
+            org.apache.camel.component.zookeepermaster.osgi.Activator
+        </camel.osgi.activator>
+        <firstVersion>2.19.0</firstVersion>
+        <label>clustering,management,bigdata</label>
+        <title>ZooKeeper Master</title>
+    </properties>
+
+    <dependencies>
+
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-zookeeper-master</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-zookeeper-master-osgi/src/generated/resources/META-INF/services/org/apache/camel/other.properties b/components/camel-zookeeper-master-osgi/src/generated/resources/META-INF/services/org/apache/camel/other.properties
new file mode 100644
index 0000000..bad92d2
--- /dev/null
+++ b/components/camel-zookeeper-master-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=zookeeper-master-osgi
+groupId=org.apache.camel.karaf
+artifactId=camel-zookeeper-master-osgi
+version=3.11.0-SNAPSHOT
+projectName=Camel Karaf :: Zookeeper Master
+projectDescription=Camel Zookeeper Master Support
diff --git a/components/camel-zookeeper-master-osgi/src/generated/resources/zookeeper-master-osgi.json b/components/camel-zookeeper-master-osgi/src/generated/resources/zookeeper-master-osgi.json
new file mode 100644
index 0000000..2a17da9
--- /dev/null
+++ b/components/camel-zookeeper-master-osgi/src/generated/resources/zookeeper-master-osgi.json
@@ -0,0 +1,15 @@
+{
+  "other": {
+    "kind": "other",
+    "name": "zookeeper-master-osgi",
+    "title": "ZooKeeper Master",
+    "description": "Camel Zookeeper Master Support",
+    "deprecated": false,
+    "firstVersion": "2.19.0",
+    "label": "clustering,management,bigdata",
+    "supportLevel": "Stable",
+    "groupId": "org.apache.camel.karaf",
+    "artifactId": "camel-zookeeper-master-osgi",
+    "version": "3.11.0-SNAPSHOT"
+  }
+}
diff --git a/components/camel-zookeeper-master-osgi/src/main/java/org/apache/camel/component/zookeepermaster/osgi/Activator.java b/components/camel-zookeeper-master-osgi/src/main/java/org/apache/camel/component/zookeepermaster/osgi/Activator.java
new file mode 100644
index 0000000..459565b
--- /dev/null
+++ b/components/camel-zookeeper-master-osgi/src/main/java/org/apache/camel/component/zookeepermaster/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.zookeepermaster.osgi;
+
+import org.apache.camel.component.zookeepermaster.ManagedGroupFactoryStrategy;
+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 OsgiManagedGroupFactoryStrategy();
+        registration = context.registerService(OsgiManagedGroupFactoryStrategy.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-zookeeper-master-osgi/src/main/java/org/apache/camel/component/zookeepermaster/osgi/OsgiManagedGroupFactory.java b/components/camel-zookeeper-master-osgi/src/main/java/org/apache/camel/component/zookeepermaster/osgi/OsgiManagedGroupFactory.java
new file mode 100644
index 0000000..e5bf1be
--- /dev/null
+++ b/components/camel-zookeeper-master-osgi/src/main/java/org/apache/camel/component/zookeepermaster/osgi/OsgiManagedGroupFactory.java
@@ -0,0 +1,184 @@
+/*
+ * 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.zookeepermaster.osgi;
+
+import org.apache.camel.component.zookeepermaster.group.Group;
+import org.apache.camel.component.zookeepermaster.group.ManagedGroupFactory;
+import org.apache.camel.component.zookeepermaster.group.NodeState;
+import org.apache.camel.component.zookeepermaster.group.internal.DelegateZooKeeperGroup;
+import org.apache.camel.component.zookeepermaster.group.internal.DelegateZooKeeperMultiGroup;
+import org.apache.curator.framework.CuratorFramework;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleReference;
+import org.osgi.framework.ServiceReference;
+import org.osgi.util.tracker.ServiceTracker;
+import org.osgi.util.tracker.ServiceTrackerCustomizer;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.ThreadFactory;
+
+public class OsgiManagedGroupFactory implements ManagedGroupFactory {
+
+    private ManagedGroupFactory delegate;
+
+    public OsgiManagedGroupFactory() {
+    }
+
+    public void setClassLoader(ClassLoader loader) {
+        this.delegate = new OsgiTrackingManagedGroupFactory(loader);
+    }
+
+    @Override
+    public CuratorFramework getCurator() {
+        return delegate.getCurator();
+    }
+
+    @Override
+    public <T extends NodeState> Group<T> createGroup(String path, Class<T> clazz) {
+        return delegate.createGroup(path, clazz);
+    }
+
+    @Override
+    public <T extends NodeState> Group<T> createGroup(String path, Class<T> clazz, ThreadFactory threadFactory) {
+        return delegate.createGroup(path, clazz, threadFactory);
+    }
+
+    @Override
+    public <T extends NodeState> Group<T> createMultiGroup(String path, Class<T> clazz) {
+        throw new IllegalStateException("not supported");
+    }
+
+    @Override
+    public <T extends NodeState> Group<T> createMultiGroup(String path, Class<T> clazz, ThreadFactory threadFactory) {
+        throw new IllegalStateException("not supported");
+    }
+
+    @Override
+    public void close() {
+        delegate.close();
+    }
+
+    static class OsgiTrackingManagedGroupFactory
+            implements ManagedGroupFactory, ServiceTrackerCustomizer<CuratorFramework, CuratorFramework> {
+
+        private final BundleContext bundleContext;
+        private final ServiceTracker<CuratorFramework, CuratorFramework> tracker;
+        private CuratorFramework curator;
+        private final List<DelegateZooKeeperGroup<?>> groups = new ArrayList<>();
+
+        OsgiTrackingManagedGroupFactory(ClassLoader loader) {
+            this(getBundleContext(loader));
+        }
+
+        OsgiTrackingManagedGroupFactory(BundleContext bundleContext) {
+            this.bundleContext = bundleContext;
+            this.tracker = new ServiceTracker<>(
+                    bundleContext, CuratorFramework.class, this);
+            this.tracker.open();
+        }
+
+        static BundleContext getBundleContext(ClassLoader loader) {
+            if (!(loader instanceof BundleReference)) {
+                throw new IllegalStateException("Not an OSGi ClassLoader");
+            }
+            return ((BundleReference) loader).getBundle().getBundleContext();
+        }
+
+        @Override
+        public CuratorFramework addingService(ServiceReference<CuratorFramework> reference) {
+            CuratorFramework curator = OsgiTrackingManagedGroupFactory.this.bundleContext.getService(reference);
+            useCurator(curator);
+            return curator;
+        }
+
+        @Override
+        public void modifiedService(ServiceReference<CuratorFramework> reference, CuratorFramework service) {
+        }
+
+        @Override
+        public void removedService(ServiceReference<CuratorFramework> reference, CuratorFramework service) {
+            useCurator(null);
+            OsgiTrackingManagedGroupFactory.this.bundleContext.ungetService(reference);
+        }
+
+        protected void useCurator(CuratorFramework curator) {
+            this.curator = curator;
+            for (DelegateZooKeeperGroup<?> group : groups) {
+                group.useCurator(curator);
+            }
+        }
+
+        @Override
+        public CuratorFramework getCurator() {
+            return curator;
+        }
+
+        @Override
+        public <T extends NodeState> Group<T> createGroup(String path, Class<T> clazz) {
+            return new DelegateZooKeeperGroup<T>(path, clazz) {
+                @Override
+                public void start() {
+                    useCurator(curator);
+                    groups.add(this);
+                    super.start();
+                }
+
+                @Override
+                public void close() throws IOException {
+                    groups.remove(this);
+                    super.close();
+                }
+            };
+        }
+
+        @Override
+        public <T extends NodeState> Group<T> createGroup(String path, Class<T> clazz, ThreadFactory threadFactory) {
+            throw new IllegalStateException("not supported");
+        }
+
+        @Override
+        public <T extends NodeState> Group<T> createMultiGroup(String path, Class<T> clazz) {
+            return new DelegateZooKeeperMultiGroup<T>(path, clazz) {
+                @Override
+                public void start() {
+                    useCurator(curator);
+                    groups.add(this);
+                    super.start();
+                }
+
+                @Override
+                public void close() throws IOException {
+                    groups.remove(this);
+                    super.close();
+                }
+            };
+        }
+
+        @Override
+        public <T extends NodeState> Group<T> createMultiGroup(String path, Class<T> clazz, ThreadFactory threadFactory) {
+            throw new IllegalStateException("not supported");
+        }
+
+        @Override
+        public void close() {
+            this.tracker.close();
+        }
+    }
+
+}
diff --git a/components/camel-zookeeper-master-osgi/src/main/java/org/apache/camel/component/zookeepermaster/osgi/OsgiManagedGroupFactoryStrategy.java b/components/camel-zookeeper-master-osgi/src/main/java/org/apache/camel/component/zookeepermaster/osgi/OsgiManagedGroupFactoryStrategy.java
new file mode 100644
index 0000000..4c2724f
--- /dev/null
+++ b/components/camel-zookeeper-master-osgi/src/main/java/org/apache/camel/component/zookeepermaster/osgi/OsgiManagedGroupFactoryStrategy.java
@@ -0,0 +1,34 @@
+/*
+ * 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.zookeepermaster.osgi;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.component.zookeepermaster.ManagedGroupFactoryStrategy;
+import org.apache.camel.component.zookeepermaster.group.ManagedGroupFactory;
+import org.apache.curator.framework.CuratorFramework;
+
+import java.util.concurrent.Callable;
+
+public class OsgiManagedGroupFactoryStrategy implements ManagedGroupFactoryStrategy {
+
+    @Override
+    public ManagedGroupFactory createGroupFactory(CuratorFramework curator, ClassLoader loader, CamelContext camelContext, Callable<CuratorFramework> factory) throws Exception {
+        OsgiManagedGroupFactory answer = new OsgiManagedGroupFactory();
+        answer.setClassLoader(loader);
+        return answer;
+    }
+}
diff --git a/components/pom.xml b/components/pom.xml
index f06ad25..fde3124 100644
--- a/components/pom.xml
+++ b/components/pom.xml
@@ -45,6 +45,7 @@
         <module>camel-paxlogging</module>
         <module>camel-servlet-osgi</module>
         <module>camel-test-karaf</module>
+        <module>camel-zookeeper-master-osgi</module>
     </modules>
 
     <properties>
diff --git a/platforms/karaf/features/src/main/resources/features.xml b/platforms/karaf/features/src/main/resources/features.xml
index 844125f..3a9c906 100644
--- a/platforms/karaf/features/src/main/resources/features.xml
+++ b/platforms/karaf/features/src/main/resources/features.xml
@@ -2426,6 +2426,7 @@
     <bundle dependency='true'>mvn:com.fasterxml.jackson.core/jackson-core/${jackson2-version}</bundle>
     <bundle dependency='true'>mvn:com.fasterxml.jackson.core/jackson-databind/${jackson2-version}</bundle>
     <bundle dependency='true'>mvn:com.fasterxml.jackson.core/jackson-annotations/${jackson2-version}</bundle>
+    <bundle>mvn:org.apache.camel.karaf/camel-zookeeper-master-osgi/${project.version}</bundle>
     <bundle>mvn:org.apache.camel/camel-zookeeper-master/${project.version}</bundle>
   </feature>