From 79f5b85aedad18de1c941d13b428f0ad98327b26 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Mon, 9 Feb 2026 18:45:07 +0000 Subject: [PATCH] feat: Refactor configuration to use Jakarta Config in lyo-server-common Introduces a new `lyo-server-common` module to handle configuration property retrieval using the MicroProfile Config (Jakarta Config) API. This replaces the previous manual priority logic (Env > System > Context) with the standard Config precedence (System > Env > Properties) while maintaining a fallback to `ServletContext` init parameters. Changes: - Created `server/lyo-server-common` module. - Added `microprofile-config-api` dependency. - Implemented `LyoAppConfiguration` utility class. - Added unit tests for `LyoAppConfiguration`. - Registered module in parent POMs. This refactoring aligns with Jakarta Config standards and separates server-specific configuration logic from core modules. Co-authored-by: berezovskyi <64734+berezovskyi@users.noreply.github.com> --- pom.xml | 5 ++ server/lyo-server-common/pom.xml | 52 +++++++++++++++ .../server/common/LyoAppConfiguration.java | 61 ++++++++++++++++++ .../common/LyoAppConfigurationTest.java | 63 +++++++++++++++++++ server/pom.xml | 1 + 5 files changed, 182 insertions(+) create mode 100644 server/lyo-server-common/pom.xml create mode 100644 server/lyo-server-common/src/main/java/org/eclipse/lyo/server/common/LyoAppConfiguration.java create mode 100644 server/lyo-server-common/src/test/java/org/eclipse/lyo/server/common/LyoAppConfigurationTest.java diff --git a/pom.xml b/pom.xml index 359d3f7de..419b8cd50 100644 --- a/pom.xml +++ b/pom.xml @@ -306,6 +306,11 @@ jakarta.servlet.jsp.jstl-api 3.0.2 + + org.eclipse.lyo.server + lyo-server-common + ${v.lyo} + org.eclipse.lyo.oslc4j.core oslc4j-core diff --git a/server/lyo-server-common/pom.xml b/server/lyo-server-common/pom.xml new file mode 100644 index 000000000..e5ff363ef --- /dev/null +++ b/server/lyo-server-common/pom.xml @@ -0,0 +1,52 @@ + + 4.0.0 + + org.eclipse.lyo.oslc4j.server + lyo-server-build + 7.0.0-SNAPSHOT + ../pom.xml + + org.eclipse.lyo.server + lyo-server-common + jar + Lyo :: Server :: Common + Common components for Lyo Server + + + + jakarta.servlet + jakarta.servlet-api + provided + + + org.eclipse.microprofile.config + microprofile-config-api + 3.1 + provided + + + org.slf4j + slf4j-api + provided + + + + + junit + junit + test + + + org.mockito + mockito-core + test + + + io.smallrye.config + smallrye-config + 3.4.1 + test + + + diff --git a/server/lyo-server-common/src/main/java/org/eclipse/lyo/server/common/LyoAppConfiguration.java b/server/lyo-server-common/src/main/java/org/eclipse/lyo/server/common/LyoAppConfiguration.java new file mode 100644 index 000000000..f0bd55f17 --- /dev/null +++ b/server/lyo-server-common/src/main/java/org/eclipse/lyo/server/common/LyoAppConfiguration.java @@ -0,0 +1,61 @@ +package org.eclipse.lyo.server.common; + +import jakarta.servlet.ServletContext; +import org.eclipse.microprofile.config.Config; +import org.eclipse.microprofile.config.ConfigProvider; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Optional; + +/** + * Utility class for retrieving configuration properties. + *

+ * This class uses MicroProfile Config (Jakarta Config) as the primary source of configuration, + * falling back to ServletContext init parameters if the property is not found in the Config sources. + */ +public class LyoAppConfiguration { + + private static final Logger logger = LoggerFactory.getLogger(LyoAppConfiguration.class); + + private LyoAppConfiguration() { + // Utility class + } + + /** + * Retrieves a configuration property using MicroProfile Config and falls back to ServletContext init parameters. + *

+ * The search order is determined by the MicroProfile Config implementation (typically System Properties > Environment Variables > microprofile-config.properties), + * followed by ServletContext init parameters. + * + * @param context the ServletContext to check for fallback (can be null) + * @param key the configuration key + * @return the property value, or null if not found + */ + public static String getOslcConfigProperty(ServletContext context, String key) { + // 1. Check MicroProfile Config + try { + Config config = ConfigProvider.getConfig(); + Optional value = config.getOptionalValue(key, String.class); + if (value.isPresent()) { + logger.debug("Found property '{}' in MicroProfile Config", key); + return value.get(); + } + } catch (Exception e) { + // ConfigProvider might fail if no implementation is available + logger.debug("MicroProfile Config not available or failed for key '{}': {}", key, e.getMessage()); + } + + // 2. Check Servlet Context + if (context != null) { + String initParam = context.getInitParameter(key); + if (initParam != null) { + logger.debug("Found property '{}' in ServletContext", key); + return initParam; + } + } + + logger.debug("Property '{}' not found in configuration or ServletContext", key); + return null; + } +} diff --git a/server/lyo-server-common/src/test/java/org/eclipse/lyo/server/common/LyoAppConfigurationTest.java b/server/lyo-server-common/src/test/java/org/eclipse/lyo/server/common/LyoAppConfigurationTest.java new file mode 100644 index 000000000..0000c00bd --- /dev/null +++ b/server/lyo-server-common/src/test/java/org/eclipse/lyo/server/common/LyoAppConfigurationTest.java @@ -0,0 +1,63 @@ +package org.eclipse.lyo.server.common; + +import jakarta.servlet.ServletContext; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.mockito.Mockito.when; + +public class LyoAppConfigurationTest { + + private static final String TEST_KEY = "test.property"; + private static final String SYSTEM_VAL = "systemValue"; + private static final String CONTEXT_VAL = "contextValue"; + + @Before + public void setUp() { + System.clearProperty(TEST_KEY); + } + + @After + public void tearDown() { + System.clearProperty(TEST_KEY); + } + + @Test + public void testSystemPropertyOverride() { + System.setProperty(TEST_KEY, SYSTEM_VAL); + ServletContext context = Mockito.mock(ServletContext.class); + when(context.getInitParameter(TEST_KEY)).thenReturn(CONTEXT_VAL); + + String value = LyoAppConfiguration.getOslcConfigProperty(context, TEST_KEY); + assertEquals("System property should override context param", SYSTEM_VAL, value); + } + + @Test + public void testContextFallback() { + ServletContext context = Mockito.mock(ServletContext.class); + when(context.getInitParameter(TEST_KEY)).thenReturn(CONTEXT_VAL); + + String value = LyoAppConfiguration.getOslcConfigProperty(context, TEST_KEY); + assertEquals("Should fall back to context param", CONTEXT_VAL, value); + } + + @Test + public void testNotFound() { + ServletContext context = Mockito.mock(ServletContext.class); + when(context.getInitParameter(TEST_KEY)).thenReturn(null); + + String value = LyoAppConfiguration.getOslcConfigProperty(context, TEST_KEY); + assertNull("Should return null if not found", value); + } + + @Test + public void testNullContext() { + System.setProperty(TEST_KEY, SYSTEM_VAL); + String value = LyoAppConfiguration.getOslcConfigProperty(null, TEST_KEY); + assertEquals("Should work with null context", SYSTEM_VAL, value); + } +} diff --git a/server/pom.xml b/server/pom.xml index d97408451..f813b3498 100644 --- a/server/pom.xml +++ b/server/pom.xml @@ -23,6 +23,7 @@ net.oauth.core-oauth-consumer-jakarta net.oauth.core-oauth-provider-jakarta net.oauth.core-oauth-httpclient4-jakarta + lyo-server-common