1. import java.io.File;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. public class ResourceLoader {
  5. private final Map<String, String> resources = new HashMap<>();
  6. /**
  7. * Loads resources from directories based on optional flags.
  8. *
  9. * @param baseDir The base directory to start searching from.
  10. * @param flags Optional flags to enable/disable specific resource directories.
  11. */
  12. public void loadResources(String baseDir, String flags) {
  13. File dir = new File(baseDir);
  14. if (!dir.exists() || !dir.isDirectory()) {
  15. System.err.println("Error: Base directory does not exist or is not a directory: " + baseDir);
  16. return; //Or throw exception if appropriate
  17. }
  18. File[] files = dir.listFiles();
  19. if (files != null) {
  20. for (File file : files) {
  21. if (file.isDirectory()) {
  22. String dirName = file.getName();
  23. boolean include = true;
  24. // Check if the flag disables the directory
  25. if (flags != null && flags.contains("-" + dirName)) {
  26. include = false;
  27. }
  28. if (include) {
  29. loadResourcesFromDirectory(file);
  30. }
  31. }
  32. }
  33. }
  34. }
  35. private void loadResourcesFromDirectory(File directory) {
  36. File[] files = directory.listFiles();
  37. if (files != null) {
  38. for (File file : files) {
  39. if (file.isFile() && file.getName().endsWith(".properties")) { //Or other resource types
  40. String resourceName = file.getName().substring(0, file.getName().lastIndexOf(".")); //Remove extension
  41. try {
  42. loadProperties(file, resourceName);
  43. } catch (Exception e) {
  44. System.err.println("Error loading resource: " + resourceName + " - " + e.getMessage());
  45. }
  46. }
  47. }
  48. }
  49. }
  50. private void loadProperties(File file, String resourceName) {
  51. //Load properties from the file
  52. try {
  53. Map<String, String> properties = loadPropertiesFile(file);
  54. resources.put(resourceName, properties.toString());
  55. } catch (Exception e) {
  56. System.err.println("Error loading properties file: " + file.getName() + " - " + e.getMessage());
  57. }
  58. }
  59. private Map<String, String> loadPropertiesFile(File file) throws Exception {
  60. //Placeholder for loading the properties file.
  61. //Replace this with your actual properties loading logic.
  62. //For example using org.apache.commons.lang3.SystemUtils
  63. //This is just a dummy implementation
  64. Map<String, String> properties = new HashMap<>();
  65. properties.put("key1", "value1");
  66. properties.put("key2", "value2");
  67. return properties;
  68. }
  69. /**
  70. * Returns the loaded resources.
  71. *
  72. * @return A map of resource names to their content.
  73. */
  74. public Map<String, String> getResources() {
  75. return resources;
  76. }
  77. public static void main(String[] args) {
  78. ResourceLoader loader = new ResourceLoader();
  79. // Example usage:
  80. // Assuming you have directories 'default', 'optional1', 'optional2'
  81. // and properties files in each directory.
  82. // You can specify flags like "-optional1 -optional2" to enable specific directories.
  83. loader.loadResources("resources", "-optional1"); //Load resources from "resources" directory, excluding "optional1"
  84. Map<String, String> loadedResources = loader.getResources();
  85. System.out.println(loadedResources);
  86. }
  87. }

Add your comment