Skip to content

Commit 5b42d4b

Browse files
committed
[bugfix] Swap load order as from classpath should not happen?
1 parent 891b3b6 commit 5b42d4b

1 file changed

Lines changed: 41 additions & 39 deletions

File tree

exist-core/src/main/java/org/exist/util/Configuration.java

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -263,62 +263,64 @@ public Configuration(@Nullable String configFilename, Optional<Path> existHomeDi
263263
configFilename = DatabaseImpl.CONF_XML;
264264
}
265265

266-
// firstly, try to read the configuration from a file within the
267-
// classpath
268-
try {
269-
is = Configuration.class.getClassLoader().getResourceAsStream(configFilename);
270-
271-
if (is != null) {
272-
LOG.info("Reading configuration from classloader");
273-
configFilePath = Optional.of(Path.of(Configuration.class.getClassLoader().getResource(configFilename).toURI()));
274-
existHome = configFilePath.map(p -> p.getParent().getParent());
275-
}
276-
} catch (final Exception e) {
277-
// EB: ignore and go forward, e.g. in case there is an absolute
278-
// file name for configFileName
279-
LOG.debug("Error reading configuration from classloader: {}", e.getMessage(), e);
280-
}
281-
282266
existHomeDirname = existHomeDirname.map(Path::normalize);
283267

284-
// otherwise, secondly try to read configuration from file. Guess the
268+
// firstly, try to read configuration from file. Guess the
285269
// location if necessary
286-
if (is == null) {
287-
existHome = existHomeDirname.map(Optional::of)
288-
.orElse(ConfigurationHelper.getExistHome(configFilename));
270+
existHome = existHomeDirname.map(Optional::of)
271+
.orElse(ConfigurationHelper.getExistHome(configFilename));
289272

290-
if (existHome.isEmpty()) {
273+
if (existHome.isEmpty()) {
291274

292-
// EB: try to create existHome based on location of config file
293-
// when config file points to absolute file location
294-
final Path absoluteConfigFile = Path.of(configFilename);
275+
// EB: try to create existHome based on location of config file
276+
// when config file points to absolute file location
277+
final Path absoluteConfigFile = Path.of(configFilename);
295278

296-
if (absoluteConfigFile.isAbsolute() && Files.exists(absoluteConfigFile) && Files.isReadable(absoluteConfigFile)) {
297-
existHome = Optional.of(absoluteConfigFile.getParent());
298-
configFilename = FileUtils.fileName(absoluteConfigFile);
299-
}
279+
if (absoluteConfigFile.isAbsolute() && Files.exists(absoluteConfigFile) && Files.isReadable(absoluteConfigFile)) {
280+
existHome = Optional.of(absoluteConfigFile.getParent());
281+
configFilename = FileUtils.fileName(absoluteConfigFile);
300282
}
283+
}
301284

302-
Path configFile = Path.of(configFilename);
303-
304-
if (!configFile.isAbsolute() && existHome.isPresent()) {
285+
Path configFile = Path.of(configFilename);
305286

306-
// try the passed or constructed existHome first
307-
configFile = existHome.get().resolve(configFilename);
287+
if (!configFile.isAbsolute() && existHome.isPresent()) {
308288

309-
if (!Files.exists(configFile)) {
310-
configFile = existHome.get().resolve(Main.CONFIG_DIR_NAME).resolve(configFilename);
311-
}
312-
}
289+
// try the passed or constructed existHome first
290+
configFile = existHome.get().resolve(configFilename);
313291

314-
if (!Files.exists(configFile) || !Files.isReadable(configFile)) {
315-
throw new DatabaseConfigurationException("Unable to read configuration file at " + configFile);
292+
if (!Files.exists(configFile)) {
293+
configFile = existHome.get().resolve(Main.CONFIG_DIR_NAME).resolve(configFilename);
316294
}
295+
}
317296

297+
if (Files.exists(configFile) && Files.isReadable(configFile)) {
318298
configFilePath = Optional.of(configFile.toAbsolutePath());
319299
is = Files.newInputStream(configFile);
320300
}
321301

302+
// otherwise, secondly try to read the configuration from a file within the
303+
// classpath
304+
if (is == null) {
305+
try {
306+
is = Configuration.class.getClassLoader().getResourceAsStream(configFilename);
307+
308+
if (is != null) {
309+
LOG.info("Reading configuration from classloader");
310+
configFilePath = Optional.of(Path.of(Configuration.class.getClassLoader().getResource(configFilename).toURI()));
311+
existHome = configFilePath.map(p -> p.getParent().getParent());
312+
}
313+
} catch (final Exception e) {
314+
// EB: ignore and go forward, e.g. in case there is an absolute
315+
// file name for configFileName
316+
LOG.debug("Error reading configuration from classloader: {}", e.getMessage(), e);
317+
}
318+
}
319+
320+
if (is == null) {
321+
throw new DatabaseConfigurationException("Unable to read configuration file at " + configFile);
322+
}
323+
322324
LOG.info("Reading configuration from file {}", configFilePath.map(Path::toString).orElse("Unknown"));
323325

324326
// set dbHome to parent of the conf file found, to resolve relative

0 commit comments

Comments
 (0)