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:52 UTC

(jackrabbit-oak) branch feature/OAK-11686-lookup-from-framework-properties-without-passing-bundle-context created (now c4fc516930)

This is an automated email from the ASF dual-hosted git repository.

kwin pushed a change to branch feature/OAK-11686-lookup-from-framework-properties-without-passing-bundle-context
in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git


      at c4fc516930 OAK-11686: Provide method to lookup value from OSGi framework property without passing BundleContext

This branch includes the following new commits:

     new c4fc516930 OAK-11686: Provide method to lookup value from OSGi framework property without passing BundleContext

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



(jackrabbit-oak) 01/01: OAK-11686: Provide method to lookup value from OSGi framework property without passing BundleContext

Posted by kw...@apache.org.
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);
+            }
+        }
+    }
 }