Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ jdk:
- oraclejdk8
- oraclejdk7
- openjdk7
- openjdk6

script:
"./gradlew check -i"
8 changes: 4 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ allprojects {

idea {
project {
jdkName = JavaVersion.VERSION_1_6
languageLevel = JavaVersion.VERSION_1_6
jdkName = JavaVersion.VERSION_1_7
languageLevel = JavaVersion.VERSION_1_7

vcs = "Git"
}
Expand All @@ -24,8 +24,8 @@ subprojects {
apply plugin: 'java'
group = 'com.github.dreamhead'
version = '0.11.0-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_1_6
targetCompatibility = JavaVersion.VERSION_1_6
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7

repositories {
jcenter()
Expand Down
12 changes: 12 additions & 0 deletions moco-doc/cmd.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,15 @@ Then you can use the shutdown port to shutdown the running Moco instance.
```shell
java -jar moco-runner-<version>-standalone.jar shutdown -s 9527
```

## Watch Service

Moco use monitor from commons-io to monitor file changes by default. You can change to WatchService API by adding `--watch-service` argument.

The `--watch-service` argument works with `http`, `https` and `socket` server types:

```shell
java -jar moco-runner-<version>-standalone.jar http -p 12306 -c foo.json --watch-service
```

The WatchService API provide better performance and less compatibility. A known issue is that it won't work on Docker Toolbox above Win 7.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

public final class HttpArgs extends StartArgs {
private HttpArgs(final Integer port, final Integer shutdownPort,
final String configurationFile, final String globalSettings,
final String env) {
super(ServerType.HTTP, port, shutdownPort, configurationFile, globalSettings, env, null);
final String configurationFile, final String globalSettings,
final String env, final boolean watchService) {
super(ServerType.HTTP, port, shutdownPort, configurationFile, globalSettings, env, null, watchService);
}

public static Builder httpArgs() {
Expand All @@ -19,6 +19,7 @@ public static class Builder {
private String configurationFile;
private String settings;
private String env;
private boolean watchService;

public Builder withPort(final Integer port) {
this.port = port;
Expand All @@ -45,8 +46,13 @@ public Builder withEnv(final String env) {
return this;
}

public Builder withWatchService(final boolean watchService) {
this.watchService = watchService;
return this;
}

public HttpArgs build() {
return new HttpArgs(port, shutdownPort, configurationFile, settings, env);
return new HttpArgs(port, shutdownPort, configurationFile, settings, env, watchService);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@

public final class HttpsArgs extends StartArgs {
private HttpsArgs(final Integer port, final Integer shutdownPort, final String configurationFile,
final String globalSettings, final String env, final HttpsArg httpsArg) {
super(ServerType.HTTPS, port, shutdownPort, configurationFile, globalSettings, env, httpsArg);
final String globalSettings, final String env, final HttpsArg httpsArg,
final boolean watchService) {
super(ServerType.HTTPS, port, shutdownPort, configurationFile, globalSettings, env, httpsArg, watchService);
}

public static Builder httpsArgs() {
Expand All @@ -20,6 +21,7 @@ public static class Builder {
private String settings;
private String env;
private HttpsArg httpsArg;
private boolean watchService;

public Builder withPort(final Integer port) {
this.port = port;
Expand Down Expand Up @@ -51,8 +53,13 @@ public Builder withHttpsArg(final HttpsArg httpsArg) {
return this;
}

public Builder withWatchService(final boolean watchService) {
this.watchService = watchService;
return this;
}

public HttpsArgs build() {
return new HttpsArgs(port, shutdownPort, configurationFile, settings, env, httpsArg);
return new HttpsArgs(port, shutdownPort, configurationFile, settings, env, httpsArg, watchService);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import static com.github.dreamhead.moco.bootstrap.ServerType.SOCKET;

public final class SocketArgs extends StartArgs {
private SocketArgs(final Integer port, final Integer shutdownPort, final String configurationFile) {
super(SOCKET, port, shutdownPort, configurationFile, null, null, null);
private SocketArgs(final Integer port, final Integer shutdownPort, final String configurationFile,
final boolean watchService) {
super(SOCKET, port, shutdownPort, configurationFile, null, null, null, watchService);
}

public static Builder socketArgs() {
Expand All @@ -15,6 +16,7 @@ public static class Builder {
private Integer port;
private Integer shutdownPort;
private String configurationFile;
private boolean watchService;

public Builder withPort(final Integer port) {
this.port = port;
Expand All @@ -31,8 +33,13 @@ public Builder withConfigurationFile(final String configurationFile) {
return this;
}

public Builder withWatchService(final boolean watchService) {
this.watchService = watchService;
return this;
}

public SocketArgs build() {
return new SocketArgs(port, shutdownPort, configurationFile);
return new SocketArgs(port, shutdownPort, configurationFile, watchService);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,19 @@ public abstract class StartArgs extends ShutdownPortOption {
private final Optional<String> settings;
private final Optional<String> env;
private final Optional<HttpsArg> httpsArg;
private final boolean watchService;

protected StartArgs(final ServerType type, final Integer port, final Integer shutdownPort,
final String configurationFile, final String globalSettings,
final String env, final HttpsArg httpsArg) {
final String env, final HttpsArg httpsArg, final boolean watchService) {
super(shutdownPort);
this.type = type;
this.port = fromNullable(port);
this.configurationFile = fromNullable(configurationFile);
this.settings = fromNullable(globalSettings);
this.env = fromNullable(env);
this.httpsArg = fromNullable(httpsArg);
this.watchService = watchService;
}

public Optional<Integer> getPort() {
Expand Down Expand Up @@ -74,4 +76,8 @@ public static String help() {
public boolean isSocket() {
return this.type == ServerType.SOCKET;
}

public boolean isWatchService() {
return watchService;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;

import static com.github.dreamhead.moco.bootstrap.ShutdownPortOption.shutdownPortOption;
import static com.github.dreamhead.moco.bootstrap.arg.HttpArgs.httpArgs;

public class HttpArgsParser extends StartArgsParser {
Expand All @@ -16,6 +15,7 @@ protected StartArgs parseArgs(final CommandLine cmd) {
String globalSettings = cmd.getOptionValue("g");
String shutdownPort = cmd.getOptionValue("s");
String env = cmd.getOptionValue("e");
boolean watchService = cmd.hasOption("watch-service");

if (config == null && globalSettings == null) {
throw new ParseArgException("config or global setting is required");
Expand All @@ -39,17 +39,14 @@ protected StartArgs parseArgs(final CommandLine cmd) {
.withConfigurationFile(config)
.withSettings(globalSettings)
.withEnv(env)
.withWatchService(watchService)
.build();
}

@Override
protected Options options() {
Options options = new Options();
options.addOption(configOption());
options.addOption(portOption());
options.addOption(shutdownPortOption());
options.addOption(settingsOption());
options.addOption(envOption());
return options;
return super.options()
.addOption(settingsOption())
.addOption(envOption());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.github.dreamhead.moco.bootstrap.HttpsArg;
import com.github.dreamhead.moco.bootstrap.ParseArgException;
import com.github.dreamhead.moco.bootstrap.ShutdownPortOption;
import com.github.dreamhead.moco.bootstrap.arg.StartArgs;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;
Expand All @@ -17,6 +16,7 @@ protected StartArgs parseArgs(final CommandLine cmd) {
String globalSettings = cmd.getOptionValue("g");
String shutdownPort = cmd.getOptionValue("s");
String env = cmd.getOptionValue("e");
boolean watchService = cmd.hasOption("watch-service");

if (config == null && globalSettings == null) {
throw new ParseArgException("config or global setting is required");
Expand All @@ -41,6 +41,7 @@ protected StartArgs parseArgs(final CommandLine cmd) {
.withSettings(globalSettings)
.withEnv(env)
.withHttpsArg(httpsArg(cmd))
.withWatchService(watchService)
.build();
}

Expand All @@ -61,15 +62,11 @@ private HttpsArg httpsArg(final CommandLine cmd) {

@Override
protected Options options() {
Options options = new Options();
options.addOption(configOption());
options.addOption(portOption());
options.addOption(ShutdownPortOption.shutdownPortOption());
options.addOption(settingsOption());
options.addOption(envOption());
options.addOption(httpsCertificate());
options.addOption(keyStore());
options.addOption(cert());
return options;
return super.options()
.addOption(settingsOption())
.addOption(envOption())
.addOption(httpsCertificate())
.addOption(keyStore())
.addOption(cert());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
import com.github.dreamhead.moco.bootstrap.ParseArgException;
import com.github.dreamhead.moco.bootstrap.arg.StartArgs;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;

import static com.github.dreamhead.moco.bootstrap.ShutdownPortOption.shutdownPortOption;
import static com.github.dreamhead.moco.bootstrap.arg.SocketArgs.socketArgs;

public class SocketArgsParser extends StartArgsParser {
Expand All @@ -14,6 +12,7 @@ protected StartArgs parseArgs(final CommandLine cmd) {
String port = cmd.getOptionValue("p");
String config = cmd.getOptionValue("c");
String shutdownPort = cmd.getOptionValue("s");
boolean watchService = cmd.hasOption("watch-service");

if (config == null) {
throw new ParseArgException("config is required");
Expand All @@ -27,15 +26,7 @@ protected StartArgs parseArgs(final CommandLine cmd) {
.withPort(getPort(port))
.withShutdownPort(getPort(shutdownPort))
.withConfigurationFile(config)
.withWatchService(watchService)
.build();
}

@Override
protected Options options() {
Options options = new Options();
options.addOption(configOption());
options.addOption(portOption());
options.addOption(shutdownPortOption());
return options;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@

import com.github.dreamhead.moco.bootstrap.ParseArgException;
import com.github.dreamhead.moco.bootstrap.arg.StartArgs;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.*;

import static com.github.dreamhead.moco.bootstrap.ShutdownPortOption.shutdownPortOption;

public abstract class StartArgsParser {
protected abstract Options options();
protected Options options() {
return new Options()
.addOption(configOption())
.addOption(portOption())
.addOption(shutdownPortOption())
.addOption(watchServiceOption());
}

protected abstract StartArgs parseArgs(final CommandLine cmd);

public StartArgs parse(final String[] args) {
Expand Down Expand Up @@ -76,6 +80,10 @@ protected Option cert() {
return option;
}

Option watchServiceOption() {
return new Option(null, "watch-service", false, "Enable watch service");
}

public static Integer getPort(final String port) {
if (port == null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,24 @@ private Runner createDynamicSettingRunner(final StartArgs startArgs) {
final File settingsFile = new File(startArgs.getSettings().get());
final FileRunner fileRunner = createSettingFileRunner(settingsFile, startArgs);
final SettingRunner runner = (SettingRunner) fileRunner.getRunner();
MocoRunnerWatcher fileMocoRunnerWatcher = monitorFactory.createSettingWatcher(settingsFile,
runner.getFiles(), fileRunner);
return new MonitorRunner(fileRunner, fileMocoRunnerWatcher);
final MocoRunnerWatcher mocoRunnerWatcher;
if (startArgs.isWatchService()) {
mocoRunnerWatcher = monitorFactory.createSettingWatcherBasedOnWatchService(settingsFile, runner.getFiles(), fileRunner);
} else {
mocoRunnerWatcher = monitorFactory.createSettingWatcher(settingsFile, runner.getFiles(), fileRunner);
}
return new MonitorRunner(fileRunner, mocoRunnerWatcher);
}

private Runner createDynamicConfigurationRunner(final StartArgs startArgs) {
final File configuration = new File(startArgs.getConfigurationFile().get());
final FileRunner fileRunner = createConfigurationFileRunner(configuration, startArgs);
MocoRunnerWatcher fileMocoRunnerWatcher = monitorFactory.createConfigurationWatcher(configuration, fileRunner);
return new MonitorRunner(fileRunner, fileMocoRunnerWatcher);
final MocoRunnerWatcher mocoRunnerWatcher;
if (startArgs.isWatchService()) {
mocoRunnerWatcher = monitorFactory.createConfigurationWatcherBasedOnWatchService(configuration, fileRunner);
} else {
mocoRunnerWatcher = monitorFactory.createConfigurationWatcher(configuration, fileRunner);
}
return new MonitorRunner(fileRunner, mocoRunnerWatcher);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.github.dreamhead.moco.runner.watcher;

import com.google.common.base.Function;
import org.apache.commons.io.monitor.FileAlterationListener;
import org.apache.commons.io.monitor.FileAlterationListenerAdaptor;
import org.apache.commons.io.monitor.FileAlterationMonitor;
import org.apache.commons.io.monitor.FileAlterationObserver;
import org.slf4j.Logger;
Expand All @@ -18,8 +20,13 @@ public class FileMocoRunnerWatcher implements MocoRunnerWatcher {
private final FileAlterationMonitor monitor;
private boolean running = false;

public FileMocoRunnerWatcher(final File file, final FileAlterationListener listener) {
this.monitor = monitorFile(file, listener);
public FileMocoRunnerWatcher(final File file, final Function<File, Void> listener) {
this.monitor = monitorFile(file, new FileAlterationListenerAdaptor() {
@Override
public void onFileChange(final File file) {
listener.apply(file);
}
});
}

public synchronized void startMonitor() {
Expand Down
Loading