Skip to content

Commit

Permalink
chore : Add a debug log statement for application config file locatio…
Browse files Browse the repository at this point in the history
…n while inferring ports

Add a debug log statement in generators of these frameworks while
resolving default web ports:
- Spring Boot
- Thorntail
- Helidon
- Quarkus
- Micronaut

Signed-off-by: Rohan Kumar <rohaan@redhat.com>
  • Loading branch information
rohanKanojia committed Sep 2, 2024
1 parent 301956d commit 7f2fa66
Show file tree
Hide file tree
Showing 41 changed files with 439 additions and 272 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import java.io.File;
import java.io.Serializable;
import java.net.URL;
import java.time.LocalDate;
import java.util.List;
import java.util.Locale;
Expand All @@ -38,14 +37,7 @@
public class JavaProject implements Serializable {

private static final long serialVersionUID = 6438404976521622633L;

/**
* Properties file for the project
*
* @param propertiesFile for the project.
* @return Properties file for the project.
*/
private URL propertiesFile;

/**
* Project's name.
*
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -98,21 +98,34 @@ public static Map<String, String> toMap(Properties properties) {
return map;
}

public static Properties fromApplicationConfig(JavaProject javaProject, String[] appConfigSources) {
public static URL fromApplicationConfigSource(JavaProject javaProject, String[] appConfigSources) {
final URLClassLoader urlClassLoader = getClassLoader(javaProject);
for (String source : appConfigSources) {
final Properties properties;
if (source.endsWith(".properties")) {
properties = getPropertiesFromResource(urlClassLoader.findResource(source));
} else {
properties = getPropertiesFromYamlResource(urlClassLoader.findResource(source));
}
URL appConfigSourceUrl = urlClassLoader.findResource(source);
final Properties properties = readPropertiesFromResource(appConfigSourceUrl);
// Consider only the first non-empty application config source
if (!properties.isEmpty()) {
properties.putAll(toMap(javaProject.getProperties()));
return properties;
return appConfigSourceUrl;
}
}
return null;
}

public static Properties readPropertiesFromResource(URL source) {
Properties properties = new Properties();
if (source != null && source.getFile().endsWith(".properties")) {
properties = getPropertiesFromResource(source);
} else if (source != null) {
properties = getPropertiesFromYamlResource(source);
}
return properties;
}

public static Properties mergeResourcePropertiesWithProjectProperties(Properties resourceProperties, JavaProject javaProject) {
if (resourceProperties != null && !resourceProperties.isEmpty()) {
resourceProperties.putAll(toMap(javaProject.getProperties()));
return resourceProperties;
}
return javaProject.getProperties();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import lombok.Builder;
import lombok.Getter;
import org.eclipse.jkube.kit.common.JavaProject;
import org.eclipse.jkube.kit.common.PropertiesExtender;

import java.util.Optional;
import java.util.Properties;
Expand All @@ -40,11 +39,9 @@ public class SpringBootConfiguration {
private boolean managementHealthProbesEnabled;

public static SpringBootConfiguration from(JavaProject project) {
final PropertiesExtender properties = SpringBootUtil.getSpringBootApplicationProperties(
final Properties properties = SpringBootUtil.getSpringBootApplicationProperties(
SpringBootUtil.getSpringBootActiveProfile(project),
JKubeProjectUtil.getClassLoader(project));
project.setPropertiesFile(properties.getPropertiesFile());

final int majorVersion = SpringBootUtil.getSpringBootVersion(project)
.map(semVer -> {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,21 @@
import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;

import org.eclipse.jkube.kit.common.JavaProject;
import org.eclipse.jkube.kit.common.KitLogger;
import org.eclipse.jkube.kit.common.Plugin;
import org.eclipse.jkube.kit.common.PropertiesExtender;

import static org.eclipse.jkube.kit.common.util.PropertiesUtil.fromApplicationConfigSource;
import static org.eclipse.jkube.kit.common.util.PropertiesUtil.getPropertiesFromResource;
import static org.eclipse.jkube.kit.common.util.PropertiesUtil.mergeResourcePropertiesWithProjectProperties;
import static org.eclipse.jkube.kit.common.util.PropertiesUtil.readPropertiesFromResource;

/**
* Utility methods to access spring-boot resources.
Expand All @@ -39,6 +44,7 @@ public class SpringBootUtil {
public static final String SPRING_BOOT_GRADLE_PLUGIN_ARTIFACT_ID = "org.springframework.boot.gradle.plugin";
public static final String DEV_TOOLS_REMOTE_SECRET = "spring.devtools.remote.secret";
public static final String DEV_TOOLS_REMOTE_SECRET_ENV = "SPRING_DEVTOOLS_REMOTE_SECRET";
private static final String[] SPRING_BOOT_APP_CONFIG_FILES_LIST = new String[] {"application.properties", "application.yml"};

private static final String SPRING_WEB_FLUX_ARTIFACT_ID = "spring-boot-starter-webflux";
private static final String PLACEHOLDER_PREFIX = "${";
Expand Down Expand Up @@ -66,22 +72,14 @@ public static Properties getSpringBootApplicationProperties(URLClassLoader compi
* @param compileClassLoader compile class loader
* @return properties object
*/
public static PropertiesExtender getSpringBootApplicationProperties(String springActiveProfile, URLClassLoader compileClassLoader) {
public static Properties getSpringBootApplicationProperties(String springActiveProfile, URLClassLoader compileClassLoader) {
URL ymlResource = compileClassLoader.findResource("application.yml");
URL propertiesResource = compileClassLoader.findResource("application.properties");

Properties props = YamlUtil.getPropertiesFromYamlResource(springActiveProfile, ymlResource);
props.putAll(getPropertiesFromResource(propertiesResource));
props = new SpringBootPropertyPlaceholderHelper(PLACEHOLDER_PREFIX, PLACEHOLDER_SUFFIX, VALUE_SEPARATOR, true)
.replaceAllPlaceholders(props);

// Extend Properties object with resources file path
PropertiesExtender propsExtender = new PropertiesExtender();
URL propertiesFile = ymlResource != null ? ymlResource : propertiesResource;
propsExtender.setPropertiesFile(propertiesFile);
propsExtender.putAll(props);

return propsExtender;
return new SpringBootPropertyPlaceholderHelper(PLACEHOLDER_PREFIX, PLACEHOLDER_SUFFIX, VALUE_SEPARATOR, true)
.replaceAllPlaceholders(props);
}

/**
Expand Down Expand Up @@ -163,5 +161,13 @@ public static File findNativeArtifactFile(JavaProject project) {
public static boolean hasSpringWebFluxDependency(JavaProject javaProject) {
return JKubeProjectUtil.hasDependency(javaProject, SPRING_BOOT_GROUP_ID, SPRING_WEB_FLUX_ARTIFACT_ID);
}

public static Properties resolveSpringBootApplicationConfigProperties(KitLogger log, JavaProject javaProject) {
URL propertySource = fromApplicationConfigSource(javaProject, SPRING_BOOT_APP_CONFIG_FILES_LIST);
if (propertySource != null) {
log.debug("Spring Boot Application Config loaded from : %s", propertySource);
}
return mergeResourcePropertiesWithProjectProperties(readPropertiesFromResource(propertySource), javaProject);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,35 @@
package org.eclipse.jkube.kit.common.util;

import org.eclipse.jkube.kit.common.JavaProject;
import org.eclipse.jkube.kit.common.KitLogger;

import java.net.URL;
import java.util.Properties;

import static org.eclipse.jkube.kit.common.util.PropertiesUtil.fromApplicationConfig;
import static org.eclipse.jkube.kit.common.util.PropertiesUtil.fromApplicationConfigSource;
import static org.eclipse.jkube.kit.common.util.PropertiesUtil.mergeResourcePropertiesWithProjectProperties;
import static org.eclipse.jkube.kit.common.util.PropertiesUtil.readPropertiesFromResource;


public class ThorntailUtil {
private static final String[] THORNTAIL_APP_CONFIG_FILES_LIST = new String[] {"project-defaults.yml"};
private static final String THORNTAIL_HTTP_PORT_PROPERTY = "thorntail.http.port";

private ThorntailUtil() {}

/**
* Returns the thorntail configuration (supports `project-defaults.yml`)
* or an empty properties object if not found
*
* @param javaProject Java Project
* @return thorntail configuration properties
*/
public static Properties getThorntailProperties(JavaProject javaProject) {
return fromApplicationConfig(javaProject, THORNTAIL_APP_CONFIG_FILES_LIST);
public static Properties resolveThorntailAppConfigProperties(KitLogger log, JavaProject javaProject) {
URL propertySource = fromApplicationConfigSource(javaProject, THORNTAIL_APP_CONFIG_FILES_LIST);
if (propertySource != null) {
log.info("Thorntail Application Config loaded from : %s", propertySource);
}
return mergeResourcePropertiesWithProjectProperties(readPropertiesFromResource(propertySource), javaProject);
}

public static String resolveThorntailWebPortFromThorntailConfig(Properties thorntailApplicationConfig) {
thorntailApplicationConfig.putAll(System.getProperties());
if (thorntailApplicationConfig.containsKey(THORNTAIL_HTTP_PORT_PROPERTY)) {
return (String) thorntailApplicationConfig.get(THORNTAIL_HTTP_PORT_PROPERTY);
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package org.eclipse.jkube.kit.common.util;

import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
Expand Down Expand Up @@ -123,8 +124,8 @@ void toMap_validProperties_shouldReturnValidMap() {
}

@Nested
@DisplayName("fromApplicationConfig")
class FromApplicationConfig {
@DisplayName("application configuration files in project classpath")
class ApplicationConfig {
@TempDir
private Path temporaryFolder;

Expand All @@ -139,53 +140,53 @@ void setUp() throws IOException {
.build();
}

@Test
@DisplayName("no source provided, return empty Properties")
void noSourceProvided_thenReturnEmptyProperty() {
// When
Properties properties = PropertiesUtil.fromApplicationConfig(javaProject, new String[0]);
// Then
assertThat(properties).isEmpty();
}

@Test
@DisplayName("application.yml source")
void yml() {
// When
Properties properties = PropertiesUtil.fromApplicationConfig(javaProject, new String[]{"application.yml"});
// Then
assertThat(properties).containsExactly(
entry("application.name", "name-via-yaml"));
}

@Test
@DisplayName("application.properties source")
void properties() {
// When
Properties properties = PropertiesUtil.fromApplicationConfig(javaProject, new String[]{"application.properties"});
// Then
assertThat(properties).containsExactly(
entry("application.name", "name-via-properties"));
}

@Test
@DisplayName("multiple sources provided, then first one takes precedence")
void multipleSources_thenFirstOneTakesPrecedence() {
// When
Properties properties = PropertiesUtil.fromApplicationConfig(javaProject, new String[]{"application.properties", "application.yml"});
// Then
assertThat(properties).containsExactly(
entry("application.name", "name-via-properties"));
}

@Test
@DisplayName("multiple sources provided, then first one takes precedence")
void multipleSourcesWithEmpty_thenFirstNonEmptyTakesPrecedence() {
// When
Properties properties = PropertiesUtil.fromApplicationConfig(javaProject, new String[]{"not-there", "application.yml", "application.properties"});
// Then
assertThat(properties).containsExactly(
entry("application.name", "name-via-yaml"));
@Nested
@DisplayName("fromApplicationConfigSource")
class FromApplicationConfigSource {
@Test
@DisplayName("no source provided, return empty Properties")
void noSourceProvided_thenReturnEmptyProperty() {
// When
URL source = PropertiesUtil.fromApplicationConfigSource(javaProject, new String[0]);
// Then
assertThat(source).isNull();
}

@Test
@DisplayName("application.yml source")
void yml() {
// When
URL source = PropertiesUtil.fromApplicationConfigSource(javaProject, new String[]{"application.yml"});
// Then
assertThat(source).isEqualTo(PropertiesUtil.class.getResource("/util/properties-util/yaml/application.yml"));
}

@Test
@DisplayName("application.properties source")
void properties() {
// When
URL source = PropertiesUtil.fromApplicationConfigSource(javaProject, new String[]{"application.properties"});
// Then
assertThat(source).isEqualTo(PropertiesUtil.class.getResource("/util/properties-util/properties/application.properties"));
}

@Test
@DisplayName("multiple sources provided, then first one takes precedence")
void multipleSources_thenFirstOneTakesPrecedence() {
// When
URL source = PropertiesUtil.fromApplicationConfigSource(javaProject, new String[]{"application.properties", "application.yml"});
// Then
assertThat(source).isEqualTo(PropertiesUtil.class.getResource("/util/properties-util/properties/application.properties"));
}

@Test
@DisplayName("multiple sources provided, then first one takes precedence")
void multipleSourcesWithEmpty_thenFirstNonEmptyTakesPrecedence() {
// When
URL source = PropertiesUtil.fromApplicationConfigSource(javaProject, new String[]{"not-there", "application.yml", "application.properties"});
// Then
assertThat(source).isEqualTo(PropertiesUtil.class.getResource("/util/properties-util/yaml/application.yml"));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ void setUp(@TempDir Path target) throws IOException {
project = JavaProject.builder()
.properties(properties)
.outputDirectory(target.toFile())
.baseDirectory(target.toFile())
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ void getSpringBootApplicationProperties_withCompileClassloader_shouldLoadPropert
JavaProject javaProject = JavaProject.builder()
.compileClassPathElement(Objects.requireNonNull(getClass().getResource("/util/springboot/resources")).getPath())
.outputDirectory(new File("target"))
.baseDirectory(new File("target"))
.build();
URLClassLoader compileClassLoader = JKubeProjectUtil.getClassLoader(javaProject);

Expand Down
Loading

0 comments on commit 7f2fa66

Please sign in to comment.