When using the ConfigLoader class in the Simple Java Mail library, environment variables are not being loaded unless a properties file (simplejavamail.properties) is present on the classpath. This issue prevents configuration via environment variables in environments where properties files are not used or cannot be placed on the classpath, such as containerized applications or cloud deployments.
Steps to Reproduce:
Ensure there is no simplejavamail.properties file on the classpath.
Set environment variables corresponding to configuration properties. For example:
Run the application that utilizes the ConfigLoader to load configuration properties.
Expected Behavior:
The ConfigLoader should load configuration properties from environment variables even when the properties file is absent.
Actual Behavior:
The ConfigLoader does not load properties from environment variables unless the properties file is found on the classpath. As a result, the application cannot be configured using environment variables alone.
Cause:
In the loadProperties(String filename, boolean addProperties) method of the ConfigLoader class, when the properties file is not found, the method returns without attempting to load environment variables or system properties. The environment variables are only considered when a properties file is successfully loaded, which is not the intended behavior.
Suggested Fix:
Modify the loadProperties method to ensure that system properties and environment variables are always loaded, regardless of whether the properties file exists. This can be achieved by passing an empty Properties object to the loadProperties(Properties properties, boolean addProperties) method when the properties file is not found.
Additional Information:
This issue affects users who rely on environment variables for configuration, especially in containerized environments like Docker or Kubernetes.
The priority of property resolution should be:
System Properties (System.getProperty)
Environment Variables (System.getenv)
Properties File (simplejavamail.properties)
Ensuring that environment variables are loaded correctly enhances the flexibility and usability of the library in various deployment scenarios.
When using the
ConfigLoader
class in the Simple Java Mail library, environment variables are not being loaded unless a properties file (simplejavamail.properties
) is present on the classpath. This issue prevents configuration via environment variables in environments where properties files are not used or cannot be placed on the classpath, such as containerized applications or cloud deployments.Steps to Reproduce:
Ensure there is no
simplejavamail.properties
file on the classpath. Set environment variables corresponding to configuration properties. For example:Run the application that utilizes the
ConfigLoader
to load configuration properties. Expected Behavior:The
ConfigLoader
should load configuration properties from environment variables even when the properties file is absent.Actual Behavior:
The
ConfigLoader
does not load properties from environment variables unless the properties file is found on the classpath. As a result, the application cannot be configured using environment variables alone.Cause:
In the
loadProperties(String filename, boolean addProperties)
method of theConfigLoader
class, when the properties file is not found, the method returns without attempting to load environment variables or system properties. The environment variables are only considered when a properties file is successfully loaded, which is not the intended behavior.Suggested Fix:
Modify the
loadProperties
method to ensure that system properties and environment variables are always loaded, regardless of whether the properties file exists. This can be achieved by passing an emptyProperties
object to theloadProperties(Properties properties, boolean addProperties)
method when the properties file is not found.Additional Information:
System.getProperty
)System.getenv
)simplejavamail.properties
)