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 2012/06/21 22:36:01 UTC

svn commit: r1352670 - in /jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr: ObservationManagerImpl.java RepositoryImpl.java SessionDelegate.java util/ util/LazyValue.java

Author: mduerig
Date: Thu Jun 21 20:36:00 2012
New Revision: 1352670

URL: http://svn.apache.org/viewvc?rev=1352670&view=rev
Log:
OAK-144: Implement observation
- Use shared background thread for all sessions instead of one thread per session

Added:
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/util/
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/util/LazyValue.java
Modified:
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/ObservationManagerImpl.java
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/RepositoryImpl.java
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/SessionDelegate.java

Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/ObservationManagerImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/ObservationManagerImpl.java?rev=1352670&r1=1352669&r2=1352670&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/ObservationManagerImpl.java (original)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/ObservationManagerImpl.java Thu Jun 21 20:36:00 2012
@@ -33,6 +33,7 @@ import org.apache.jackrabbit.commons.ite
 import org.apache.jackrabbit.oak.api.ChangeExtractor;
 import org.apache.jackrabbit.oak.api.PropertyState;
 import org.apache.jackrabbit.oak.commons.PathUtils;
+import org.apache.jackrabbit.oak.jcr.util.LazyValue;
 import org.apache.jackrabbit.oak.spi.state.NodeState;
 import org.apache.jackrabbit.oak.spi.state.NodeStateDiff;
 
@@ -41,17 +42,18 @@ public class ObservationManagerImpl impl
     private final Map<EventListener, ChangeProcessor> processors =
             new HashMap<EventListener, ChangeProcessor>();
 
-    private final Timer timer = new Timer("ObservationManager", true);
+    private final LazyValue<Timer> timer;
 
