Skip to content

Commit

Permalink
External file scan when --watch is set was broken for a while
Browse files Browse the repository at this point in the history
  • Loading branch information
azagniotov committed Feb 25, 2021
1 parent 5abc29a commit 89c44d7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,28 @@


import io.github.azagniotov.stubby4j.cli.ANSITerminal;
import io.github.azagniotov.stubby4j.cli.CommandLineInterpreter;
import io.github.azagniotov.stubby4j.filesystem.ExternalFilesScanner;
import io.github.azagniotov.stubby4j.filesystem.MainYamlScanner;
import io.github.azagniotov.stubby4j.stubs.StubRepository;
import io.github.azagniotov.stubby4j.utils.ObjectUtils;
import org.eclipse.jetty.server.Server;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;

public final class StubbyManager {
private static final Logger LOGGER = LoggerFactory.getLogger(StubbyManager.class);

private final Map<String, String> commandLineArgs;
private final Server server;
private final JettyFactory jettyFactory;
private final StubRepository stubRepository;

StubbyManager(final Server server, final JettyFactory jettyFactory, final StubRepository stubRepository) {
StubbyManager(final Map<String, String> commandLineArgs, final Server server, final JettyFactory jettyFactory, final StubRepository stubRepository) {
this.commandLineArgs = commandLineArgs;
this.server = server;
this.jettyFactory = jettyFactory;
this.stubRepository = stubRepository;
Expand All @@ -53,6 +60,12 @@ public synchronized void startJetty() throws Exception {
Thread.sleep(250);
}
stubRepository.retrieveLoadedStubs();

if (commandLineArgs.containsKey(CommandLineInterpreter.OPTION_WATCH)) {
final String watchValue = commandLineArgs.get(CommandLineInterpreter.OPTION_WATCH);
final long watchScanTime = ObjectUtils.isNotNull(watchValue) ? Long.parseLong(watchValue) : 100;
watchDataStore(stubRepository, watchScanTime);
}
}

public synchronized void stopJetty() throws Exception {
Expand Down Expand Up @@ -95,4 +108,13 @@ private boolean isJettyStopping() throws Exception {
private boolean isJettyDown() throws Exception {
return (server.isStopped() && !server.isRunning());
}

private void watchDataStore(final StubRepository stubRepository, final long sleepTime) {

final MainYamlScanner mainYamlScanner = new MainYamlScanner(stubRepository, sleepTime);
new Thread(mainYamlScanner, MainYamlScanner.class.getCanonicalName()).start();

final ExternalFilesScanner externalFilesScanner = new ExternalFilesScanner(stubRepository, sleepTime);
new Thread(externalFilesScanner, ExternalFilesScanner.class.getCanonicalName()).start();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@
import io.github.azagniotov.stubby4j.cli.ANSITerminal;
import io.github.azagniotov.stubby4j.cli.CommandLineInterpreter;
import io.github.azagniotov.stubby4j.cli.EmptyLogger;
import io.github.azagniotov.stubby4j.filesystem.ExternalFilesScanner;
import io.github.azagniotov.stubby4j.filesystem.MainYamlScanner;
import io.github.azagniotov.stubby4j.stubs.StubRepository;
import io.github.azagniotov.stubby4j.utils.ObjectUtils;
import io.github.azagniotov.stubby4j.yaml.YamlParseResultSet;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.log.Log;
Expand All @@ -51,25 +48,10 @@ public synchronized StubbyManager construct(final File configFile,
final JettyFactory jettyFactory = new JettyFactory(commandLineArgs, stubRepository);
final Server server = jettyFactory.construct();

if (commandLineArgs.containsKey(CommandLineInterpreter.OPTION_WATCH)) {
final String watchValue = commandLineArgs.get(CommandLineInterpreter.OPTION_WATCH);
final long watchScanTime = ObjectUtils.isNotNull(watchValue) ? Long.parseLong(watchValue) : 100;
watchDataStore(stubRepository, watchScanTime);
}

if (commandLineArgs.containsKey(CommandLineInterpreter.OPTION_MUTE)) {
ANSITerminal.muteConsole(true);
}

return new StubbyManager(server, jettyFactory, stubRepository);
}

private void watchDataStore(final StubRepository stubRepository, final long sleepTime) {

final MainYamlScanner mainYamlScanner = new MainYamlScanner(stubRepository, sleepTime);
new Thread(mainYamlScanner, MainYamlScanner.class.getCanonicalName()).start();

final ExternalFilesScanner externalFilesScanner = new ExternalFilesScanner(stubRepository, sleepTime);
new Thread(externalFilesScanner, ExternalFilesScanner.class.getCanonicalName()).start();
return new StubbyManager(commandLineArgs, server, jettyFactory, stubRepository);
}
}

0 comments on commit 89c44d7

Please sign in to comment.