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 mr...@apache.org on 2017/05/03 08:37:57 UTC

svn commit: r1793625 - /jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreService.java

Author: mreutegg
Date: Wed May  3 08:37:57 2017
New Revision: 1793625

URL: http://svn.apache.org/viewvc?rev=1793625&view=rev
Log:
OAK-6154: Use Closer in DocumentNodeStoreService

Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreService.java

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreService.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreService.java?rev=1793625&r1=1793624&r2=1793625&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreService.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreService.java Wed May  3 08:37:57 2017
@@ -19,6 +19,7 @@
 package org.apache.jackrabbit.oak.plugins.document;
 
 import static com.google.common.base.Preconditions.checkNotNull;
+import static org.apache.jackrabbit.oak.commons.IOUtils.closeQuietly;
 import static org.apache.jackrabbit.oak.commons.PropertiesUtil.toBoolean;
 import static org.apache.jackrabbit.oak.commons.PropertiesUtil.toInteger;
 import static org.apache.jackrabbit.oak.commons.PropertiesUtil.toLong;
@@ -34,20 +35,21 @@ import static org.apache.jackrabbit.oak.
 import static org.apache.jackrabbit.oak.spi.whiteboard.WhiteboardUtils.registerMBean;
 
 import java.io.ByteArrayInputStream;
+import java.io.Closeable;
 import java.io.IOException;
-import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Dictionary;
 import java.util.HashMap;
 import java.util.Hashtable;
-import java.util.List;
 import java.util.Locale;
 import java.util.Map;
 import java.util.concurrent.TimeUnit;
 
+import javax.annotation.Nonnull;
 import javax.sql.DataSource;
 
 import com.google.common.base.Strings;
+import com.google.common.io.Closer;
 import com.mongodb.MongoClientURI;
 
 import org.apache.commons.io.FilenameUtils;
@@ -91,6 +93,7 @@ import org.apache.jackrabbit.oak.spi.sta
 import org.apache.jackrabbit.oak.spi.state.NodeStoreProvider;
 import org.apache.jackrabbit.oak.spi.state.RevisionGC;
 import org.apache.jackrabbit.oak.spi.state.RevisionGCMBean;
+import org.apache.jackrabbit.oak.spi.whiteboard.AbstractServiceTracker;
 import org.apache.jackrabbit.oak.spi.whiteboard.Registration;
 import org.apache.jackrabbit.oak.spi.whiteboard.Whiteboard;
 import org.apache.jackrabbit.oak.spi.whiteboard.WhiteboardExecutor;
