diff --git a/common/src/main/java/stirling/software/common/configuration/YamlConfig.java b/common/src/main/java/stirling/software/common/configuration/YamlConfig.java new file mode 100644 index 000000000..4da4dd8cc --- /dev/null +++ b/common/src/main/java/stirling/software/common/configuration/YamlConfig.java @@ -0,0 +1,44 @@ +package stirling.software.common.configuration; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import lombok.extern.slf4j.Slf4j; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.PropertySource; +import org.springframework.core.io.FileSystemResource; +import org.springframework.core.io.Resource; +import org.springframework.core.io.support.EncodedResource; + +@Slf4j +@Configuration +public class YamlConfig { + + @Bean + public PropertySource dynamicYamlPropertySource(ConfigurableEnvironment environment) + throws IOException { + String configPath = InstallationPathConfig.getSettingsPath(); + log.debug("Attempting to load settings from: " + configPath); + + File file = new File(configPath); + if (!file.exists()) { + log.error("Warning: Settings file does not exist at: " + configPath); + } + + Resource resource = new FileSystemResource(configPath); + if (!resource.exists()) { + throw new FileNotFoundException("Settings file not found at: " + configPath); + } + + EncodedResource encodedResource = new EncodedResource(resource); + PropertySource propertySource = + new YamlPropertySourceFactory().createPropertySource(null, encodedResource); + environment.getPropertySources().addFirst(propertySource); + + log.debug("Loaded properties: " + propertySource.getSource()); + + return propertySource; + } +} diff --git a/common/src/main/java/stirling/software/common/configuration/YamlPropertySourceFactory.java b/common/src/main/java/stirling/software/common/configuration/YamlPropertySourceFactory.java index 783d6d621..17ad21bfd 100644 --- a/common/src/main/java/stirling/software/common/configuration/YamlPropertySourceFactory.java +++ b/common/src/main/java/stirling/software/common/configuration/YamlPropertySourceFactory.java @@ -1,8 +1,6 @@ package stirling.software.common.configuration; -import java.io.IOException; import java.util.Properties; - import org.springframework.beans.factory.config.YamlPropertiesFactoryBean; import org.springframework.core.env.PropertiesPropertySource; import org.springframework.core.env.PropertySource; @@ -12,8 +10,7 @@ import org.springframework.core.io.support.PropertySourceFactory; public class YamlPropertySourceFactory implements PropertySourceFactory { @Override - public PropertySource createPropertySource(String name, EncodedResource encodedResource) - throws IOException { + public PropertySource createPropertySource(String name, EncodedResource encodedResource) { YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); factory.setResources(encodedResource.getResource()); Properties properties = factory.getObject(); diff --git a/src/test/java/stirling/software/SPDF/service/PdfMetadataServiceBasicTest.java b/src/test/java/stirling/software/SPDF/service/PdfMetadataServiceBasicTest.java index ab4e846cc..f09156ca8 100644 --- a/src/test/java/stirling/software/SPDF/service/PdfMetadataServiceBasicTest.java +++ b/src/test/java/stirling/software/SPDF/service/PdfMetadataServiceBasicTest.java @@ -19,6 +19,7 @@ import stirling.software.common.model.ApplicationProperties.Premium; import stirling.software.common.model.ApplicationProperties.Premium.ProFeatures; import stirling.software.common.model.ApplicationProperties.Premium.ProFeatures.CustomMetadata; import stirling.software.common.model.PdfMetadata; +import stirling.software.common.service.PdfMetadataService; import stirling.software.common.service.UserServiceInterface; class PdfMetadataServiceBasicTest {