Skip to content

Commit

Permalink
Resolve conf/application.conf and conf/routes from classpath (#198)
Browse files Browse the repository at this point in the history
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 Jun 27, 2023
1 parent b101f01 commit 8b9dbef
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
14 changes: 8 additions & 6 deletions framework/src/play/Play.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,7 @@
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.*;

import static java.nio.charset.StandardCharsets.UTF_8;

Expand Down Expand Up @@ -158,6 +153,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
33 changes: 27 additions & 6 deletions framework/src/play/PropertiesConfLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
import play.utils.OrderSafeProperties;
import play.vfs.VirtualFile;

import java.util.HashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -33,9 +30,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 8b9dbef

Please sign in to comment.