1 20 21 package net.innig.macker.recording; 22 23 import net.innig.macker.event.*; 24 import net.innig.macker.rule.Rule; 25 import net.innig.macker.rule.RuleSet; 26 27 import java.io.PrintWriter ; 28 import java.util.*; 29 30 import org.jdom.Element; 31 import org.jdom.Attribute; 32 33 import net.innig.collect.CollectionDiff; 34 35 public class GenericRuleRecording 36 extends EventRecording 37 { 38 public GenericRuleRecording(EventRecording parent) 39 { 40 super(parent); 41 events = new HashSet(); 42 } 43 44 public EventRecording record(MackerEvent event) 45 { 46 if(rule == null) 47 rule = event.getRule(); 48 if(event.getRule() != rule) 49 return getParent().record(event); 50 51 Map eventAttributes = new TreeMap(); 52 eventType = event.getClass().getName(); 53 if(eventType.startsWith(DEFAULT_EVENT_PACKAGE)) 54 eventType = eventType.substring(DEFAULT_EVENT_PACKAGE.length()); 55 eventAttributes.put("type", eventType); 56 eventAttributes.put("severity", event.getRule().getSeverity().getName()); 57 int msgNum = 0; 58 for(Iterator msgIter = event.getMessages().iterator(); msgIter.hasNext(); msgNum++) 59 eventAttributes.put("message" + msgNum, msgIter.next()); 60 61 if(event instanceof MessageEvent) 62 { } else if(event instanceof AccessRuleViolation) 64 { 65 AccessRuleViolation arv = (AccessRuleViolation) event; 66 eventAttributes.put("from", arv.getFrom().getFullName()); 67 eventAttributes.put("to", arv.getTo().getFullName()); 68 } 69 else 70 throw new IllegalArgumentException ("Unknown event type: " + event); 71 72 events.add(eventAttributes); 73 74 return this; 75 } 76 77 public void read(Element elem) 78 { 79 Set eventSet = new HashSet(); 80 Map baseAtt = getAttributeValueMap(elem); 81 for(Iterator evtIter = elem.getChildren("event").iterator(); evtIter.hasNext(); ) 82 { 83 Element eventElem = (Element) evtIter.next(); 84 Map eventAtt = new TreeMap(baseAtt); 85 eventAtt.putAll(getAttributeValueMap(eventElem)); 86 eventType = (String ) eventAtt.get("type"); 87 events.add(eventAtt); 88 } 89 } 90 91 private Map getAttributeValueMap(Element elem) 92 { 93 Map attValues = new TreeMap(); 94 for(Iterator i = elem.getAttributes().iterator(); i.hasNext(); ) 95 { 96 Attribute attr = (Attribute) i.next(); 97 attValues.put(attr.getName(), attr.getValue()); 98 } 99 return attValues; 100 } 101 102 public boolean compare(EventRecording actual, PrintWriter out) 103 { 104 if(!super.compare(actual, out)) 105 return false; 106 107 boolean match = true; 108 GenericRuleRecording actualGRR = (GenericRuleRecording) actual; 109 Set expectedSet = events; 110 Set actualSet = actualGRR.events; 111 CollectionDiff diff = new CollectionDiff(expectedSet, actualSet); 112 if(!diff.getRemoved().isEmpty()) 113 { 114 out.println(this + ": missing events:"); 115 dump(out, diff.getRemoved()); 116 match = false; 117 } 118 if(!diff.getAdded().isEmpty()) 119 { 120 out.println(this + ": unexpected events:"); 121 dump(out, diff.getAdded()); 122 match = false; 123 } 124 return match; 125 } 126 127 private void dump(PrintWriter out, Collection events) 128 { 129 for(Iterator i = events.iterator(); i.hasNext(); ) 130 out.println(" " + i.next()); 131 } 132 133 public String toString() 134 { return "[rule:" + eventType + "]"; } 135 136 public void dump(PrintWriter out, int indent) 137 { 138 super.dump(out, indent); 139 for(Iterator eventIter = events.iterator(); eventIter.hasNext(); ) 140 { 141 Map event = (Map) eventIter.next(); 142 for(int n = -3; n < indent; n++) 143 out.print(' '); 144 out.println(event); 145 } 146 } 147 148 private Rule rule; 149 private String eventType; 150 private Set events; 151 private static final String DEFAULT_EVENT_PACKAGE = "net.innig.macker.event."; 152 } 153 154 155 | Popular Tags |