1 16 package org.apache.webapp.balancer; 17 18 import java.net.URL ; 19 20 import java.util.ArrayList ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 24 import javax.servlet.http.HttpServletRequest ; 25 26 27 35 public class RuleChain { 36 39 private List rules; 40 41 44 public RuleChain() { 45 rules = new ArrayList (); 46 } 47 48 54 protected List getRules() { 55 return rules; 56 } 57 58 64 protected Iterator getRuleIterator() { 65 return getRules().iterator(); 66 } 67 68 73 public void addRule(Rule theRule) { 74 if (theRule == null) { 75 throw new IllegalArgumentException ("The rule cannot be null."); 76 } else { 77 getRules().add(theRule); 78 } 79 } 80 81 92 public URL evaluate(HttpServletRequest request) { 93 Iterator iter = getRuleIterator(); 94 95 Rule currentRule = null; 96 boolean currentMatches = false; 97 98 while (iter.hasNext()) { 99 currentRule = (Rule) iter.next(); 100 currentMatches = currentRule.matches(request); 101 102 if (currentMatches) { 103 try { 104 return new URL (currentRule.getRedirectUrl()); 105 } catch (Exception e) { 106 throw new RuntimeException (e); 107 } 108 } 109 } 110 111 return null; 112 } 113 114 119 public String toString() { 120 StringBuffer buffer = new StringBuffer (); 121 122 buffer.append("["); 123 buffer.append(getClass().getName()); 124 buffer.append(": "); 125 126 Iterator iter = getRuleIterator(); 127 Rule currentRule = null; 128 129 while (iter.hasNext()) { 130 currentRule = (Rule) iter.next(); 131 buffer.append(currentRule); 132 133 if (iter.hasNext()) { 134 buffer.append(", "); 135 } 136 } 137 138 buffer.append("]"); 139 140 return buffer.toString(); 141 } 142 } 143 144 145 | Popular Tags |