Reloading cards from admin console - as watchers do not work on remote storage

This commit is contained in:
marcin.sciesinski
2019-09-03 12:18:10 -07:00
parent 2933fca29e
commit 9f1a467d86
2 changed files with 18 additions and 64 deletions

View File

@@ -5,12 +5,14 @@ import com.gempukku.lotro.async.HttpProcessingException;
import com.gempukku.lotro.async.ResponseWriter;
import com.gempukku.lotro.cache.CacheManager;
import com.gempukku.lotro.collection.CollectionsManager;
import com.gempukku.lotro.common.ApplicationConfiguration;
import com.gempukku.lotro.db.LeagueDAO;
import com.gempukku.lotro.db.PlayerDAO;
import com.gempukku.lotro.db.vo.CollectionType;
import com.gempukku.lotro.draft2.SoloDraftDefinitions;
import com.gempukku.lotro.game.CardCollection;
import com.gempukku.lotro.game.CardSets;
import com.gempukku.lotro.game.LotroCardBlueprintLibrary;
import com.gempukku.lotro.game.Player;
import com.gempukku.lotro.game.formats.LotroFormatLibrary;
import com.gempukku.lotro.hall.HallServer;
@@ -35,6 +37,7 @@ import java.util.List;
import java.util.Map;
public class AdminRequestHandler extends LotroServerRequestHandler implements UriRequestHandler {
private final LotroCardBlueprintLibrary lotroCardBlueprintLibrary;
private SoloDraftDefinitions _soloDraftDefinitions;
private LeagueService _leagueService;
private TournamentService _tournamentService;
@@ -60,6 +63,7 @@ public class AdminRequestHandler extends LotroServerRequestHandler implements Ur
_playerDAO = extractObject(context, PlayerDAO.class);
_collectionManager = extractObject(context, CollectionsManager.class);
_adminService = extractObject(context, AdminService.class);
lotroCardBlueprintLibrary = extractObject(context, LotroCardBlueprintLibrary.class);
}
@Override
@@ -68,6 +72,8 @@ public class AdminRequestHandler extends LotroServerRequestHandler implements Ur
clearCache(request, responseWriter);
} else if (uri.equals("/shutdown") && request.getMethod() == HttpMethod.GET) {
shutdown(request, responseWriter);
} else if (uri.equals("/reloadCards") && request.getMethod() == HttpMethod.GET) {
reloadCards(request, responseWriter);
} else if (uri.equals("/setMotd") && request.getMethod() == HttpMethod.POST) {
setMotd(request, responseWriter);
} else if (uri.equals("/previewSealedLeague") && request.getMethod() == HttpMethod.POST) {
@@ -541,6 +547,14 @@ public class AdminRequestHandler extends LotroServerRequestHandler implements Ur
responseWriter.writeHtmlResponse("OK");
}
private void reloadCards(HttpRequest request, ResponseWriter responseWriter) throws HttpProcessingException {
validateAdmin(request);
lotroCardBlueprintLibrary.reloadCards(new java.io.File(ApplicationConfiguration.getProperty("card.path")));
responseWriter.writeHtmlResponse("OK");
}
private void clearCache(HttpRequest request, ResponseWriter responseWriter) throws HttpProcessingException {
validateAdmin(request);

View File

@@ -9,12 +9,9 @@ import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.io.*;
import java.nio.file.*;
import java.util.*;
import java.util.concurrent.CountDownLatch;
import static java.nio.file.StandardWatchEventKinds.*;
public class LotroCardBlueprintLibrary {
private static Logger logger = Logger.getLogger(LotroCardBlueprintLibrary.class);
@@ -77,11 +74,14 @@ public class LotroCardBlueprintLibrary {
public void init(File cardPath, CardSets cardSets) {
loadCards(cardPath);
setupWatcher(cardPath.toPath());
initCardSets(cardSets);
collectionReadyLatch.countDown();
}
public void reloadCards(File cardPath) {
loadCards(cardPath);
}
private void loadCards(File path) {
if (path.isFile())
loadCardsFromFile(path);
@@ -115,66 +115,6 @@ public class LotroCardBlueprintLibrary {
logger.debug("Loaded card file " + file.getName());
}
private void setupWatcher(Path path) {
Thread thr = new Thread(
() -> {
try {
WatchService watcher = FileSystems.getDefault().newWatchService();
path.register(watcher,
ENTRY_CREATE,
ENTRY_MODIFY);
while (true) {
// wait for key to be signaled
WatchKey key;
try {
key = watcher.take();
} catch (InterruptedException x) {
return;
}
for (WatchEvent<?> event : key.pollEvents()) {
WatchEvent.Kind<?> kind = event.kind();
// This key is registered only
// for ENTRY_CREATE events,
// but an OVERFLOW event can
// occur regardless if events
// are lost or discarded.
if (kind == OVERFLOW) {
continue;
}
// The filename is the
// context of the event.
WatchEvent<Path> ev = (WatchEvent<Path>) event;
Path filename = ev.context();
// Resolve the filename against the directory.
// If the filename is "test" and the directory is "foo",
// the resolved name is "test/foo".
Path child = path.resolve(filename);
final File file = child.toFile();
loadCards(file);
}
// Reset the key -- this step is critical if you want to
// receive further watch events. If the key is no longer valid,
// the directory is inaccessible so exit the loop.
boolean valid = key.reset();
if (!valid) {
break;
}
}
} catch (IOException exp) {
logger.error("Unable to setup folder watcher on " + path.toString(), exp);
}
}
);
thr.start();
}
public String getBaseBlueprintId(String blueprintId) {
blueprintId = stripBlueprintModifiers(blueprintId);
String base = _blueprintMapping.get(blueprintId);