Parse and load watchers from configuration.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 11 Jan 2013 06:21:47 +0000 (07:21 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 11 Jan 2013 06:21:47 +0000 (07:21 +0100)
src/main/java/net/pterodactylus/rhynodge/loader/Chain.java
src/main/java/net/pterodactylus/rhynodge/loader/ChainWatcher.java
src/main/java/net/pterodactylus/rhynodge/loader/ReactionLoader.java

index 8fc6c35..2587d09 100644 (file)
@@ -184,6 +184,10 @@ public class Chain {
        @JsonProperty
        private Part trigger;
 
+       /** A combination of query, filters, and a trigger. */
+       @JsonProperty
+       private Part watcher;
+
        /** The action of the chain. */
        @JsonProperty
        private Part action;
@@ -238,6 +242,15 @@ public class Chain {
        }
 
        /**
+        * Returns an optional watcher.
+        *
+        * @return The watcher of this chain
+        */
+       public Part watcher() {
+               return watcher;
+       }
+
+       /**
         * Returns the action of this chain.
         *
         * @return The action of this chain
index 52b3fa4..71320ff 100644 (file)
@@ -125,19 +125,23 @@ public class ChainWatcher extends AbstractExecutionThreadService {
                                /* dump chain */
                                logger.debug(String.format(" Enabled: %s", chain.enabled()));
 
-                               logger.debug(String.format(" Query: %s", chain.query().name()));
-                               for (Parameter parameter : chain.query().parameters()) {
-                                       logger.debug(String.format("  Parameter: %s=%s", parameter.name(), parameter.value()));
-                               }
-                               for (Part filter : chain.filters()) {
-                                       logger.debug(String.format(" Filter: %s", filter.name()));
-                                       for (Parameter parameter : filter.parameters()) {
+                               if (chain.watcher() != null) {
+                                       logger.debug(String.format("Reaction: %s", chain.watcher().name()));
+                               } else {
+                                       logger.debug(String.format(" Query: %s", chain.query().name()));
+                                       for (Parameter parameter : chain.query().parameters()) {
+                                               logger.debug(String.format("  Parameter: %s=%s", parameter.name(), parameter.value()));
+                                       }
+                                       for (Part filter : chain.filters()) {
+                                               logger.debug(String.format(" Filter: %s", filter.name()));
+                                               for (Parameter parameter : filter.parameters()) {
+                                                       logger.debug(String.format("  Parameter: %s=%s", parameter.name(), parameter.value()));
+                                               }
+                                       }
+                                       logger.debug(String.format(" Trigger: %s", chain.trigger().name()));
+                                       for (Parameter parameter : chain.trigger().parameters()) {
                                                logger.debug(String.format("  Parameter: %s=%s", parameter.name(), parameter.value()));
                                        }
-                               }
-                               logger.debug(String.format(" Trigger: %s", chain.trigger().name()));
-                               for (Parameter parameter : chain.trigger().parameters()) {
-                                       logger.debug(String.format("  Parameter: %s=%s", parameter.name(), parameter.value()));
                                }
                                logger.debug(String.format(" Action: %s", chain.action().name()));
                                for (Parameter parameter : chain.action().parameters()) {
index 2cf1d21..100e42f 100644 (file)
@@ -28,6 +28,7 @@ import net.pterodactylus.rhynodge.Filter;
 import net.pterodactylus.rhynodge.Query;
 import net.pterodactylus.rhynodge.Reaction;
 import net.pterodactylus.rhynodge.Trigger;
+import net.pterodactylus.rhynodge.Watcher;
 import net.pterodactylus.rhynodge.loader.Chain.Parameter;
 import net.pterodactylus.rhynodge.loader.Chain.Part;
 
@@ -55,6 +56,15 @@ public class ReactionLoader {
                        throw new IllegalArgumentException("Chain is not enabled.");
                }
 
+               /* create action. */
+               Action action = createObject(chain.action().name(), "net.pterodactylus.rhynodge.actions", extractParameters(chain.action().parameters()));
+
+               /* do we have a reaction defined? */
+               if (chain.watcher() != null) {
+                       Watcher watcher = createObject(chain.watcher().name(), "net.pterodactylus.rhynodge.watchers", extractParameters(chain.watcher().parameters()));
+                       return new Reaction(chain.name(), watcher.query(), watcher.filters(), watcher.trigger(), action);
+               }
+
                /* create query. */
                Query query = createObject(chain.query().name(), "net.pterodactylus.rhynodge.queries", extractParameters(chain.query().parameters()));
 
@@ -67,9 +77,6 @@ public class ReactionLoader {
                /* create trigger. */
                Trigger trigger = createObject(chain.trigger().name(), "net.pterodactylus.rhynodge.triggers", extractParameters(chain.trigger().parameters()));
 
-               /* create action. */
-               Action action = createObject(chain.action().name(), "net.pterodactylus.rhynodge.actions", extractParameters(chain.action().parameters()));
-
                return new Reaction(chain.name(), query, filters, trigger, action).setUpdateInterval(TimeUnit.SECONDS.toMillis(chain.updateInterval()));
        }