-    public ObservationManagerImpl(SessionDelegate sessionDelegate) {
+    public ObservationManagerImpl(SessionDelegate sessionDelegate, LazyValue<Timer> timer) {
         this.sessionDelegate = sessionDelegate;
+        this.timer = timer;
     }
 
     public void dispose() {
         for (ChangeProcessor processor : processors.values()) {
             processor.stop();
         }
-        timer.cancel();
+        timer.get().cancel();
     }
 
     @Override
@@ -65,7 +67,7 @@ public class ObservationManagerImpl impl
             ChangeFilter filter = new ChangeFilter(eventTypes, absPath, isDeep, uuid, nodeTypeName, noLocal);
             ChangeProcessor changeProcessor = new ChangeProcessor(extractor, listener, filter);
             processors.put(listener, changeProcessor);
-            timer.schedule(changeProcessor, 0, 1000);
+            timer.get().schedule(changeProcessor, 0, 1000);
         }
         else {
             ChangeFilter filter = new ChangeFilter(eventTypes, absPath, isDeep, uuid, nodeTypeName, noLocal);

Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/RepositoryImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/RepositoryImpl.java?rev=1352670&r1=1352669&r2=1352670&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/RepositoryImpl.java (original)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/RepositoryImpl.java Thu Jun 21 20:36:00 2012
@@ -16,12 +16,7 @@
  */
 package org.apache.jackrabbit.oak.jcr;
 
-import org.apache.jackrabbit.commons.SimpleValueFactory;
-import org.apache.jackrabbit.oak.api.ContentSession;
-import org.apache.jackrabbit.oak.api.ContentRepository;
-import org.apache.jackrabbit.oak.core.ContentRepositoryImpl;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import java.util.Timer;
 
 import javax.jcr.Credentials;
 import javax.jcr.Repository;
@@ -30,6 +25,14 @@ import javax.jcr.Session;
 import javax.jcr.Value;
 import javax.security.auth.login.LoginException;
 
+import org.apache.jackrabbit.commons.SimpleValueFactory;
+import org.apache.jackrabbit.oak.api.ContentRepository;
+import org.apache.jackrabbit.oak.api.ContentSession;
+import org.apache.jackrabbit.oak.core.ContentRepositoryImpl;
+import org.apache.jackrabbit.oak.jcr.util.LazyValue;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
 /**
  * {@code RepositoryImpl}...
  */
@@ -43,6 +46,13 @@ public class RepositoryImpl implements R
     private final Descriptors descriptors = new Descriptors(new SimpleValueFactory());
     private final ContentRepository contentRepository;
 
+    private final LazyValue<Timer> observationTimer = new LazyValue<Timer>() {
+        @Override
+        protected Timer create() {
+            return new Timer("Observation", true);
+        }
+    };
+
     public RepositoryImpl(ContentRepository contentRepository) {
         this.contentRepository = contentRepository;
     }
@@ -121,7 +131,7 @@ public class RepositoryImpl implements R
         // TODO: needs complete refactoring
         try {
             ContentSession contentSession = contentRepository.login(credentials, workspaceName);
-            return new SessionDelegate(this, contentSession).getSession();
+            return new SessionDelegate(this, observationTimer, contentSession).getSession();
         } catch (LoginException e) {
             throw new javax.jcr.LoginException(e.getMessage());
         }

Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/SessionDelegate.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/SessionDelegate.java?rev=1352670&r1=1352669&r2=1352670&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/SessionDelegate.java (original)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/SessionDelegate.java Thu Jun 21 20:36:00 2012
@@ -20,6 +20,7 @@ import java.io.IOException;
 import java.text.ParseException;
 import java.util.Collections;
 import java.util.Map;
+import java.util.Timer;
 
 import javax.annotation.CheckForNull;
 import javax.annotation.Nonnull;
@@ -50,6 +51,7 @@ import org.apache.jackrabbit.oak.api.Roo
 import org.apache.jackrabbit.oak.api.Tree;
 import org.apache.jackrabbit.oak.commons.PathUtils;
 import org.apache.jackrabbit.oak.core.DefaultConflictHandler;
+import org.apache.jackrabbit.oak.jcr.util.LazyValue;
 import org.apache.jackrabbit.oak.jcr.value.ValueFactoryImpl;
 import org.apache.jackrabbit.oak.namepath.AbstractNameMapper;
 import org.apache.jackrabbit.oak.namepath.NameMapper;
@@ -65,6 +67,7 @@ public class SessionDelegate {
     private final NameMapper nameMapper = new SessionNameMapper();
     private final NamePathMapper namePathMapper = new NamePathMapperImpl(nameMapper);
     private final Repository repository;
+    private final LazyValue<Timer> observationTimer;
     private final ContentSession contentSession;
     private final ValueFactoryImpl valueFactory;
     private final Workspace workspace;
@@ -75,11 +78,13 @@ public class SessionDelegate {
     private ObservationManagerImpl observationManager;
     private boolean isAlive = true;
 
-    SessionDelegate(Repository repository, ContentSession contentSession) throws RepositoryException {
+    SessionDelegate(Repository repository, LazyValue<Timer> observationTimer, ContentSession contentSession)
+            throws RepositoryException {
         assert repository != null;
         assert contentSession != null;
 
         this.repository = repository;
+        this.observationTimer = observationTimer;
         this.contentSession = contentSession;
         this.valueFactory = new ValueFactoryImpl(contentSession.getCoreValueFactory(), namePathMapper);
         this.workspace = new WorkspaceImpl(this);
@@ -374,7 +379,7 @@ public class SessionDelegate {
     @Nonnull
     public ObservationManager getObservationManager() {
         if (observationManager == null) {
-            observationManager = new ObservationManagerImpl(this);
+            observationManager = new ObservationManagerImpl(this, observationTimer);
         }
         return observationManager;
     }

Added: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/util/LazyValue.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/util/LazyValue.java?rev=1352670&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/util/LazyValue.java (added)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/util/LazyValue.java Thu Jun 21 20:36:00 2012
@@ -0,0 +1,47 @@
+/*
+ * 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.jcr.util;
+
+/**
+ * Abstract base class for a lazy value of type {@code T}. The initialisation of
+ * the actual value is deferred until first accessed.
+ *
+ * @param <T> type of the value
+ */
+public abstract class LazyValue<T> {
+    private T value;
+
+    /**
+     * Create the value.
+     * @return  the value
+     */
+    protected abstract T create();
+
+    /**
+     * Retrieve the value. The first call of this method results in a call
+     * to {@link #create()}. Subsequent calls return the same instance as the
+     * first call.
+     *
+     * @return the underlying value
+     */
+    public T get() {
+        if (value == null) {
+            value = create();
+        }
+        return value;
+    }
+}