From aec39c5d7e922ad75feb97426d323e4b901b3ebb Mon Sep 17 00:00:00 2001 From: Julian Pellico Date: Fri, 1 Aug 2014 15:10:46 -0700 Subject: [PATCH] Modify FilterListener to allow multiple values for an attribute (to form a whitelist) --- .../org/lwes/listener/FilterListener.java | 46 +++++++++++++------ .../org/lwes/listener/FilterListenerTest.java | 30 ++++++------ 2 files changed, 50 insertions(+), 26 deletions(-) diff --git a/src/main/java/org/lwes/listener/FilterListener.java b/src/main/java/org/lwes/listener/FilterListener.java index 66ef5cf..f87f2d7 100644 --- a/src/main/java/org/lwes/listener/FilterListener.java +++ b/src/main/java/org/lwes/listener/FilterListener.java @@ -10,6 +10,7 @@ package org.lwes.listener; /** * This is a filtering multicast listener. It can filter by event name or any attribute of an event. + * Specifying an attribute name more than once with multiple values creates a whitelist. * * Show only Test::Event events * java org.lwes.listener.FilterListener -m 224.0.0.1 -p 6001 -e "Test::Event" @@ -17,6 +18,9 @@ * Show only events with eid=1234 AND aid=someapp * java org.lwes.listener.FilterListener -m 224.0.0.1 -p 6001 -a "eid=1234,aid=someapp" * + * Show only events with aid=someapp OR aid=anotherapp + * java org.lwes.listener.FilterListener -m 224.0.0.1 -p 6001 -a "eid=1234,aid=someapp,aid=anotherapp" + * * Events are printed to stdout one per line. * * User: frank.maritato @@ -33,11 +37,7 @@ import java.net.InetAddress; import java.net.UnknownHostException; -import java.util.Collections; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; +import java.util.*; public class FilterListener implements EventHandler { @@ -59,7 +59,7 @@ public class FilterListener implements EventHandler { private String attrList = null; private List eventNames; - private Map eventAttrs; + private Map> eventAttrs; public void processArguments(String[] args) { CmdLineParser parser = new CmdLineParser(this); @@ -72,12 +72,19 @@ public void processArguments(String[] args) { throw new RuntimeException(e.getMessage(), e); } if (attrList != null && !attrList.isEmpty()) { - eventAttrs = new HashMap(); + eventAttrs = new HashMap>(); String[] kvList = attrList.split(","); for (String kv : kvList) { String[] pairs = kv.trim().split("="); if (pairs.length == 2) { - eventAttrs.put(pairs[0].trim(), pairs[1].trim()); + if (eventAttrs.containsKey(pairs[0].trim())) { + eventAttrs.get(pairs[0].trim()).add(pairs[1].trim()); + } + else { + HashSet hashSet = new HashSet(); + hashSet.add(pairs[1].trim()); + eventAttrs.put(pairs[0].trim(), hashSet); + } } else { log.warn(kv + " not a valid k=v pair"); @@ -122,12 +129,25 @@ public static void main(String[] args) throws UnknownHostException, new FilterListener().run(args); } - public Map getEventAttrs() { + public Map> getEventAttrs() { return eventAttrs; } - public void setEventAttrs(Map eventAttrs) { - this.eventAttrs = eventAttrs; + /** + * You can still use setEventAttrs the old way by passing an int as the second parameter, + * that way you would not have to rewrite your code if you were using this method + */ + public void setEventAttrs(Map eventAttrs, int ignored) { + this.eventAttrs = new HashMap>(); + for (String key : eventAttrs.keySet()) { + Set valSet = new HashSet(1); + valSet.add(eventAttrs.get(key)); + this.eventAttrs.put(key, valSet); + } + } + + public void setEventAttrs(Map> eventAttrSets) { + eventAttrs = eventAttrSets; } public List getEventNames() { @@ -154,10 +174,10 @@ public Event match(Event event) { if (eventAttrs != null) { for (String key : eventAttrs.keySet()) { Object o = event.get(key); - String val = eventAttrs.get(key); + Set vals = eventAttrs.get(key); // If more than one kv pair is submitted, events must meet // all conditions. If any of them fail, return. - if (!(o != null && val.equals(o.toString()))) { + if (!(o != null && vals.contains(o.toString()))) { return null; } } diff --git a/src/test/java/org/lwes/listener/FilterListenerTest.java b/src/test/java/org/lwes/listener/FilterListenerTest.java index ec1ed0f..c5e5aed 100644 --- a/src/test/java/org/lwes/listener/FilterListenerTest.java +++ b/src/test/java/org/lwes/listener/FilterListenerTest.java @@ -15,9 +15,7 @@ import java.io.ByteArrayOutputStream; import java.io.PrintStream; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import org.junit.After; import org.junit.Before; @@ -65,15 +63,17 @@ public void testManuallySet() { "-m", "224.0.0.0", "-p", "0000" }); - Map inattrs = new HashMap(); - inattrs.put("eid", "1"); + Map> inattrs = new HashMap>(); + Set s = new TreeSet(); + s.add("1"); + inattrs.put("eid", s); filterListener.setEventAttrs(inattrs); // Verify the argument was parsed properly - Map attrs = filterListener.getEventAttrs(); + Map> attrs = filterListener.getEventAttrs(); Assert.assertNotNull(attrs); Assert.assertEquals(1, attrs.size()); - Assert.assertEquals("1", attrs.get("eid")); + Assert.assertTrue(attrs.get("eid").contains("1")); filterListener.destroy(); Assert.assertNull(filterListener.getEventAttrs()); @@ -146,10 +146,11 @@ public void testSingleAttribute() { }); // Verify the argument was parsed properly - Map attrs = filterListener.getEventAttrs(); + Map> attrs = filterListener.getEventAttrs(); Assert.assertNotNull(attrs); Assert.assertEquals(1, attrs.size()); - Assert.assertEquals("1", attrs.get("eid")); + Assert.assertEquals(1, attrs.get("eid").size()); + Assert.assertTrue(attrs.get("eid").contains("1")); // Verify matching functions properly MapEvent evt = new MapEvent("Test::One"); @@ -170,15 +171,18 @@ public void testMultipleAttributes() { new String[]{ "-m", "224.0.0.0", "-p", "0000", - "-a", "eid=1,pid=5" + "-a", "eid=1,pid=5,eid=z" }); // Verify the argument was parsed properly - Map attrs = filterListener.getEventAttrs(); + Map> attrs = filterListener.getEventAttrs(); Assert.assertNotNull(attrs); Assert.assertEquals(2, attrs.size()); - Assert.assertEquals("1", attrs.get("eid")); - Assert.assertEquals("5", attrs.get("pid")); + Assert.assertEquals(2, attrs.get("eid").size()); + Assert.assertTrue(attrs.get("eid").contains("1")); + Assert.assertTrue(attrs.get("eid").contains("z")); + Assert.assertEquals(1, attrs.get("pid").size()); + Assert.assertTrue(attrs.get("pid").contains("5")); // Verify multiple arguments get matched properly MapEvent evt = new MapEvent("Test::One");