@@ -274,7 +277,7 @@ public class DocumentNodeStoreService {
     private final Logger log = LoggerFactory.getLogger(this.getClass());
 
     private ServiceRegistration nodeStoreReg;
-    private final List<Registration> registrations = new ArrayList<Registration>();
+    private Closer closer;
     private WhiteboardExecutor executor;
 
     @Reference(cardinality = ReferenceCardinality.OPTIONAL_UNARY,
@@ -403,6 +406,7 @@ public class DocumentNodeStoreService {
 
     @Activate
     protected void activate(ComponentContext context, Map<String, ?> config) throws Exception {
+        closer = Closer.create();
         this.context = context;
         whiteboard = new OsgiWhiteboard(context.getBundleContext());
         executor = new WhiteboardExecutor();
@@ -752,10 +756,7 @@ public class DocumentNodeStoreService {
     private void unregisterNodeStore() {
         deactivationTimestamp = System.currentTimeMillis();
 
-        for (Registration r : registrations) {
-            r.unregister();
-        }
-        registrations.clear();
+        closeQuietly(closer);
 
         if (nodeStoreReg != null) {
             nodeStoreReg.unregister();
@@ -784,13 +785,13 @@ public class DocumentNodeStoreService {
 
     private void registerJMXBeans(final DocumentNodeStore store, DocumentMK.Builder mkBuilder) throws
             IOException {
-        registrations.add(
+        addRegistration(
                 registerMBean(whiteboard,
                         CacheStatsMBean.class,
                         store.getNodeCacheStats(),
                         CacheStatsMBean.TYPE,
                         store.getNodeCacheStats().getName()));
-        registrations.add(
+        addRegistration(
                 registerMBean(whiteboard,
                         CacheStatsMBean.class,
                         store.getNodeChildrenCacheStats(),
@@ -798,7 +799,7 @@ public class DocumentNodeStoreService {
                         store.getNodeChildrenCacheStats().getName())
         );
         for (CacheStats cs : store.getDiffCacheStats()) {
-            registrations.add(
+            addRegistration(
                     registerMBean(whiteboard,
                             CacheStatsMBean.class, cs,
                             CacheStatsMBean.TYPE, cs.getName()));
@@ -806,7 +807,7 @@ public class DocumentNodeStoreService {
         DocumentStore ds = store.getDocumentStore();
         if (ds.getCacheStats() != null) {
             for (CacheStats cacheStats : ds.getCacheStats()) {
-                registrations.add(
+                addRegistration(
                         registerMBean(whiteboard,
                                 CacheStatsMBean.class,
                                 cacheStats,
@@ -816,7 +817,7 @@ public class DocumentNodeStoreService {
             }
         }
 
-        registrations.add(
+        addRegistration(
                 registerMBean(whiteboard,
                         CheckpointMBean.class,
                         new DocumentCheckpointMBean(store),
@@ -824,7 +825,7 @@ public class DocumentNodeStoreService {
                         "Document node store checkpoint management")
         );
 
-        registrations.add(
+        addRegistration(
                 registerMBean(whiteboard,
                         DocumentNodeStoreMBean.class,
                         store.getMBean(),
@@ -833,7 +834,7 @@ public class DocumentNodeStoreService {
         );
 
         if (mkBuilder.getBlobStoreCacheStats() != null) {
-            registrations.add(
+            addRegistration(
                     registerMBean(whiteboard,
                             CacheStatsMBean.class,
                             mkBuilder.getBlobStoreCacheStats(),
@@ -843,7 +844,7 @@ public class DocumentNodeStoreService {
         }
 
         if (mkBuilder.getDocumentStoreStatsCollector() instanceof DocumentStoreStatsMBean) {
-            registrations.add(
+            addRegistration(
                     registerMBean(whiteboard,
                             DocumentStoreStatsMBean.class,
                             (DocumentStoreStatsMBean) mkBuilder.getDocumentStoreStatsCollector(),
@@ -855,7 +856,7 @@ public class DocumentNodeStoreService {
         // register persistent cache stats
         Map<CacheType, PersistentCacheStats> persistenceCacheStats = mkBuilder.getPersistenceCacheStats();
         for (PersistentCacheStats pcs: persistenceCacheStats.values()) {
-            registrations.add(
+            addRegistration(
                     registerMBean(whiteboard,
                             PersistentCacheStatsMBean.class,
                             pcs,
@@ -871,7 +872,7 @@ public class DocumentNodeStoreService {
         if (store.getBlobStore() instanceof GarbageCollectableBlobStore) {
             BlobGarbageCollector gc = store.createBlobGarbageCollector(blobGcMaxAgeInSecs, 
                                                         ClusterRepositoryInfo.getOrCreateId(nodeStore));
-            registrations.add(registerMBean(whiteboard, BlobGCMBean.class, new BlobGC(gc, executor),
+            addRegistration(registerMBean(whiteboard, BlobGCMBean.class, new BlobGC(gc, executor),
                     BlobGCMBean.TYPE, "Document node store blob garbage collection"));
         }
 
@@ -892,12 +893,12 @@ public class DocumentNodeStoreService {
             }
         };
         RevisionGC revisionGC = new RevisionGC(startGC, cancelGC, executor);
-        registrations.add(registerMBean(whiteboard, RevisionGCMBean.class, revisionGC,
+        addRegistration(registerMBean(whiteboard, RevisionGCMBean.class, revisionGC,
                 RevisionGCMBean.TYPE, "Document node store revision garbage collection"));
 
         BlobStoreStats blobStoreStats = mkBuilder.getBlobStoreStats();
         if (!customBlobStore && blobStoreStats != null) {
-            registrations.add(registerMBean(whiteboard,
+            addRegistration(registerMBean(whiteboard,
                     BlobStoreStatsMBean.class,
                     blobStoreStats,
                     BlobStoreStatsMBean.TYPE,
@@ -905,7 +906,7 @@ public class DocumentNodeStoreService {
         }
 
         if (!mkBuilder.isBundlingDisabled()){
-            registrations.add(registerMBean(whiteboard,
+            addRegistration(registerMBean(whiteboard,
                     BackgroundObserverMBean.class,
                     store.getBundlingConfigHandler().getMBean(),
                     BackgroundObserverMBean.TYPE,
@@ -922,7 +923,7 @@ public class DocumentNodeStoreService {
                 nodeStore.getLastRevRecoveryAgent().performRecoveryIfNeeded();
             }
         };
-        registrations.add(WhiteboardUtils.scheduleWithFixedDelay(whiteboard,
+        addRegistration(WhiteboardUtils.scheduleWithFixedDelay(whiteboard,
                 recoverJob, TimeUnit.MILLISECONDS.toSeconds(leaseTime),
                 false/*runOnSingleClusterNode*/, true /*use dedicated pool*/));
     }
@@ -941,7 +942,7 @@ public class DocumentNodeStoreService {
             }
 
         };
-        registrations.add(WhiteboardUtils.scheduleWithFixedDelay(whiteboard,
+        addRegistration(WhiteboardUtils.scheduleWithFixedDelay(whiteboard,
                 journalGCJob, TimeUnit.MILLISECONDS.toSeconds(journalGCInterval),
                 true/*runOnSingleClusterNode*/, true /*use dedicated pool*/));
     }
@@ -985,4 +986,28 @@ public class DocumentNodeStoreService {
         }
         return result;
     }
+
+    private void addRegistration(@Nonnull Registration reg) {
+        closer.register(asCloseable(reg));
+    }
+
+    private static Closeable asCloseable(@Nonnull final Registration reg) {
+        checkNotNull(reg);
+        return new Closeable() {
+            @Override
+            public void close() throws IOException {
+                reg.unregister();
+            }
+        };
+    }
+
+    private static Closeable asCloseable(@Nonnull final AbstractServiceTracker t) {
+        checkNotNull(t);
+        return new Closeable() {
+            @Override
+            public void close() throws IOException {
+                t.stop();
+            }
+        };
+    }
 }