diff --git a/exist-core/src/main/java/org/exist/util/Configuration.java b/exist-core/src/main/java/org/exist/util/Configuration.java index bbead109e73..d4b8e3b4adc 100644 --- a/exist-core/src/main/java/org/exist/util/Configuration.java +++ b/exist-core/src/main/java/org/exist/util/Configuration.java @@ -263,61 +263,64 @@ public Configuration(@Nullable String configFilename, Optional existHomeDi configFilename = DatabaseImpl.CONF_XML; } - // firstly, try to read the configuration from a file within the - // classpath - try { - is = Configuration.class.getClassLoader().getResourceAsStream(configFilename); - - if (is != null) { - LOG.info("Reading configuration from classloader"); - configFilePath = Optional.of(Path.of(Configuration.class.getClassLoader().getResource(configFilename).toURI())); - } - } catch (final Exception e) { - // EB: ignore and go forward, e.g. in case there is an absolute - // file name for configFileName - LOG.debug(e); - } - existHomeDirname = existHomeDirname.map(Path::normalize); - // otherwise, secondly try to read configuration from file. Guess the + // firstly, try to read configuration from file. Guess the // location if necessary - if (is == null) { - existHome = existHomeDirname.map(Optional::of) - .orElse(ConfigurationHelper.getExistHome(configFilename)); + existHome = existHomeDirname.map(Optional::of) + .orElse(ConfigurationHelper.getExistHome(configFilename)); - if (existHome.isEmpty()) { + if (existHome.isEmpty()) { - // EB: try to create existHome based on location of config file - // when config file points to absolute file location - final Path absoluteConfigFile = Path.of(configFilename); + // EB: try to create existHome based on location of config file + // when config file points to absolute file location + final Path absoluteConfigFile = Path.of(configFilename); - if (absoluteConfigFile.isAbsolute() && Files.exists(absoluteConfigFile) && Files.isReadable(absoluteConfigFile)) { - existHome = Optional.of(absoluteConfigFile.getParent()); - configFilename = FileUtils.fileName(absoluteConfigFile); - } + if (absoluteConfigFile.isAbsolute() && Files.exists(absoluteConfigFile) && Files.isReadable(absoluteConfigFile)) { + existHome = Optional.of(absoluteConfigFile.getParent()); + configFilename = FileUtils.fileName(absoluteConfigFile); } + } - Path configFile = Path.of(configFilename); - - if (!configFile.isAbsolute() && existHome.isPresent()) { + Path configFile = Path.of(configFilename); - // try the passed or constructed existHome first - configFile = existHome.get().resolve(configFilename); + if (!configFile.isAbsolute() && existHome.isPresent()) { - if (!Files.exists(configFile)) { - configFile = existHome.get().resolve(Main.CONFIG_DIR_NAME).resolve(configFilename); - } - } + // try the passed or constructed existHome first + configFile = existHome.get().resolve(configFilename); - if (!Files.exists(configFile) || !Files.isReadable(configFile)) { - throw new DatabaseConfigurationException("Unable to read configuration file at " + configFile); + if (!Files.exists(configFile)) { + configFile = existHome.get().resolve(Main.CONFIG_DIR_NAME).resolve(configFilename); } + } + if (Files.exists(configFile) && Files.isReadable(configFile)) { configFilePath = Optional.of(configFile.toAbsolutePath()); is = Files.newInputStream(configFile); } + // otherwise, secondly try to read the configuration from a file within the + // classpath + if (is == null) { + try { + is = Configuration.class.getClassLoader().getResourceAsStream(configFilename); + + if (is != null) { + LOG.info("Reading configuration from classloader"); + configFilePath = Optional.of(Path.of(Configuration.class.getClassLoader().getResource(configFilename).toURI())); + existHome = configFilePath.map(p -> p.getParent().getParent()); + } + } catch (final Exception e) { + // EB: ignore and go forward, e.g. in case there is an absolute + // file name for configFileName + LOG.debug("Error reading configuration from classloader: {}", e.getMessage(), e); + } + } + + if (is == null) { + throw new DatabaseConfigurationException("Unable to read configuration file at " + configFile); + } + LOG.info("Reading configuration from file {}", configFilePath.map(Path::toString).orElse("Unknown")); // set dbHome to parent of the conf file found, to resolve relative