Skip to content

Commit

Permalink
Resolve conf/application.conf and conf/routes from classpath (replay-…
Browse files Browse the repository at this point in the history
…framework#198)

in case when they don't exists in the working directory.

Running RePlay apps in a distributed form (zip, tar, docker) the conf/
directory doesn't always exists in the working directory, but in the
classpath.
  • Loading branch information
xabolcs committed Aug 18, 2023
1 parent 3a83f6f commit 8687995
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
8 changes: 8 additions & 0 deletions framework/src/play/Play.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;

import static java.nio.charset.StandardCharsets.UTF_8;
Expand Down Expand Up @@ -158,6 +159,13 @@ public void init(String id) {
setupApplicationMode();
VirtualFile appRoot = setupAppRoot();
routes = appRoot.child("conf/routes");
if (!routes.exists()) {
try {
routes = VirtualFile.open(Objects.requireNonNull(Thread.currentThread().getContextClassLoader().getResource("conf/routes")).getFile());
} catch (NullPointerException e) {
// keep going! VirtualFile's exception is more descriptive
}
}

modulesRoutes.clear();
loadModules(appRoot);
Expand Down
29 changes: 27 additions & 2 deletions framework/src/play/PropertiesConfLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.Set;
import java.util.regex.Matcher;
Expand All @@ -33,9 +34,33 @@ public Properties readOneConfigurationFile(String playId, String filename) {
}

private Properties readOneConfigurationFile(String filename, String playId, String inheritedId, Set<VirtualFile> confs) {
VirtualFile conf = VirtualFile.open(Play.applicationPath + "/conf/" + filename);
VirtualFile conf = resolveFilenameToVirtualFile(filename);
return readOneConfigurationFile(conf, playId, inheritedId, confs);
}

protected VirtualFile resolveFilenameToVirtualFile(String filename) {
VirtualFile conf = resolveFileNameWithApplicationPath(filename);
if (!conf.exists()) {
try {
conf = resolveFileNameWithClasspath(filename);
} catch (NullPointerException e) {
// keep going! VirtualFile's exception is more descriptive
}
}
return conf;
}

protected final VirtualFile resolveFileNameWithApplicationPath(String filename) {
return VirtualFile.open(Play.applicationPath + "/conf/" + filename);
}

protected final VirtualFile resolveFileNameWithClasspath(String filename) {
return VirtualFile.open(Objects.requireNonNull(Thread.currentThread().getContextClassLoader().getResource("conf/" + filename)).getPath());
}

private Properties readOneConfigurationFile(VirtualFile conf, String playId, String inheritedId, Set<VirtualFile> confs) {
if (confs.contains(conf)) {
throw new RuntimeException("Detected recursive @include usage. Have seen the file " + filename + " before");
throw new RuntimeException("Detected recursive @include usage. Have seen the file " + conf.getName() + " before");
}

confs.add(conf);
Expand Down

0 comments on commit 8687995

Please sign in to comment.