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 da...@apache.org on 2015/08/03 11:28:16 UTC

svn commit: r1693868 - in /jackrabbit/oak/trunk/oak-core/src: main/java/org/apache/jackrabbit/oak/osgi/ main/java/org/apache/jackrabbit/oak/plugins/segment/ test/java/org/apache/jackrabbit/oak/osgi/

Author: davide
Date: Mon Aug  3 09:28:16 2015
New Revision: 1693868

URL: http://svn.apache.org/r1693868
Log:
OAK-3048 - Enable lookup of OSGi configuration from framework first and
component next

- applying Francesco Mari's patch. Thanks!

Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/osgi/OsgiUtil.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentNodeStoreService.java
    jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/osgi/OsgiUtilTest.java

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/osgi/OsgiUtil.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/osgi/OsgiUtil.java?rev=1693868&r1=1693867&r2=1693868&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/osgi/OsgiUtil.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/osgi/OsgiUtil.java Mon Aug  3 09:28:16 2015
@@ -49,7 +49,7 @@ public class OsgiUtil {
      * {@code null} if the property is not found or if the property is found but
      * it is an empty string.
      *
-     * @param context Component context.
+     * @param context Bundle context.
      * @param name    Name of the property.
      * @return The property value serialized as a string, or {@code null}.
      */
@@ -67,19 +67,75 @@ public class OsgiUtil {
      * @param name    Name of the property.
      * @return The property value serialized as a string, or {@code null}.
      */
-    public static String fallbackLookup(ComponentContext context, String name) {
-        String fromComponent = lookup(context, name);
+    public static String lookupConfigurationThenFramework(ComponentContext context, String name) {
+        return lookupConfigurationThenFramework(context, name, name);
+    }
+
+    /**
+     * Looks a property up by name in the component context first, falling back
+     * in the framework properties if not found. Returns {@code null} if the
+     * property is not found or if the property is found but it is an empty
+     * string.
+     *
+     * @param context         Component context.
+     * @param nameInComponent Name of the property in the component context.
+     * @param nameInFramework Name of the property in the framework properties.
+     * @return The property value serialized as a string, or {@code null}.
+     */
+    public static String lookupConfigurationThenFramework(ComponentContext context, String nameInComponent, String nameInFramework) {
+        String fromComponent = lookup(context, nameInComponent);
 
         if (fromComponent != null) {
             return fromComponent;
         }
 
-        String fromFramework = lookup(context.getBundleContext(), name);
+        String fromFramework = lookup(context.getBundleContext(), nameInFramework);
+
+        if (fromFramework != null) {
+            return fromFramework;
+        }
+
+        return null;
+    }
+
+    /**
+     * Looks a property up by name in the framework properties first, falling
+     * back to the component context if not not found. Returns {@code null} if
+     * the property is not found or if the property is found but it is an empty
+     * string.
+     *
+     * @param context Component context.
+     * @param name    Name of the property.
+     * @return The property value serialized as a string, or {@code null}.
+     */
+    public static String lookupFrameworkThenConfiguration(ComponentContext context, String name) {
+        return lookupFrameworkThenConfiguration(context, name, name);
+    }
+
+    /**
+     * Looks a property up by name in the framework properties first, falling
+     * back to the component context if not not found. Returns {@code null} if
+     * the property is not found or if the property is found but it is an empty
+     * string.
+     *
+     * @param context         Component context.
+     * @param nameInComponent Name of the property in the component context.
+     * @param nameInFramework Name of the property in the framework properties.
+     * @return The property value serialized as a string, or {@code null}.
+     */
+    public static String lookupFrameworkThenConfiguration(ComponentContext context, String nameInComponent, String nameInFramework) {
+        String fromFramework = lookup(checkNotNull(context).getBundleContext(), nameInFramework);
 
         if (fromFramework != null) {
             return fromFramework;
         }
 
+        String fromComponent = lookup(context, nameInComponent);
+
+        if (fromComponent != null) {
+            return fromComponent;
+        }
+
         return null;
     }
 

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentNodeStoreService.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentNodeStoreService.java?rev=1693868&r1=1693867&r2=1693868&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentNodeStoreService.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentNodeStoreService.java Mon Aug  3 09:28:16 2015
@@ -21,7 +21,7 @@ import static java.util.Collections.empt
 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;
-import static org.apache.jackrabbit.oak.osgi.OsgiUtil.fallbackLookup;
+import static org.apache.jackrabbit.oak.osgi.OsgiUtil.lookupConfigurationThenFramework;
 import static org.apache.jackrabbit.oak.plugins.segment.compaction.CompactionStrategy.CLEANUP_DEFAULT;
 import static org.apache.jackrabbit.oak.plugins.segment.compaction.CompactionStrategy.CLONE_BINARIES_DEFAULT;
 import static org.apache.jackrabbit.oak.plugins.segment.compaction.CompactionStrategy.FORCE_AFTER_FAIL_DEFAULT;
@@ -287,7 +287,7 @@ public class SegmentNodeStoreService ext
     @Activate
     private void activate(ComponentContext context) throws IOException {
         this.context = context;
-        this.customBlobStore = Boolean.parseBoolean(fallbackLookup(context, CUSTOM_BLOB_STORE));
+        this.customBlobStore = Boolean.parseBoolean(property(CUSTOM_BLOB_STORE));
 
         if (blobStore == null && customBlobStore) {
             log.info("BlobStore use enabled. SegmentNodeStore would be initialized when BlobStore would be available");
@@ -298,7 +298,7 @@ public class SegmentNodeStoreService ext
 
     public void registerNodeStore() throws IOException {
         if (registerSegmentStore()) {
-            boolean standby = toBoolean(fallbackLookup(context, STANDBY), false);
+            boolean standby = toBoolean(property(STANDBY), false);
             providerRegistration = context.getBundleContext().registerService(
                     SegmentStoreProvider.class.getName(), this, null);
             if (!standby) {
@@ -319,62 +319,62 @@ public class SegmentNodeStoreService ext
         Dictionary<?, ?> properties = context.getProperties();
         name = String.valueOf(properties.get(NAME));
 
-        String directory = fallbackLookup(context, DIRECTORY);
+        String directory = property(DIRECTORY);
         if (directory == null) {
             directory = "tarmk";
         }else{
             directory = FilenameUtils.concat(directory, "segmentstore");
         }
 
-        String mode = fallbackLookup(context, MODE);
+        String mode = property(MODE);
         if (mode == null) {
             mode = System.getProperty(MODE,
                     System.getProperty("sun.arch.data.model", "32"));
         }
 
-        String size = fallbackLookup(context, SIZE);
+        String size = property(SIZE);
         if (size == null) {
             size = System.getProperty(SIZE, "256");
         }
 
-        String cache = fallbackLookup(context, CACHE);
+        String cache = property(CACHE);
         if (cache == null) {
             cache = System.getProperty(CACHE);
         }
 
-        boolean pauseCompaction = toBoolean(fallbackLookup(context, PAUSE_COMPACTION),
+        boolean pauseCompaction = toBoolean(property(PAUSE_COMPACTION),
                 PAUSE_DEFAULT);
         boolean cloneBinaries = toBoolean(
-                fallbackLookup(context, COMPACTION_CLONE_BINARIES),
+                property(COMPACTION_CLONE_BINARIES),
                 CLONE_BINARIES_DEFAULT);
-        long cleanupTs = toLong(fallbackLookup(context, COMPACTION_CLEANUP_TIMESTAMP),
+        long cleanupTs = toLong(property(COMPACTION_CLEANUP_TIMESTAMP),
                 TIMESTAMP_DEFAULT);
-        int retryCount = toInteger(fallbackLookup(context, COMPACTION_RETRY_COUNT),
+        int retryCount = toInteger(property(COMPACTION_RETRY_COUNT),
                 RETRY_COUNT_DEFAULT);
-        boolean forceCommit = toBoolean(fallbackLookup(context, COMPACTION_FORCE_AFTER_FAIL),
+        boolean forceCommit = toBoolean(property(COMPACTION_FORCE_AFTER_FAIL),
                 FORCE_AFTER_FAIL_DEFAULT);
-        final int lockWaitTime = toInteger(fallbackLookup(context, COMPACTION_LOCK_WAIT_TIME),
+        final int lockWaitTime = toInteger(property(COMPACTION_LOCK_WAIT_TIME),
                 COMPACTION_LOCK_WAIT_TIME_DEFAULT);
-        boolean persistCompactionMap = toBoolean(fallbackLookup(context, PERSIST_COMPACTION_MAP),
+        boolean persistCompactionMap = toBoolean(property(PERSIST_COMPACTION_MAP),
                 PERSIST_COMPACTION_MAP_DEFAULT);
-        String cleanup = fallbackLookup(context, COMPACTION_CLEANUP);
+        String cleanup = property(COMPACTION_CLEANUP);
         if (cleanup == null) {
             cleanup = CLEANUP_DEFAULT.toString();
         }
 
-        String memoryThresholdS = fallbackLookup(context, COMPACTION_MEMORY_THRESHOLD);
+        String memoryThresholdS = property(COMPACTION_MEMORY_THRESHOLD);
         byte memoryThreshold = MEMORY_THRESHOLD_DEFAULT;
         if (memoryThresholdS != null) {
             memoryThreshold = Byte.valueOf(memoryThresholdS);
         }
 
-        String gainThresholdS = fallbackLookup(context, COMPACTION_GAIN_THRESHOLD);
+        String gainThresholdS = property(COMPACTION_GAIN_THRESHOLD);
         byte gainThreshold = GAIN_THRESHOLD_DEFAULT;
         if (gainThresholdS != null) {
             gainThreshold = Byte.valueOf(gainThresholdS);
         }
 
-        final long blobGcMaxAgeInSecs = toLong(fallbackLookup(context, PROP_BLOB_GC_MAX_AGE), DEFAULT_BLOB_GC_MAX_AGE);
+        final long blobGcMaxAgeInSecs = toLong(property(PROP_BLOB_GC_MAX_AGE), DEFAULT_BLOB_GC_MAX_AGE);
 
         OsgiWhiteboard whiteboard = new OsgiWhiteboard(context.getBundleContext());
         gcMonitor = new GCMonitorTracker();
@@ -476,6 +476,10 @@ public class SegmentNodeStoreService ext
         return true;
     }
 
+    private String property(String name) {
+        return lookupConfigurationThenFramework(context, name);
+    }
+
     @Deactivate
     public void deactivate() {
         unregisterNodeStore();

Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/osgi/OsgiUtilTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/osgi/OsgiUtilTest.java?rev=1693868&r1=1693867&r2=1693868&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/osgi/OsgiUtilTest.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/osgi/OsgiUtilTest.java Mon Aug  3 09:28:16 2015
@@ -23,8 +23,9 @@ import org.osgi.service.component.Compon
 
 import java.util.Dictionary;
 
-import static org.apache.jackrabbit.oak.osgi.OsgiUtil.fallbackLookup;
 import static org.apache.jackrabbit.oak.osgi.OsgiUtil.lookup;
+import static org.apache.jackrabbit.oak.osgi.OsgiUtil.lookupConfigurationThenFramework;
+import static org.apache.jackrabbit.oak.osgi.OsgiUtil.lookupFrameworkThenConfiguration;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNull;
 import static org.mockito.Mockito.doReturn;
@@ -140,63 +141,67 @@ public class OsgiUtilTest {
     }
 
     @Test
-    public void testFallbackLookupWithNotFoundValue() {
+    public void testFallbackLookupWithNoValue() {
         Dictionary dictionary = mock(Dictionary.class);
-        doReturn(null).when(dictionary).get("name");
+        doReturn(null).when(dictionary).get("cname");
 
         BundleContext bundleContext = mock(BundleContext.class);
-        doReturn(null).when(bundleContext).getProperty("name");
+        doReturn(null).when(bundleContext).getProperty("fname");
 
         ComponentContext componentContext = mock(ComponentContext.class);
         doReturn(dictionary).when(componentContext).getProperties();
         doReturn(bundleContext).when(componentContext).getBundleContext();
 
-        assertNull(fallbackLookup(componentContext, "name"));
+        assertNull(lookupConfigurationThenFramework(componentContext, "cname", "fname"));
+        assertNull(lookupFrameworkThenConfiguration(componentContext, "cname", "fname"));
     }
 
     @Test
     public void testFallbackLookupWithValueInComponent() {
         Dictionary dictionary = mock(Dictionary.class);
-        doReturn("value").when(dictionary).get("name");
+        doReturn("value").when(dictionary).get("cname");
 
         BundleContext bundleContext = mock(BundleContext.class);
-        doReturn(null).when(bundleContext).getProperty("name");
+        doReturn(null).when(bundleContext).getProperty("fname");
 
         ComponentContext componentContext = mock(ComponentContext.class);
         doReturn(dictionary).when(componentContext).getProperties();
         doReturn(bundleContext).when(componentContext).getBundleContext();
 
-        assertEquals("value", fallbackLookup(componentContext, "name"));
+        assertEquals("value", lookupConfigurationThenFramework(componentContext, "cname", "fname"));
+        assertEquals("value", lookupFrameworkThenConfiguration(componentContext, "cname", "fname"));
     }
 
     @Test
     public void testFallbackLookupWithValueInFramework() {
         Dictionary dictionary = mock(Dictionary.class);
-        doReturn(null).when(dictionary).get("name");
+        doReturn(null).when(dictionary).get("cname");
 
         BundleContext bundleContext = mock(BundleContext.class);
-        doReturn("value").when(bundleContext).getProperty("name");
+        doReturn("value").when(bundleContext).getProperty("fname");
 
         ComponentContext componentContext = mock(ComponentContext.class);
         doReturn(dictionary).when(componentContext).getProperties();
         doReturn(bundleContext).when(componentContext).getBundleContext();
 
-        assertEquals("value", fallbackLookup(componentContext, "name"));
+        assertEquals("value", lookupConfigurationThenFramework(componentContext, "cname", "fname"));
+        assertEquals("value", lookupFrameworkThenConfiguration(componentContext, "cname", "fname"));
     }
 
     @Test
     public void testFallbackLookupWithValueInComponentAndFramework() {
         Dictionary dictionary = mock(Dictionary.class);
-        doReturn("value").when(dictionary).get("name");
+        doReturn("cvalue").when(dictionary).get("cname");
 
         BundleContext bundleContext = mock(BundleContext.class);
-        doReturn("ignored").when(bundleContext).getProperty("name");
+        doReturn("fvalue").when(bundleContext).getProperty("fname");
 
         ComponentContext componentContext = mock(ComponentContext.class);
         doReturn(dictionary).when(componentContext).getProperties();
         doReturn(bundleContext).when(componentContext).getBundleContext();
 
-        assertEquals("value", fallbackLookup(componentContext, "name"));
+        assertEquals("cvalue", lookupConfigurationThenFramework(componentContext, "cname", "fname"));
+        assertEquals("fvalue", lookupFrameworkThenConfiguration(componentContext, "cname", "fname"));
     }
 
 }