X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Freactor%2Floader%2FChainWatcher.java;h=3d30f2615edfc140405d9237064e891134b05cdd;hb=ecdd6bf87e8e2bddb897e97a6089deefef04a91b;hp=10abcc5a883a1e023c4918fad06f9cdd7eac39c5;hpb=78d6c1bdd74b83235b4eb8ca4654e60846f9b421;p=rhynodge.git diff --git a/src/main/java/net/pterodactylus/reactor/loader/ChainWatcher.java b/src/main/java/net/pterodactylus/reactor/loader/ChainWatcher.java index 10abcc5..3d30f26 100644 --- a/src/main/java/net/pterodactylus/reactor/loader/ChainWatcher.java +++ b/src/main/java/net/pterodactylus/reactor/loader/ChainWatcher.java @@ -19,9 +19,12 @@ package net.pterodactylus.reactor.loader; import java.io.File; import java.io.FilenameFilter; +import java.io.IOException; import java.util.HashMap; +import java.util.HashSet; import java.util.Map; import java.util.Map.Entry; +import java.util.Set; import java.util.concurrent.TimeUnit; import net.pterodactylus.reactor.Reaction; @@ -30,16 +33,17 @@ import net.pterodactylus.reactor.loader.Chain.Parameter; import net.pterodactylus.reactor.loader.Chain.Part; import org.apache.log4j.Logger; -import org.simpleframework.xml.Serializer; -import org.simpleframework.xml.core.Persister; +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.base.Predicate; import com.google.common.collect.Maps; import com.google.common.util.concurrent.AbstractExecutionThreadService; import com.google.common.util.concurrent.Uninterruptibles; /** - * Watches a directory for chain XML files and loads and unloads + * Watches a directory for chain configuration files and loads and unloads * {@link Reaction}s from the {@link Engine}. * * @author David ‘Bombe’ Roden @@ -49,13 +53,16 @@ public class ChainWatcher extends AbstractExecutionThreadService { /** The logger. */ private static final Logger logger = Logger.getLogger(ChainWatcher.class); + /** The JSON object mapper. */ + private static final ObjectMapper objectMapper = new ObjectMapper(); + /** The reaction loader. */ private final ReactionLoader reactionLoader = new ReactionLoader(); /** The engine to load reactions with. */ private final Engine engine; - /** The directory to watch for chain XML files. */ + /** The directory to watch for chain configuration files. */ private final String directory; /** @@ -93,23 +100,27 @@ public class ChainWatcher extends AbstractExecutionThreadService { continue; } - /* list all files, scan for XMLs. */ + /* list all files, scan for configuration files. */ logger.debug(String.format("Scanning %s...", directory)); - File[] xmlFiles = directoryFile.listFiles(new FilenameFilter() { + File[] configurationFiles = directoryFile.listFiles(new FilenameFilter() { @Override public boolean accept(File dir, String name) { - return name.endsWith(".xml"); + return name.endsWith(".json"); } }); - logger.debug(String.format("Found %d XML file(s), parsing...", xmlFiles.length)); + logger.debug(String.format("Found %d configuration file(s), parsing...", configurationFiles.length)); /* now parse all XML files. */ Map chains = new HashMap(); - for (File xmlFile : xmlFiles) { + for (File configurationFile : configurationFiles) { /* parse XML file. */ - Chain chain = parseXmlFile(xmlFile); + Chain chain = parseConfigurationFile(configurationFile); + if (chain == null) { + logger.warn(String.format("Could not parse %s.", configurationFile)); + continue; + } /* dump chain */ logger.debug(String.format(" Enabled: %s", chain.enabled())); @@ -133,7 +144,7 @@ public class ChainWatcher extends AbstractExecutionThreadService { logger.debug(String.format(" Parameter: %s=%s", parameter.name(), parameter.value())); } - chains.put(xmlFile.getName(), chain); + chains.put(getReactionName(configurationFile.getName()), chain); } /* filter enabled chains. */ @@ -147,6 +158,7 @@ public class ChainWatcher extends AbstractExecutionThreadService { logger.debug(String.format("Found %d enabled Chain(s).", enabledChains.size())); /* check for removed chains. */ + Set chainsToRemove = new HashSet(); for (Entry loadedChain : loadedChains.entrySet()) { /* skip chains that still exist. */ @@ -156,7 +168,12 @@ public class ChainWatcher extends AbstractExecutionThreadService { logger.info(String.format("Removing Chain: %s", loadedChain.getKey())); engine.removeReaction(loadedChain.getKey()); - loadedChains.remove(loadedChain.getKey()); + chainsToRemove.add(loadedChain.getKey()); + } + + /* remove removed chains from loaded chains. */ + for (String reactionName : chainsToRemove) { + loadedChains.remove(reactionName); } /* check for new chains. */ @@ -184,16 +201,34 @@ public class ChainWatcher extends AbstractExecutionThreadService { // /** - * Parses the given XML file into a {@link Chain}. + * Parses the given configuration file into a {@link Chain}. * - * @param xmlFile - * The XML file to parse + * @param configurationFile + * The configuration file to parse * @return The parsed chain */ - private static Chain parseXmlFile(File xmlFile) { - Serializer serializer = new Persister(); - logger.debug(String.format("Reading %s...", xmlFile.getPath())); - return serializer.read(Chain.class, xmlFile); + private static Chain parseConfigurationFile(File configurationFile) { + try { + return objectMapper.readValue(configurationFile, Chain.class); + } catch (JsonParseException jpe1) { + logger.warn(String.format("Could not parse %s.", configurationFile), jpe1); + } catch (JsonMappingException jme1) { + logger.warn(String.format("Could not parse %s.", configurationFile), jme1); + } catch (IOException ioe1) { + logger.info(String.format("Could not read %s.", configurationFile)); + } + return null; + } + + /** + * Extracts the name of the reaction from the given filename. + * + * @param filename + * The filename to extract the reaction name from + * @return The name of the reaction + */ + private static String getReactionName(String filename) { + return (filename.lastIndexOf(".") > -1) ? filename.substring(0, filename.lastIndexOf(".")) : filename; } }