Skip to content

Commit

Permalink
Separated the root app factory in its own class.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Aug 13, 2015
1 parent be7e456 commit 1ea1845
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 56 deletions.
92 changes: 92 additions & 0 deletions rapidoid-main/src/main/java/org/rapidoid/main/AppTool.java
@@ -0,0 +1,92 @@
package org.rapidoid.main;

/*
* #%L
* rapidoid-main
* %%
* Copyright (C) 2014 - 2015 Nikolche Mihajlovski and contributors
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/

import java.util.Map;
import java.util.Map.Entry;

import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.app.AppHandler;
import org.rapidoid.config.Conf;
import org.rapidoid.io.Res;
import org.rapidoid.jackson.YAML;
import org.rapidoid.log.Log;
import org.rapidoid.webapp.AppMenu;
import org.rapidoid.webapp.RootWebApp;
import org.rapidoid.webapp.WebApp;
import org.rapidoid.webapp.WebAppGroup;

@Authors("Nikolche Mihajlovski")
@Since("4.1.0")
public class AppTool {

public static WebApp createRootApp() {
RootWebApp app = WebAppGroup.root();
app.getRouter().generic(new AppHandler());

String menufile = "menu.yaml";
String firstMenuFile = Conf.configPath() + "/" + menufile;
String defaultMenuFile = Conf.configPathDefault() + "/" + menufile;
final Res menuRes = Res.from(menufile, true, firstMenuFile, defaultMenuFile).trackChanges();

final WebApp rootApp = app;
menuRes.getChangeListeners().add(new Runnable() {
@Override
public void run() {
if (menuRes.exists()) {
Object menuData = YAML.parse(menuRes.getContent(), Object.class);
AppMenu menu = AppMenu.from(menuData);
rootApp.setMenu(menu);
}
}
});

String appfile = "app.yaml";
String firstAppFile = Conf.configPath() + "/" + appfile;
String defaultAppFile = Conf.configPathDefault() + "/" + appfile;
final Res confRes = Res.from(appfile, true, firstAppFile, defaultAppFile).trackChanges();

confRes.getChangeListeners().add(new Runnable() {
@Override
public void run() {
if (confRes.exists()) {
Map<String, Object> conf = YAML.parseMap(confRes.getContent());

Conf.reset();
rootApp.getConfig().clear();

for (Entry<String, Object> e : conf.entrySet()) {
String key = e.getKey();
Object value = e.getValue();

Log.info("Configuring", key, value);
Conf.set(key, value);
rootApp.getConfig().put(key, value);
}
}
}
});

return app;
}

}
62 changes: 6 additions & 56 deletions rapidoid-main/src/main/java/org/rapidoid/main/Rapidoid.java
Expand Up @@ -20,19 +20,12 @@
* #L%
*/

import java.util.Map;
import java.util.Map.Entry;

import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.app.AppHandler;
import org.rapidoid.config.Conf;
import org.rapidoid.io.Res;
import org.rapidoid.jackson.YAML;
import org.rapidoid.log.Log;
import org.rapidoid.quick.Quick;
import org.rapidoid.util.U;
import org.rapidoid.webapp.AppMenu;
import org.rapidoid.webapp.WebApp;
import org.rapidoid.webapp.WebAppGroup;

Expand All @@ -42,12 +35,11 @@ public class Rapidoid {

private static boolean initialized = false;

public static synchronized void run(String[] args, Object... config) {
WebApp noApp = null;
run(noApp, args, config);
public static synchronized WebApp run(String[] args, Object... config) {
return run(null, args, config);
}

public static synchronized void run(WebApp app, String[] args, Object... config) {
public static synchronized WebApp run(WebApp app, String[] args, Object... config) {
Log.info("Starting Rapidoid...");
U.must(!initialized, "Already initialized!");
initialized = true;
Expand All @@ -56,55 +48,13 @@ public static synchronized void run(WebApp app, String[] args, Object... config)

Conf.args(args, config);

if (app == null) {
app = WebAppGroup.root();
app.getRouter().generic(new AppHandler());

String menufile = "menu.yaml";
String firstMenuFile = Conf.configPath() + "/" + menufile;
String defaultMenuFile = Conf.configPathDefault() + "/" + menufile;
final Res menuRes = Res.from(menufile, true, firstMenuFile, defaultMenuFile).trackChanges();

final WebApp rootApp = app;
menuRes.getChangeListeners().add(new Runnable() {
@Override
public void run() {
if (menuRes.exists()) {
Object menuData = YAML.parse(menuRes.getContent(), Object.class);
AppMenu menu = AppMenu.from(menuData);
rootApp.setMenu(menu);
}
}
});

String appfile = "app.yaml";
String firstAppFile = Conf.configPath() + "/" + appfile;
String defaultAppFile = Conf.configPathDefault() + "/" + appfile;
final Res confRes = Res.from(appfile, true, firstAppFile, defaultAppFile).trackChanges();

confRes.getChangeListeners().add(new Runnable() {
@Override
public void run() {
if (confRes.exists()) {
Map<String, Object> conf = YAML.parseMap(confRes.getContent());

Conf.reset();
rootApp.getConfig().clear();

for (Entry<String, Object> e : conf.entrySet()) {
String key = e.getKey();
Object value = e.getValue();

Log.info("Configuring", key, value);
Conf.set(key, value);
rootApp.getConfig().put(key, value);
}
}
}
});
if (app == null) {
app = AppTool.createRootApp();
}

Quick.run(app, args, config);
return app;
}

public static void register(WebApp app) {
Expand Down

0 comments on commit 1ea1845

Please sign in to comment.