Skip to content

Commit

Permalink
Restructured config options info.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Nov 14, 2015
1 parent 041cc67 commit 7676385
Show file tree
Hide file tree
Showing 8 changed files with 218 additions and 81 deletions.
27 changes: 12 additions & 15 deletions rapidoid-config/src/main/java/org/rapidoid/config/Conf.java
Expand Up @@ -42,23 +42,21 @@ public class Conf {
private static String dynamicPath = dynamicPathDefault();
private static String configPath = configPathDefault();

static {
initConfig();
}

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

public static synchronized void init(String[] mainArgs, Object... extraArgs) {
if (mainArgs != null) {
for (String arg : mainArgs) {
public static synchronized void init(String[] args, Object... extraOptions) {
ConfigHelp.processHelp(args);

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

if (extraArgs != null) {
for (Object arg : extraArgs) {
if (extraOptions != null) {
for (Object arg : extraOptions) {
if (arg instanceof String) {
processArg((String) arg);
}
Expand Down Expand Up @@ -261,7 +259,6 @@ public static void setRootPath(String rootPath) {
setStaticPath(Conf.rootPath + "/static");
setDynamicPath(Conf.rootPath + "/dynamic");
setConfigPath(Conf.rootPath);
initConfig();
}

public static void setStaticPath(String staticPath) {
Expand Down Expand Up @@ -322,12 +319,12 @@ public void run() {
return conf;
}

public static Config users() {
return USERS;
}
public static synchronized Config users() {
if (USERS == null) {
USERS = refreshing("", "users.yaml");
}

private static synchronized void initConfig() {
USERS = refreshing("", "users.yaml");
return USERS;
}

}
58 changes: 58 additions & 0 deletions rapidoid-config/src/main/java/org/rapidoid/config/ConfigHelp.java
@@ -0,0 +1,58 @@
package org.rapidoid.config;

import org.rapidoid.u.U;

/*
* #%L
* rapidoid-config
* %%
* 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%
*/

/**
* @author Nikolche Mihajlovski
* @since 5.0.2
*/
public class ConfigHelp {

public static void processHelp(Object[] args) {
if (args.length == 1 && args[0].equals("--help")) {
show("Usage:");
show(" java -cp <yourapp>.jar com.yourapp.Main [option1 option2 ...]");

show("\nExample:");
show(" java -cp <yourapp>.jar com.yourapp.Main port=9090 address=127.0.0.1 cpus=2 workers=4 nodelay");

show("\nAvailable options:");

for (ConfigOption opt : ConfigOptions.ALL) {
String desc = U.frmt("%s (default: %s)", opt.getDesc(), opt.getDefaultValue());
opt(opt.getName(), desc);
}

System.exit(0);
}
}

private static void opt(String opt, String desc) {
show(" " + opt + U.mul(" ", 21 - opt.length()) + " - " + desc);
}

private static void show(String msg) {
System.out.println(msg);
}

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

/*
* #%L
* rapidoid-config
* %%
* 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%
*/

/**
* @author Nikolche Mihajlovski
* @since 5.0.2
*/
public class ConfigOption {

public final String name;

public final String desc;

public final Object defaultValue;

public ConfigOption(String name, String desc, Object defaultValue) {
this.name = name;
this.desc = desc;
this.defaultValue = defaultValue;
}

public String getName() {
return name;
}

public String getDesc() {
return desc;
}

public Object getDefaultValue() {
return defaultValue;
}

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

import java.util.List;

import org.rapidoid.u.U;

/*
* #%L
* rapidoid-config
* %%
* 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%
*/

/**
* @author Nikolche Mihajlovski
* @since 5.0.2
*/
public class ConfigOptions {

public static final List<ConfigOption> ALL = configOptions();

private static List<ConfigOption> configOptions() {
List<ConfigOption> opts = U.list();

opts.add(opt("mode=(dev|production)", "configure DEV or PRODUCTION mode", "auto-detected"));
opts.add(opt("secret=<SECRET>", "configure app-specific secret token for encryption", null));
opts.add(opt("port=<P>", "listen at port P", 8888));
opts.add(opt("address=<ADDR>", "listen at address ADDR", "0.0.0.0"));
// opts.add(opt("stateless", "Run in stateless mode, session becomes cookiepack", false));
opts.add(opt("threads=<T>", "start T threads for the job executor service", 100));
opts.add(opt("cpus=<C>", "optimize for C number of CPUs", "the actual number of the CPUs"));
opts.add(opt("workers=<W>", "start W number of I/O workers", "the configured number of CPUs - cpus options"));
opts.add(opt("nodelay", "set the TCP_NODELAY flag to disable Nagle's algorithm", false));
opts.add(opt("blockingAccept", "accept connection in BLOCKING mode", false));
opts.add(opt("bufSizeKB=<SIZE>", "TCP socket buffer size in KB", 16));

return opts;
}

private static ConfigOption opt(String name, String desc, Object def) {
return new ConfigOption(name, desc, def);
}

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

/*
* #%L
* rapidoid-config
* %%
* 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 org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;

@Authors("Nikolche Mihajlovski")
@Since("5.0.2")
public class ConfigOptionsTest {

public static void main(String[] args) {
Conf.args(new String[] { "--help" });
}

}
3 changes: 2 additions & 1 deletion rapidoid-main/src/main/java/org/rapidoid/main/Main.java
Expand Up @@ -22,13 +22,14 @@

import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.config.ConfigHelp;

@Authors("Nikolche Mihajlovski")
@Since("4.0.0")
public class Main {

public static void main(String[] args) {
MainHelp.processHelp(args);
ConfigHelp.processHelp(args);
Rapidoid.run(args);
}

Expand Down
64 changes: 0 additions & 64 deletions rapidoid-main/src/main/java/org/rapidoid/main/MainHelp.java

This file was deleted.

3 changes: 2 additions & 1 deletion rapidoid-main/src/main/java/org/rapidoid/main/Rapidoid.java
Expand Up @@ -23,6 +23,7 @@
import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.config.Conf;
import org.rapidoid.config.ConfigHelp;
import org.rapidoid.log.Log;
import org.rapidoid.plugins.Plugins;
import org.rapidoid.plugins.cache.guava.GuavaCachePlugin;
Expand Down Expand Up @@ -65,7 +66,7 @@ private static synchronized void initAndStart(WebApp app, String[] args, Object.

Log.info("Starting Rapidoid...", "version", UTILS.version());

MainHelp.processHelp(args);
ConfigHelp.processHelp(args);

// print internal state
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
Expand Down

0 comments on commit 7676385

Please sign in to comment.