Skip to content

Commit 919ea6c

Browse files
committed
feat: Refactor configuration to use Jakarta Config in lyo-server-common
1 parent f8a2028 commit 919ea6c

5 files changed

Lines changed: 182 additions & 0 deletions

File tree

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,11 @@
275275
<artifactId>jakarta.servlet.jsp.jstl-api</artifactId>
276276
<version>3.0.2</version>
277277
</dependency>
278+
<dependency>
279+
<groupId>org.eclipse.lyo.server</groupId>
280+
<artifactId>lyo-server-common</artifactId>
281+
<version>${v.lyo}</version>
282+
</dependency>
278283
<dependency>
279284
<groupId>org.eclipse.lyo.oslc4j.core</groupId>
280285
<artifactId>oslc4j-core</artifactId>

server/lyo-server-common/pom.xml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>org.eclipse.lyo.oslc4j.server</groupId>
6+
<artifactId>lyo-server-build</artifactId>
7+
<version>7.0.0-SNAPSHOT</version>
8+
<relativePath>../pom.xml</relativePath>
9+
</parent>
10+
<groupId>org.eclipse.lyo.server</groupId>
11+
<artifactId>lyo-server-common</artifactId>
12+
<packaging>jar</packaging>
13+
<name>Lyo :: Server :: Common</name>
14+
<description>Common components for Lyo Server</description>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>jakarta.servlet</groupId>
19+
<artifactId>jakarta.servlet-api</artifactId>
20+
<scope>provided</scope>
21+
</dependency>
22+
<dependency>
23+
<groupId>org.eclipse.microprofile.config</groupId>
24+
<artifactId>microprofile-config-api</artifactId>
25+
<version>3.1</version>
26+
<scope>provided</scope>
27+
</dependency>
28+
<dependency>
29+
<groupId>org.slf4j</groupId>
30+
<artifactId>slf4j-api</artifactId>
31+
<scope>provided</scope>
32+
</dependency>
33+
34+
<!-- Test -->
35+
<dependency>
36+
<groupId>junit</groupId>
37+
<artifactId>junit</artifactId>
38+
<scope>test</scope>
39+
</dependency>
40+
<dependency>
41+
<groupId>org.mockito</groupId>
42+
<artifactId>mockito-core</artifactId>
43+
<scope>test</scope>
44+
</dependency>
45+
<dependency>
46+
<groupId>io.smallrye.config</groupId>
47+
<artifactId>smallrye-config</artifactId>
48+
<version>3.4.1</version>
49+
<scope>test</scope>
50+
</dependency>
51+
</dependencies>
52+
</project>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package org.eclipse.lyo.server.common;
2+
3+
import jakarta.servlet.ServletContext;
4+
import org.eclipse.microprofile.config.Config;
5+
import org.eclipse.microprofile.config.ConfigProvider;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
9+
import java.util.Optional;
10+
11+
/**
12+
* Utility class for retrieving configuration properties.
13+
* <p>
14+
* This class uses MicroProfile Config (Jakarta Config) as the primary source of configuration,
15+
* falling back to ServletContext init parameters if the property is not found in the Config sources.
16+
*/
17+
public class LyoAppConfiguration {
18+
19+
private static final Logger logger = LoggerFactory.getLogger(LyoAppConfiguration.class);
20+
21+
private LyoAppConfiguration() {
22+
// Utility class
23+
}
24+
25+
/**
26+
* Retrieves a configuration property using MicroProfile Config and falls back to ServletContext init parameters.
27+
* <p>
28+
* The search order is determined by the MicroProfile Config implementation (typically System Properties > Environment Variables > microprofile-config.properties),
29+
* followed by ServletContext init parameters.
30+
*
31+
* @param context the ServletContext to check for fallback (can be null)
32+
* @param key the configuration key
33+
* @return the property value, or null if not found
34+
*/
35+
public static String getOslcConfigProperty(ServletContext context, String key) {
36+
// 1. Check MicroProfile Config
37+
try {
38+
Config config = ConfigProvider.getConfig();
39+
Optional<String> value = config.getOptionalValue(key, String.class);
40+
if (value.isPresent()) {
41+
logger.debug("Found property '{}' in MicroProfile Config", key);
42+
return value.get();
43+
}
44+
} catch (Exception e) {
45+
// ConfigProvider might fail if no implementation is available
46+
logger.debug("MicroProfile Config not available or failed for key '{}': {}", key, e.getMessage());
47+
}
48+
49+
// 2. Check Servlet Context
50+
if (context != null) {
51+
String initParam = context.getInitParameter(key);
52+
if (initParam != null) {
53+
logger.debug("Found property '{}' in ServletContext", key);
54+
return initParam;
55+
}
56+
}
57+
58+
logger.debug("Property '{}' not found in configuration or ServletContext", key);
59+
return null;
60+
}
61+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package org.eclipse.lyo.server.common;
2+
3+
import jakarta.servlet.ServletContext;
4+
import org.junit.After;
5+
import org.junit.Before;
6+
import org.junit.Test;
7+
import org.mockito.Mockito;
8+
9+
import static org.junit.Assert.assertEquals;
10+
import static org.junit.Assert.assertNull;
11+
import static org.mockito.Mockito.when;
12+
13+
public class LyoAppConfigurationTest {
14+
15+
private static final String TEST_KEY = "test.property";
16+
private static final String SYSTEM_VAL = "systemValue";
17+
private static final String CONTEXT_VAL = "contextValue";
18+
19+
@Before
20+
public void setUp() {
21+
System.clearProperty(TEST_KEY);
22+
}
23+
24+
@After
25+
public void tearDown() {
26+
System.clearProperty(TEST_KEY);
27+
}
28+
29+
@Test
30+
public void testSystemPropertyOverride() {
31+
System.setProperty(TEST_KEY, SYSTEM_VAL);
32+
ServletContext context = Mockito.mock(ServletContext.class);
33+
when(context.getInitParameter(TEST_KEY)).thenReturn(CONTEXT_VAL);
34+
35+
String value = LyoAppConfiguration.getOslcConfigProperty(context, TEST_KEY);
36+
assertEquals("System property should override context param", SYSTEM_VAL, value);
37+
}
38+
39+
@Test
40+
public void testContextFallback() {
41+
ServletContext context = Mockito.mock(ServletContext.class);
42+
when(context.getInitParameter(TEST_KEY)).thenReturn(CONTEXT_VAL);
43+
44+
String value = LyoAppConfiguration.getOslcConfigProperty(context, TEST_KEY);
45+
assertEquals("Should fall back to context param", CONTEXT_VAL, value);
46+
}
47+
48+
@Test
49+
public void testNotFound() {
50+
ServletContext context = Mockito.mock(ServletContext.class);
51+
when(context.getInitParameter(TEST_KEY)).thenReturn(null);
52+
53+
String value = LyoAppConfiguration.getOslcConfigProperty(context, TEST_KEY);
54+
assertNull("Should return null if not found", value);
55+
}
56+
57+
@Test
58+
public void testNullContext() {
59+
System.setProperty(TEST_KEY, SYSTEM_VAL);
60+
String value = LyoAppConfiguration.getOslcConfigProperty(null, TEST_KEY);
61+
assertEquals("Should work with null context", SYSTEM_VAL, value);
62+
}
63+
}

server/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<module>net.oauth.core-oauth-consumer-jakarta</module>
2424
<module>net.oauth.core-oauth-provider-jakarta</module>
2525
<module>net.oauth.core-oauth-httpclient4-jakarta</module>
26+
<module>lyo-server-common</module>
2627
</modules>
2728

2829
<dependencyManagement>

0 commit comments

Comments
 (0)