Skip to content

Commit

Permalink
Simplified configuration utils and initialization.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Feb 10, 2016
1 parent a81525a commit cce04c3
Show file tree
Hide file tree
Showing 25 changed files with 142 additions and 210 deletions.
173 changes: 18 additions & 155 deletions rapidoid-commons/src/main/java/org/rapidoid/config/Conf.java
Expand Up @@ -4,7 +4,6 @@
import org.rapidoid.io.Res;
import org.rapidoid.log.Log;
import org.rapidoid.u.U;
import org.rapidoid.util.UTILS;

import java.util.Map;

Expand Down Expand Up @@ -34,6 +33,12 @@
*/
public class Conf {

private static final Config ROOT = new Config();

static {
RapidoidInitializer.initialize();
}

public static final ConfigParser YAML_PARSER = new ConfigParser() {
@SuppressWarnings("unchecked")
@Override
Expand All @@ -42,61 +47,23 @@ public Map<String, Object> parse(byte[] bytes) {
}
};

private static final Config ROOT = new Config();

private static String rootPath = rootPathDefault();
private static String staticPath = staticPathDefault();
private static String dynamicPath = dynamicPathDefault();
private static String configPath = configPathDefault();

public static synchronized void args(String... args) {
init(args, (Object[]) null);
}

public static synchronized void init(String[] args, Object... extraOptions) {
ConfigHelp.processHelp(args);

if (args != null) {
for (String arg : args) {
processArg(arg);
}
}

if (extraOptions != null) {
for (Object arg : extraOptions) {
if (arg instanceof String) {
processArg((String) arg);
}
}
}
}

private static void processArg(String arg) {
String name;
Object value;
String[] parts = arg.split("=", 2);

int pos = arg.indexOf('=');
if (pos > 0) {
name = arg.substring(0, pos);
value = arg.substring(pos + 1);
if (parts.length > 1) {
ROOT.put(parts[0], parts[1]);
} else {
name = arg;
value = true;
}

ROOT.put(name, value);
processArgIfSpecial(name, value);
}

private static void processArgIfSpecial(String name, Object value) {
if (name.equals("path")) {
setRootPath(value.toString());
} else if (name.equals("static")) {
setStaticPath(value.toString());
} else if (name.equals("dynamic")) {
setDynamicPath(value.toString());
} else if (name.equals("config")) {
setConfigPath(value.toString());
ROOT.put(parts[0], true);
}
}

Expand Down Expand Up @@ -153,58 +120,21 @@ public static int cpus() {
}

public static boolean micro() {
return has("size", "micro");
return is("micro");
}

public static boolean production() {
return has("mode", "production");
return is("production");
}

public static boolean dev() {
if (production()) {
return false;
}

assert configureDevMode(); // in debug mode

return has("mode", "dev") || !production();
}

private static boolean configureDevMode() {
ROOT.put("mode", "dev");
return true;
return true; // FIXME
}

public static String secret() {
return option("secret", null);
}

public static String system(String key) {
String value = option(key, null);

if (value == null) {
value = System.getProperty(key);
}

if (value == null) {
value = System.getenv(key);
}

return value;
}

public static String JAVA_HOME() {
return system("JAVA_HOME");
}

public static String HOSTNAME() {
return system("HOSTNAME");
}

public static String IP_ADDRESS() {
return system("IP_ADDRESS");
}

public static void reset() {
ROOT.clear();
}
Expand All @@ -225,85 +155,18 @@ public static <T> T nested(String... name) {
return ROOT.nested(name);
}

public static String rootPathDefault() {
return "rapidoid";
}

public static String rootPath() {
return rootPath;
}

public static String configPathDefault() {
return rootPathDefault();
}

public static String configPath() {
return configPath;
}

public static String staticPathDefault() {
return rootPathDefault() + "/static";
}

public static String staticPath() {
return staticPath;
}

public static String dynamicPathDefault() {
return rootPathDefault() + "/dynamic";
}

public static String dynamicPath() {
return dynamicPath;
}

private static String cleanPath(String path) {
if (path.endsWith("/") || path.endsWith("\\")) {
path = path.substring(0, path.length() - 1);
}
return path;
}

public static Config sub(String name) {
return root().sub(name);
}

public static synchronized void setRootPath(String rootPath) {
Log.info("Setting 'root' application path", "path", rootPath);
Conf.rootPath = cleanPath(rootPath);
setStaticPath(Conf.rootPath + "/static");
setDynamicPath(Conf.rootPath + "/dynamic");
setConfigPath(Conf.rootPath);
reset();
}

public static void setStaticPath(String staticPath) {
Log.info("Setting 'static' application path", "path", staticPath);
Conf.staticPath = cleanPath(staticPath);
public static Config refreshing(String filename) {
return refreshing(filename, YAML_PARSER);
}

public static void setDynamicPath(String dynamicPath) {
Log.info("Setting 'dynamic' application path", "path", dynamicPath);
Conf.dynamicPath = cleanPath(dynamicPath);
}

public static void setConfigPath(String configPath) {
Log.info("Setting 'config' application path", "path", configPath);
Conf.configPath = cleanPath(configPath);
}

public static Config refreshing(String path, String filename) {
return refreshing(path, filename, YAML_PARSER);
}

public static Config refreshing(String path, final String filename, final ConfigParser parser) {
Log.info("Initializing auto-refreshing config", "root", Conf.rootPath(), "path", path, "filename", filename);

final String resPath = UTILS.path(Conf.rootPath(), U.safe(path));

Log.info("Calculated resource path", "path", resPath);
public static Config refreshing(final String filename, final ConfigParser parser) {
Log.info("Initializing auto-refreshing config", "filename", filename);

final Res res = Res.from(filename, null, resPath);
final Res res = Res.from(filename);

Config config = res.attachment();
if (config == null) {
Expand Down
27 changes: 21 additions & 6 deletions rapidoid-commons/src/main/java/org/rapidoid/config/Config.java
Expand Up @@ -43,19 +43,30 @@ public Config() {
this(U.<String, Object>map());
}

public String option(String name) {
Object opt = getValue(name);
return opt != null ? U.str(opt) : null;
}

private Object getValue(String name) {
if (name.contains(".")) {
return nested(name.split("\\."));

} else {
return properties.get(name);
Object value = properties.get(name);

if (value == null) {
value = System.getProperty(name);
}

if (value == null) {
value = System.getenv(name);
}

return value;
}
}

public String option(String name) {
Object opt = getValue(name);
return opt != null ? U.str(opt) : null;
}

public String option(String name, String defaultValue) {
Object obj = getValue(name);
return obj != null ? U.str(obj) : defaultValue;
Expand Down Expand Up @@ -181,4 +192,8 @@ public boolean isEmpty() {
return properties.isEmpty();
}

public void putAll(Map<String, ?> entries) {
properties.putAll((Map<String, Object>) entries);
}

}
@@ -0,0 +1,25 @@
package org.rapidoid.config;

import org.rapidoid.cls.Cls;
import org.rapidoid.commons.RapidoidInfo;
import org.rapidoid.log.Log;

public class RapidoidInitializer {

private static boolean initialized;

public static synchronized void initialize() {
if (!initialized) {
Log.info("Starting Rapidoid...", "version", RapidoidInfo.version());

Log.info("Working directory is: " + System.getProperty("user.dir"));

Cls.getClassIfExists("org.rapidoid.web.RapidoidWebModule");

Log.info("Rapidoid is ready.");

initialized = true;
}
}

}
23 changes: 16 additions & 7 deletions rapidoid-commons/src/main/java/org/rapidoid/io/Res.java
Expand Up @@ -71,23 +71,32 @@ private Res(String name, String... possibleLocations) {
this.possibleLocations = possibleLocations;
}

public static Res from(File file) {
return file.isAbsolute() ? absolute(file) : from(file.getName(), "");
public static Res from(File file, String... possibleLocations) {
U.must(!file.isAbsolute() || U.isEmpty(possibleLocations), "Cannot specify locations for an absolute filename!");
return file.isAbsolute() ? absolute(file) : relative(file.getPath(), possibleLocations);
}

public static Res absolute(File file) {
U.must(file.isAbsolute(), "Expected from filename!");
public static Res from(String filename, String... possibleLocations) {
return from(new File(filename), possibleLocations);
}

private static Res absolute(File file) {
return create(file.getAbsolutePath());
}

public static Res from(String filename, String... possibleLocations) {
private static Res relative(String filename, String... possibleLocations) {
File file = new File(filename);

if (file.isAbsolute()) {
return absolute(file);
}

if (U.isEmpty(possibleLocations)) {
possibleLocations = DEFAULT_LOCATIONS;
}

U.must(!U.isEmpty(filename), "Resource filename must be specified!");
U.must(!new File(filename).isAbsolute(), "Expected relative filename!");
U.must(!file.isAbsolute(), "Expected relative filename!");

return create(filename, possibleLocations);
}
Expand Down Expand Up @@ -186,7 +195,7 @@ protected byte[] load(String filename) {
Log.trace("Resource file exists", "name", name, "file", file);

if (file.lastModified() > this.lastModified || !filename.equals(cachedFileName)) {
Log.info("Loading resource file", "name", name, "file", file);
Log.debug("Loading resource file", "name", name, "file", file);
this.lastModified = file.lastModified();
return IO.loadBytes(filename);
} else {
Expand Down
5 changes: 5 additions & 0 deletions rapidoid-commons/src/main/java/org/rapidoid/job/Jobs.java
Expand Up @@ -25,6 +25,7 @@
import org.rapidoid.annotation.Since;
import org.rapidoid.concurrent.Callback;
import org.rapidoid.config.Conf;
import org.rapidoid.config.RapidoidInitializer;
import org.rapidoid.ctx.Ctx;
import org.rapidoid.ctx.Ctxs;
import org.rapidoid.ctx.JobStatusListener;
Expand All @@ -35,6 +36,10 @@
@Since("4.1.0")
public class Jobs {

static {
RapidoidInitializer.initialize();
}

private static ScheduledExecutorService SCHEDULER;

private static Executor EXECUTOR;
Expand Down

0 comments on commit cce04c3

Please sign in to comment.