diff --git oak-core/src/main/java/org/apache/jackrabbit/oak/osgi/OsgiUtil.java oak-core/src/main/java/org/apache/jackrabbit/oak/osgi/OsgiUtil.java index cd9e332..4d60b85 100644 --- oak-core/src/main/java/org/apache/jackrabbit/oak/osgi/OsgiUtil.java +++ oak-core/src/main/java/org/apache/jackrabbit/oak/osgi/OsgiUtil.java @@ -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; } diff --git oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentNodeStoreService.java oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentNodeStoreService.java index 20f61dd..1799232 100644 --- oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentNodeStoreService.java +++ oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentNodeStoreService.java @@ -21,7 +21,7 @@ import static java.util.Collections.emptyMap; 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; @@ -273,7 +273,7 @@ public class SegmentNodeStoreService extends ProxyNodeStore @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"); @@ -284,7 +284,7 @@ public class SegmentNodeStoreService extends ProxyNodeStore 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) { @@ -305,56 +305,56 @@ public class SegmentNodeStoreService extends ProxyNodeStore 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); } - 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(); @@ -444,6 +444,10 @@ public class SegmentNodeStoreService extends ProxyNodeStore return true; } + private String property(String name) { + return lookupConfigurationThenFramework(context, name); + } + @Deactivate public void deactivate() { unregisterNodeStore(); diff --git oak-core/src/test/java/org/apache/jackrabbit/oak/osgi/OsgiUtilTest.java oak-core/src/test/java/org/apache/jackrabbit/oak/osgi/OsgiUtilTest.java index fb5c6f7..7546cc2 100644 --- oak-core/src/test/java/org/apache/jackrabbit/oak/osgi/OsgiUtilTest.java +++ oak-core/src/test/java/org/apache/jackrabbit/oak/osgi/OsgiUtilTest.java @@ -23,8 +23,9 @@ import org.osgi.service.component.ComponentContext; 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")); } }