3d30f2615edfc140405d9237064e891134b05cdd
[rhynodge.git] / src / main / java / net / pterodactylus / reactor / loader / ChainWatcher.java
1 /*
2  * Reactor - ChainWatcher.java - Copyright © 2013 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.reactor.loader;
19
20 import java.io.File;
21 import java.io.FilenameFilter;
22 import java.io.IOException;
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.Map;
26 import java.util.Map.Entry;
27 import java.util.Set;
28 import java.util.concurrent.TimeUnit;
29
30 import net.pterodactylus.reactor.Reaction;
31 import net.pterodactylus.reactor.engine.Engine;
32 import net.pterodactylus.reactor.loader.Chain.Parameter;
33 import net.pterodactylus.reactor.loader.Chain.Part;
34
35 import org.apache.log4j.Logger;
36
37 import com.fasterxml.jackson.core.JsonParseException;
38 import com.fasterxml.jackson.databind.JsonMappingException;
39 import com.fasterxml.jackson.databind.ObjectMapper;
40 import com.google.common.base.Predicate;
41 import com.google.common.collect.Maps;
42 import com.google.common.util.concurrent.AbstractExecutionThreadService;
43 import com.google.common.util.concurrent.Uninterruptibles;
44
45 /**
46  * Watches a directory for chain configuration files and loads and unloads
47  * {@link Reaction}s from the {@link Engine}.
48  *
49  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
50  */
51 public class ChainWatcher extends AbstractExecutionThreadService {
52
53         /** The logger. */
54         private static final Logger logger = Logger.getLogger(ChainWatcher.class);
55
56         /** The JSON object mapper. */
57         private static final ObjectMapper objectMapper = new ObjectMapper();
58
59         /** The reaction loader. */
60         private final ReactionLoader reactionLoader = new ReactionLoader();
61
62         /** The engine to load reactions with. */
63         private final Engine engine;
64
65         /** The directory to watch for chain configuration files. */
66         private final String directory;
67
68         /**
69          * Creates a new chain watcher.
70          *
71          * @param engine
72          *            The engine to load reactions with
73          * @param directory
74          *            The directory to watch
75          */
76         public ChainWatcher(Engine engine, String directory) {
77                 this.engine = engine;
78                 this.directory = directory;
79         }
80
81         //
82         // ABSTRACTEXECUTIONTHREADSERVICE METHODS
83         //
84
85         /**
86          * {@inheritDoc}
87          */
88         @Override
89         protected void run() throws Exception {
90
91                 /* loaded chains. */
92                 final Map<String, Chain> loadedChains = new HashMap<String, Chain>();
93
94                 while (isRunning()) {
95
96                         /* check if directory is there. */
97                         File directoryFile = new File(directory);
98                         if (!directoryFile.exists() || !directoryFile.isDirectory() || !directoryFile.canRead()) {
99                                 Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS);
100                                 continue;
101                         }
102
103                         /* list all files, scan for configuration files. */
104                         logger.debug(String.format("Scanning %s...", directory));
105                         File[] configurationFiles = directoryFile.listFiles(new FilenameFilter() {
106
107                                 @Override
108                                 public boolean accept(File dir, String name) {
109                                         return name.endsWith(".json");
110                                 }
111                         });
112                         logger.debug(String.format("Found %d configuration file(s), parsing...", configurationFiles.length));
113
114                         /* now parse all XML files. */
115                         Map<String, Chain> chains = new HashMap<String, Chain>();
116                         for (File configurationFile : configurationFiles) {
117
118                                 /* parse XML file. */
119                                 Chain chain = parseConfigurationFile(configurationFile);
120                                 if (chain == null) {
121                                         logger.warn(String.format("Could not parse %s.", configurationFile));
122                                         continue;
123                                 }
124
125                                 /* dump chain */
126                                 logger.debug(String.format(" Enabled: %s", chain.enabled()));
127
128                                 logger.debug(String.format(" Query: %s", chain.query().name()));
129                                 for (Parameter parameter : chain.query().parameters()) {
130                                         logger.debug(String.format("  Parameter: %s=%s", parameter.name(), parameter.value()));
131                                 }
132                                 for (Part filter : chain.filters()) {
133                                         logger.debug(String.format(" Filter: %s", filter.name()));
134                                         for (Parameter parameter : filter.parameters()) {
135                                                 logger.debug(String.format("  Parameter: %s=%s", parameter.name(), parameter.value()));
136                                         }
137                                 }
138                                 logger.debug(String.format(" Trigger: %s", chain.trigger().name()));
139                                 for (Parameter parameter : chain.trigger().parameters()) {
140                                         logger.debug(String.format("  Parameter: %s=%s", parameter.name(), parameter.value()));
141                                 }
142                                 logger.debug(String.format(" Action: %s", chain.action().name()));
143                                 for (Parameter parameter : chain.action().parameters()) {
144                                         logger.debug(String.format("  Parameter: %s=%s", parameter.name(), parameter.value()));
145                                 }
146
147                                 chains.put(getReactionName(configurationFile.getName()), chain);
148                         }
149
150                         /* filter enabled chains. */
151                         Map<String, Chain> enabledChains = Maps.filterEntries(chains, new Predicate<Entry<String, Chain>>() {
152
153                                 @Override
154                                 public boolean apply(Entry<String, Chain> chainEntry) {
155                                         return chainEntry.getValue().enabled();
156                                 }
157                         });
158                         logger.debug(String.format("Found %d enabled Chain(s).", enabledChains.size()));
159
160                         /* check for removed chains. */
161                         Set<String> chainsToRemove = new HashSet<String>();
162                         for (Entry<String, Chain> loadedChain : loadedChains.entrySet()) {
163
164                                 /* skip chains that still exist. */
165                                 if (enabledChains.containsKey(loadedChain.getKey())) {
166                                         continue;
167                                 }
168
169                                 logger.info(String.format("Removing Chain: %s", loadedChain.getKey()));
170                                 engine.removeReaction(loadedChain.getKey());
171                                 chainsToRemove.add(loadedChain.getKey());
172                         }
173
174                         /* remove removed chains from loaded chains. */
175                         for (String reactionName : chainsToRemove) {
176                                 loadedChains.remove(reactionName);
177                         }
178
179                         /* check for new chains. */
180                         for (Entry<String, Chain> enabledChain : enabledChains.entrySet()) {
181
182                                 /* skip already loaded chains. */
183                                 if (loadedChains.containsValue(enabledChain.getValue())) {
184                                         continue;
185                                 }
186
187                                 logger.info(String.format("Loading new Chain: %s", enabledChain.getKey()));
188
189                                 Reaction reaction = reactionLoader.loadReaction(enabledChain.getValue());
190                                 engine.addReaction(enabledChain.getKey(), reaction);
191                                 loadedChains.put(enabledChain.getKey(), enabledChain.getValue());
192                         }
193
194                         /* wait before checking again. */
195                         Uninterruptibles.sleepUninterruptibly(5, TimeUnit.SECONDS);
196                 }
197         }
198
199         //
200         // STATIC METHODS
201         //
202
203         /**
204          * Parses the given configuration file into a {@link Chain}.
205          *
206          * @param configurationFile
207          *            The configuration file to parse
208          * @return The parsed chain
209          */
210         private static Chain parseConfigurationFile(File configurationFile) {
211                 try {
212                         return objectMapper.readValue(configurationFile, Chain.class);
213                 } catch (JsonParseException jpe1) {
214                         logger.warn(String.format("Could not parse %s.", configurationFile), jpe1);
215                 } catch (JsonMappingException jme1) {
216                         logger.warn(String.format("Could not parse %s.", configurationFile), jme1);
217                 } catch (IOException ioe1) {
218                         logger.info(String.format("Could not read %s.", configurationFile));
219                 }
220                 return null;
221         }
222
223         /**
224          * Extracts the name of the reaction from the given filename.
225          *
226          * @param filename
227          *            The filename to extract the reaction name from
228          * @return The name of the reaction
229          */
230         private static String getReactionName(String filename) {
231                 return (filename.lastIndexOf(".") > -1) ? filename.substring(0, filename.lastIndexOf(".")) : filename;
232         }
233
234 }