You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by md...@apache.org on 2013/12/11 10:46:14 UTC

svn commit: r1550091 - in /jackrabbit/oak/trunk: oak-core/src/main/java/org/apache/jackrabbit/oak/osgi/ oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/osgi/

Author: mduerig
Date: Wed Dec 11 09:46:13 2013
New Revision: 1550091

URL: http://svn.apache.org/r1550091
Log:
OAK-1120: Enhance observation mechanism to support Apache Sling
Integrate with Sling's ThreadPool

Added:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/osgi/OakExecutor.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/osgi/OsgiExecutor.java
Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/osgi/ObserverTracker.java
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/osgi/Activator.java

Added: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/osgi/OakExecutor.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/osgi/OakExecutor.java?rev=1550091&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/osgi/OakExecutor.java (added)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/osgi/OakExecutor.java Wed Dec 11 09:46:13 2013
@@ -0,0 +1,29 @@
+/*
+ * 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.jackrabbit.oak.osgi;
+
+import java.util.concurrent.Executor;
+
+/**
+ * The type of the {@code Executor} instance uses by Oak for running
+ * tasks in the background.
+ */
+public interface OakExecutor extends Executor {
+}

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/osgi/ObserverTracker.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/osgi/ObserverTracker.java?rev=1550091&r1=1550090&r2=1550091&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/osgi/ObserverTracker.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/osgi/ObserverTracker.java Wed Dec 11 09:46:13 2013
@@ -29,6 +29,8 @@ import java.util.Map;
 import javax.annotation.Nonnull;
 
 import com.google.common.io.Closeables;
+import com.google.common.io.Closer;
+import org.apache.jackrabbit.oak.spi.commit.BackgroundObserver;
 import org.apache.jackrabbit.oak.spi.commit.Observable;
 import org.apache.jackrabbit.oak.spi.commit.Observer;
 import org.osgi.framework.BundleContext;
@@ -37,11 +39,12 @@ import org.osgi.util.tracker.ServiceTrac
 import org.osgi.util.tracker.ServiceTrackerCustomizer;
 
 public class ObserverTracker implements ServiceTrackerCustomizer {
-    private final Observable observable;
     private final Map<ServiceReference, Closeable> subscriptions = newHashMap();
+    private final OsgiExecutor osgiExecutor = new OsgiExecutor();
+    private final Observable observable;
 
     private BundleContext bundleContext;
-    private ServiceTracker tracker;
+    private ServiceTracker observerTracker;
 
     public ObserverTracker(@Nonnull Observable observable) {
         this.observable = checkNotNull(observable);
@@ -50,13 +53,15 @@ public class ObserverTracker implements 
     public void start(@Nonnull BundleContext bundleContext) {
         checkState(this.bundleContext == null);
         this.bundleContext = checkNotNull(bundleContext);
-        tracker = new ServiceTracker(bundleContext, Observer.class.getName(), this);
-        tracker.open();
+        osgiExecutor.start(bundleContext);
+        observerTracker = new ServiceTracker(bundleContext, Observer.class.getName(), this);
+        observerTracker.open();
     }
 
     public void stop() {
         checkState(this.bundleContext != null);
-        tracker.close();
+        observerTracker.close();
+        osgiExecutor.stop();
     }
 
     //------------------------< ServiceTrackerCustomizer >----------------------
@@ -66,7 +71,11 @@ public class ObserverTracker implements 
         Object service = bundleContext.getService(reference);
 
         if (service instanceof Observer) {
-            Closeable subscription = observable.addObserver((Observer) service);
+            Closer subscription = Closer.create();
+            BackgroundObserver observer = subscription.register(
+                    new BackgroundObserver((Observer) service, osgiExecutor));
+            subscription.register(
+                    observable.addObserver(observer));
             subscriptions.put(reference, subscription);
             return service;
         } else {
@@ -82,8 +91,10 @@ public class ObserverTracker implements 
 
     @Override
     public void removedService(ServiceReference reference, Object service) {
-        Closeables.closeQuietly(subscriptions.get(reference));
-        bundleContext.ungetService(reference);
+        if (subscriptions.containsKey(reference)) {
+            Closeables.closeQuietly(subscriptions.get(reference));
+            bundleContext.ungetService(reference);
+        }
     }
 
 }

Added: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/osgi/OsgiExecutor.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/osgi/OsgiExecutor.java?rev=1550091&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/osgi/OsgiExecutor.java (added)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/osgi/OsgiExecutor.java Wed Dec 11 09:46:13 2013
@@ -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.jackrabbit.oak.osgi;
+
+import static com.google.common.base.Preconditions.checkState;
+
+import java.util.concurrent.Executor;
+
+import org.osgi.framework.BundleContext;
+import org.osgi.util.tracker.ServiceTracker;
+
+/**
+ * OSGi implementation of an {@link OakExecutor} looking it
+ * up from the service registry.
+ */
+public class OsgiExecutor implements OakExecutor {
+    private ServiceTracker executorTracker;
+
+    @Override
+    public void execute(Runnable command) {
+        try {
+            getExecutor().execute(command);
+        } catch (InterruptedException e) {
+            // Not much we can do here. Let's hope someone up
+            // the stack sees the interrupted flag.
+            Thread.currentThread().interrupt();
+        }
+    }
+
+    private Executor getExecutor() throws InterruptedException {
+        return (Executor) executorTracker.waitForService(0);
+    }
+
+    public void start(BundleContext bundleContext) {
+        checkState(executorTracker == null);
+        executorTracker = new ServiceTracker(bundleContext, OakExecutor.class.getName(), null);
+        executorTracker.open();
+    }
+
+    public void stop() {
+        checkState(executorTracker != null);
+        executorTracker.close();
+    }
+}

Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/osgi/Activator.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/osgi/Activator.java?rev=1550091&r1=1550090&r2=1550091&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/osgi/Activator.java (original)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/osgi/Activator.java Wed Dec 11 09:46:13 2013
@@ -25,6 +25,7 @@ import javax.jcr.Repository;
 import org.apache.jackrabbit.oak.Oak;
 import org.apache.jackrabbit.oak.api.ContentRepository;
 import org.apache.jackrabbit.oak.osgi.OsgiEditorProvider;
+import org.apache.jackrabbit.oak.osgi.OsgiExecutor;
 import org.apache.jackrabbit.oak.osgi.OsgiIndexEditorProvider;
 import org.apache.jackrabbit.oak.osgi.OsgiIndexProvider;
 import org.apache.jackrabbit.oak.plugins.commit.JcrConflictHandler;
@@ -66,6 +67,8 @@ public class Activator implements Bundle
     private final OsgiIndexProvider indexProvider =
             new OsgiIndexProvider();
 
+    private final OsgiExecutor executor = new OsgiExecutor();
+
     // TODO should not be hardcoded
     private final SecurityProvider securityProvider =
             new SecurityProviderImpl(buildSecurityConfig());
@@ -97,10 +100,12 @@ public class Activator implements Bundle
         editorProvider.start(bundleContext);
         indexEditorProvider.start(bundleContext);
         indexProvider.start(bundleContext);
+        executor.start(bundleContext);
     }
 
     @Override
     public void stop(BundleContext bundleContext) throws Exception {
+        executor.stop();
         indexProvider.stop();
         indexEditorProvider.stop();
         editorProvider.stop();
@@ -125,6 +130,7 @@ public class Activator implements Bundle
                 .with(indexEditorProvider)
                 .with(indexProvider)
                 .withAsyncIndexing()
+                .with(executor)
                 .createContentRepository();
 
             services.put(reference, context.registerService(