Yi EungJun 2014-04-28
config: Fix config error while starting Yobi
Problem: An error occurs while starting Yobi in product mode if there is
no conf/application.conf.

Reason: Yobi has a feature to initilizate conf/application.conf by
copying from conf/application.conf.default. But it does not work for
product mode because Application.onLoadConfig, which does the
initialization, runs after configuration has been loaded.

Solution: Run the config initialization in Build.scala.
@53920e0315f62917961a5b9fa4f8640997ea35ef
app/Global.java
--- app/Global.java
+++ app/Global.java
@@ -68,40 +68,10 @@
 import static play.mvc.Results.badRequest;
 
 public class Global extends GlobalSettings {
-    public static final String APPLICATION_CONF_DEFAULT = "application.conf.default";
-    public static final String APPLICATION_CONF = "application.conf";
-    public static final String CONFIG_DIRNAME = "conf";
     private final String DEFAULT_SECRET = "VA2v:_I=h9>?FYOH:@ZhW]01P<mWZAKlQ>kk>Bo`mdCiA>pDw64FcBuZdDh<47Ew";
 
     private boolean isRestartRequired = false;
     private boolean isValidationRequired = false;
-
-    @Override
-    public Configuration onLoadConfig(play.Configuration config, File path, ClassLoader classloader) {
-        String basePath = path.getAbsolutePath();
-        Path pathToDefaultConfig = Paths.get(basePath, CONFIG_DIRNAME, APPLICATION_CONF_DEFAULT);
-        Path pathToConfig = Paths.get(basePath, CONFIG_DIRNAME, APPLICATION_CONF);
-        File configFile = pathToConfig.toFile();
-
-        if (!configFile.exists()) {
-            try {
-                Files.copy(pathToDefaultConfig, pathToConfig);
-            } catch (IOException e) {
-                play.Logger.error("Failed to initialize configuration", e);
-                return null;
-            }
-            Config parsedConfig = ConfigFactory.parseFileAnySyntax(configFile);
-            return new Configuration(ConfigFactory.load(classloader, parsedConfig));
-        } else {
-            if (!configFile.isFile()) {
-                play.Logger.error(
-                        "Failed to initialize configuration: " + pathToConfig + " is a directory.");
-                return null;
-            }
-        }
-
-        return null;
-    }
 
     public void onStart(Application app) {
         isValidationRequired = !validateSecret();
project/Build.scala
--- project/Build.scala
+++ project/Build.scala
@@ -5,11 +5,17 @@
 import play.Project.javaEbean
 import play.Project.templatesImport
 import play.Project.lessEntryPoints
+import java.nio.file.Files
+import java.nio.file.Paths
+import java.io.IOException;
 
 object ApplicationBuild extends Build {
 
   val appName         = "yobi"
   val appVersion      = "1.0-SNAPSHOT"
+  val APPLICATION_CONF_DEFAULT = "application.conf.default"
+  val APPLICATION_CONF = "application.conf"
+  val CONFIG_DIRNAME = "conf"
 
   val appDependencies = Seq(
     // Add your project dependencies here,
@@ -59,7 +65,29 @@
       scalacOptions ++= Seq("-feature")
     )
 
-  val main = play.Project(appName, appVersion, appDependencies)
+  val initConfig = {
+    val basePath = new File(System.getProperty("user.dir")).getAbsolutePath()
+    val pathToDefaultConfig = Paths.get(basePath, CONFIG_DIRNAME, APPLICATION_CONF_DEFAULT)
+    val pathToConfig = Paths.get(basePath, CONFIG_DIRNAME, APPLICATION_CONF)
+    val configFile = pathToConfig.toFile()
+
+    if (!configFile.exists()) {
+        try {
+          Files.copy(pathToDefaultConfig, pathToConfig)
+        } catch {
+            case e: IOException => throw new Exception("Failed to initialize configuration", e)
+        }
+    } else {
+        if (!configFile.isFile()) {
+            throw new Exception("Failed to initialize configuration: '" + pathToConfig + "' is a directory.")
+        }
+    }
+  }
+
+  val main = {
+    initConfig
+    play.Project(appName, appVersion, appDependencies)
     .settings(projectSettings: _*)
     .settings(net.virtualvoid.sbt.graph.Plugin.graphSettings: _*)
+  }
 }
Add a comment
List