SLING-7607 Move ThreadPool (Executor) handling out of OakSlingRepositoryManager
diff --git a/bnd.bnd b/bnd.bnd
index 4069512..e0eb921 100644
--- a/bnd.bnd
+++ b/bnd.bnd
@@ -17,6 +17,9 @@
   org.apache.jackrabbit.test; resolution:=optional,\
   *
 
+Provide-Capability:\
+  osgi.service;objectClass:List<String>="java.util.concurrent.Executor,org.apache.sling.commons.threads.ThreadPool"
+
 -baseline: *
 
 -includeresource:\
diff --git a/src/main/java/org/apache/sling/jcr/oak/server/internal/DefaultThreadPoolRegistrar.java b/src/main/java/org/apache/sling/jcr/oak/server/internal/DefaultThreadPoolRegistrar.java
new file mode 100644
index 0000000..2219538
--- /dev/null
+++ b/src/main/java/org/apache/sling/jcr/oak/server/internal/DefaultThreadPoolRegistrar.java
@@ -0,0 +1,61 @@
+/*
+ * 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.sling.jcr.oak.server.internal;
+
+import org.apache.sling.commons.threads.ThreadPool;
+import org.apache.sling.commons.threads.ThreadPoolManager;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Deactivate;
+import org.osgi.service.component.annotations.Reference;
+
+@Component(
+    immediate = true
+)
+public class DefaultThreadPoolRegistrar {
+
+    @Reference
+    private ThreadPoolManager threadPoolManager;
+
+    private ThreadPool threadPool;
+
+    private ServiceRegistration<ThreadPool> serviceRegistration;
+
+    public DefaultThreadPoolRegistrar() {
+    }
+
+    @Activate
+    private void activate(final BundleContext bundleContext) {
+        threadPool = threadPoolManager.get(null);
+        serviceRegistration = bundleContext.registerService(ThreadPool.class, threadPool, null);
+    }
+
+    @Deactivate
+    private void deactivate() {
+        if (serviceRegistration != null) {
+            serviceRegistration.unregister();
+            serviceRegistration = null;
+        }
+        threadPoolManager.release(threadPool);
+        threadPool = null;
+    }
+
+}
diff --git a/src/main/java/org/apache/sling/jcr/oak/server/internal/OakSlingRepositoryManager.java b/src/main/java/org/apache/sling/jcr/oak/server/internal/OakSlingRepositoryManager.java
index 365f8bf..d7b3143 100644
--- a/src/main/java/org/apache/sling/jcr/oak/server/internal/OakSlingRepositoryManager.java
+++ b/src/main/java/org/apache/sling/jcr/oak/server/internal/OakSlingRepositoryManager.java
@@ -25,8 +25,6 @@
 
 import java.util.Collections;
 import java.util.Dictionary;
-import java.util.Hashtable;
-import java.util.concurrent.Executor;
 
 import javax.jcr.Repository;
 
@@ -55,8 +53,6 @@
 import org.apache.jackrabbit.oak.spi.state.NodeBuilder;
 import org.apache.jackrabbit.oak.spi.state.NodeStore;
 import org.apache.jackrabbit.oak.spi.whiteboard.Whiteboard;
-import org.apache.sling.commons.threads.ThreadPool;
-import org.apache.sling.commons.threads.ThreadPoolManager;
 import org.apache.sling.jcr.base.AbstractSlingRepository2;
 import org.apache.sling.jcr.base.AbstractSlingRepositoryManager;
 import org.apache.sling.serviceusermapping.ServiceUserMapper;
@@ -96,13 +92,6 @@
 
     private ComponentContext componentContext;
 
-    @Reference
-    private ThreadPoolManager threadPoolManager = null;
-
-    private ThreadPool threadPool;
-
-    private ServiceRegistration oakExecutorServiceReference;
-
     private final WhiteboardIndexProvider indexProvider = new WhiteboardIndexProvider();
 
     private final WhiteboardIndexEditorProvider indexEditorProvider = new WhiteboardIndexEditorProvider();
@@ -130,13 +119,6 @@
         final Whiteboard whiteboard = new OsgiWhiteboard(bundleContext);
         this.indexProvider.start(whiteboard);
         this.indexEditorProvider.start(whiteboard);
-        this.oakExecutorServiceReference = bundleContext.registerService(
-                Executor.class.getName(), new Executor() {
-            @Override
-            public void execute(Runnable command) {
-                threadPool.execute(command);
-            }
-        }, new Hashtable<String, Object>());
 
         final Oak oak = new Oak(nodeStore)
             .withAsyncIndexing("async", 5);
@@ -192,8 +174,6 @@
     protected void disposeRepository(Repository repository) {
         this.indexProvider.stop();
         this.indexEditorProvider.stop();
-        this.oakExecutorServiceReference.unregister();
-        this.oakExecutorServiceReference = null;
         ((JackrabbitRepository) repository).shutdown();
     }
 
@@ -209,7 +189,6 @@
         if (configuration.oak_observation_limitCommitRate()) {
             commitRateLimiter = new CommitRateLimiter();
         }
-        this.threadPool = threadPoolManager.get("oak-observation");
         this.nodeAggregatorRegistration = bundleContext.registerService(NodeAggregator.class.getName(), getNodeAggregator(), null);
 
         super.start(bundleContext, new Config(defaultWorkspace, disableLoginAdministrative));
@@ -219,8 +198,6 @@
     private void deactivate() {
         super.stop();
         this.componentContext = null;
-        this.threadPoolManager.release(this.threadPool);
-        this.threadPool = null;
         this.nodeAggregatorRegistration.unregister();
     }