Skip to content

Conversation

@ggallotti
Copy link
Member

Support Reading Services Configuration from File System and Classpath

Issue

When deploying the application as a JAR (rather than a web application), it currently expects the service configuration files (SERVICES_DEV_FILE and SERVICES_FILE) to be available only in the file system. If these files are missing, the application fails to load the configurations instead of checking if they exist in the JAR’s classpath.

Additionally, if SERVICES_DEV_FILE is found, the execution stops without attempting to load SERVICES_FILE, which may contain additional configurations.

Solution

This PR enhances the configuration loading mechanism to:

  • Support reading from both the file system and the JAR classpath using ClassLoader.getResourceAsStream.
  • Ensure SERVICES_FILE is always attempted, even if SERVICES_DEV_FILE is successfully loaded.
  • Improve error handling by logging detailed messages when files fail to load.

Changes Made

  1. Added readFromClasspath method:

    • Uses ClassLoader.getResourceAsStream to attempt reading files from the JAR classpath if they are missing in the file system.
  2. Modified readServices method:

    • Attempts to read SERVICES_DEV_FILE from both file system and classpath.
    • Ensures SERVICES_FILE is always attempted after SERVICES_DEV_FILE, regardless of whether the first file was found or not.
  3. Refactored loadFromFile to loadFromStream:

    • Introduced loadFromStream to handle both file-based and classpath-based input streams.
    • Used try-with-resources to properly close input streams.
  4. Improved error logging:

    • Logs whether a file is missing in the file system or classpath.
    • Logs error details when reading from the classpath fails.

Expected Behavior After Merge

✔ If SERVICES_DEV_FILE is found, it loads but does not prevent SERVICES_FILE from loading.
✔ If SERVICES_FILE is missing in the file system, it will still be attempted from the JAR classpath.
✔ Ensures configurations are correctly loaded even when packaged inside a JAR.
✔ Eliminates failures caused by missing configuration files in JAR deployments.

Testing

  • Case 1: SERVICES_DEV_FILE and SERVICES_FILE exist in the file system → ✅ Both should load.
  • Case 2: SERVICES_DEV_FILE exists, SERVICES_FILE is missing → ✅ Loads SERVICES_DEV_FILE, tries SERVICES_FILE in the classpath.
  • Case 3: Neither file exists in the file system → ✅ Attempts to load both from the classpath.
  • Case 4: Files exist only in the classpath → ✅ Reads from the JAR successfully.

Impact

  • Ensures compatibility with JAR deployments by allowing configuration files inside JARs.
  • Prevents unexpected failures when files are not available in the local file system.
  • Improves flexibility by ensuring configurations are loaded correctly in all deployment scenarios.

🚀 Ready for review!

@ggallotti ggallotti merged commit 823979b into release-4.1 Feb 19, 2025
4 checks passed
@ggallotti ggallotti deleted the cloudservice-classloader branch February 19, 2025 20:37
@ggallotti ggallotti restored the cloudservice-classloader branch February 20, 2025 18:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants