Skip to content

Commit

Permalink
Add "filePrefix" parameter to play.PropertiesConfLoader
Browse files Browse the repository at this point in the history
to ease migration from Play1 projects.

Modify "multi-module-app" test to work from "conf/" directory:
- use "conf/application.conf" for configuration
- use "conf/routes" for routes

This is an advanced setup, so play.server.Starter couldn't help.

While at it, fix README about Criminals example project.

Ref: replay-framework#198
  • Loading branch information
xabolcs committed Apr 24, 2024
1 parent bb0d4ec commit 4a43760
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ The following list breaks down the porting effort into tasks:
* Port the dependency specification from `conf/dependencies.yml` (Ivy2 format) to `build.gradle` (Gradle format).
* Ensure that `app/play.plugins` file (or the file where the `play.plugins.descriptor` configuration property is pointing) is on the classpath (e.g. `sourceSets.main.resources { srcDir 'app' }`) and add all plugins you need explicitly (see the section on "Plugins").
* Add the `app/<appname>/Application.java` and `app/<appname>/Module.java` (see the
[RePlay example project](/Users/andrei/projects/replay/replay-tests/criminals) for inspiration).
[RePlay example project](replay-tests/criminals/) and [multi-module-app test](replay-tests/multi-module-app/) for inspiration).
* Play1's [`PropertiesEnhancer`](https://github.com/playframework/play1/blob/master/framework/src/play/classloading/enhancers/PropertiesEnhancer.java) was removed.
* This enhancer reduces the boilerplate needed to make classes adhere to the "Java Bean" standard.
In short: a *bean* is a Java class that (1) implements `java.io.Serializable`, (2) implements public getter/setter methods for accessing the state, and
Expand Down
16 changes: 14 additions & 2 deletions framework/src/play/PropertiesConfLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,25 @@ public class PropertiesConfLoader implements ConfLoader {
private final Pattern overrideKeyPattern = Pattern.compile("^%([a-zA-Z0-9_\\-]+)\\.(.*)$");
private final Pattern envVarInterpolationPattern = Pattern.compile("\\$\\{([^}]+)}");

private String filePrefix = "";

public PropertiesConfLoader() {
this("");
}

public PropertiesConfLoader(String filePrefix) {
if (filePrefix != null) {
this.filePrefix = filePrefix;
}
}

public static Properties read(String playId) {
return new PropertiesConfLoader().readConfiguration(playId);
}

@Override
public Properties readConfiguration(String playId) {
return readOneConfigurationFile(playId, "application.conf");
return readOneConfigurationFile(playId, filePrefix + "application.conf");
}

public Properties readOneConfigurationFile(String playId, String filename) {
Expand Down Expand Up @@ -114,7 +126,7 @@ private void resolveIncludes(Properties propsFromFile, String playId, String inh
for (Map.Entry<Object, Object> e : propsFromFile.entrySet()) {
if (e.getKey().toString().startsWith("@include.")) {
try {
String filenameToInclude = e.getValue().toString();
String filenameToInclude = filePrefix + e.getValue().toString();
propsFromFile.putAll(readOneConfigurationFile(filenameToInclude, playId, inheritedId, confs));
}
catch (Exception ex) {
Expand Down
12 changes: 11 additions & 1 deletion replay-tests/multi-module-app/app/app/hello/HelloWorldApp.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
package hello;

import play.ClasspathResource;
import play.Play;
import play.PropertiesConfLoader;
import play.inject.DefaultBeanSource;
import play.mvc.CookieSessionStore;
import play.server.Starter;

public class HelloWorldApp {
public int start(String playId) {
return Starter.start(playId);
Play play = new Play(new PropertiesConfLoader("conf/"), new DefaultBeanSource(), new CookieSessionStore());
play.minimalInit(playId);
Play.routes = ClasspathResource.file("conf/routes");
Play.pluginCollection.loadPlugins();
play.start();
return Starter.start(play);
}
public static void main(String[] args) {
new HelloWorldApp().start(System.getProperty("play.id", "prod"));
Expand Down

0 comments on commit 4a43760

Please sign in to comment.