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 kw...@apache.org on 2025/04/28 15:54:53 UTC
(jackrabbit-oak) 01/01: OAK-11686: Provide method to lookup value from OSGi framework property without passing BundleContext
This is an automated email from the ASF dual-hosted git repository.
kwin pushed a commit to branch feature/OAK-11686-lookup-from-framework-properties-without-passing-bundle-context
in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git
commit c4fc5169304745940f13c699719364563f0d0a0f
Author: Konrad Windszus <kw...@apache.org>
AuthorDate: Mon Apr 28 17:54:41 2025 +0200
OAK-11686: Provide method to lookup value from OSGi framework property
without passing BundleContext
This falls back to using System properties when running outside OSGi
---
.../org/apache/jackrabbit/oak/osgi/OsgiUtil.java | 22 +++++++++++++++++
.../apache/jackrabbit/oak/osgi/OsgiUtilTest.java | 28 ++++++++++++++++++++++
2 files changed, 50 insertions(+)
diff --git a/oak-core-spi/src/main/java/org/apache/jackrabbit/oak/osgi/OsgiUtil.java b/oak-core-spi/src/main/java/org/apache/jackrabbit/oak/osgi/OsgiUtil.java
index 67f9b2037b..6e44e0d9a2 100644
--- a/oak-core-spi/src/main/java/org/apache/jackrabbit/oak/osgi/OsgiUtil.java
+++ b/oak-core-spi/src/main/java/org/apache/jackrabbit/oak/osgi/OsgiUtil.java
@@ -17,6 +17,7 @@
package org.apache.jackrabbit.oak.osgi;
+import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
import org.osgi.framework.Filter;
@@ -36,6 +37,27 @@ public class OsgiUtil {
// Prevent instantiation.
}
+ /**
+ * Looks a property up by name in the set of framework properties if running inside an OSGi runtime,
+ * otherwise tries to look it up in from the system properties.
+ * Returns {@code null} if the property is not found or if the property is found but
+ * it is an empty string.
+ *
+ * @param name Name of the property.
+ * @return The property value serialized as a string, or {@code null}.
+ */
+ public static String lookup(String name) {
+ Objects.requireNonNull(name);
+ Bundle bundle = FrameworkUtil.getBundle(OsgiUtil.class);
+ final String value;
+ if (bundle == null) {
+ value = System.getProperty(name);
+ } else {
+ value = Objects.requireNonNull(bundle.getBundleContext()).getProperty(name);
+ }
+ return asString(value);
+ }
+
/**
* Looks a property up by name in a component context. Returns {@code null}
* if the property is not found or if the property is found but it is an
diff --git a/oak-core-spi/src/test/java/org/apache/jackrabbit/oak/osgi/OsgiUtilTest.java b/oak-core-spi/src/test/java/org/apache/jackrabbit/oak/osgi/OsgiUtilTest.java
index 9c82bf11ad..f145e94197 100644
--- a/oak-core-spi/src/test/java/org/apache/jackrabbit/oak/osgi/OsgiUtilTest.java
+++ b/oak-core-spi/src/test/java/org/apache/jackrabbit/oak/osgi/OsgiUtilTest.java
@@ -234,4 +234,32 @@ public class OsgiUtilTest {
assertEquals(FrameworkUtil.createFilter("(&(objectClass=java.lang.String)(foo=bar)(!(empty=*))(escaped=\\*xyz\\)))"), getFilter(String.class, m));
b.setLength(0);
}
+
+ @Test
+ public void testLookupWithoutOsgiRuntime() {
+ String oldValue = System.setProperty("name", "value");
+ try {
+ assertEquals("value", lookup("name"));
+ } finally {
+ if (oldValue == null) {
+ System.clearProperty("name");
+ } else {
+ System.setProperty("name", oldValue);
+ }
+ }
+ }
+
+ @Test
+ public void testLookupEmptyStringWithoutOsgiRuntime() {
+ String oldValue = System.setProperty("name", "");
+ try {
+ assertNull(lookup("name"));
+ } finally {
+ if (oldValue == null) {
+ System.clearProperty("name");
+ } else {
+ System.setProperty("name", oldValue);
+ }
+ }
+ }
}