KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > webapp > balancer > RuleChain


1 /*
2  * Copyright 2000,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.webapp.balancer;
17
18 import java.net.URL JavaDoc;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25
26
27 /**
28  * A RuleChain is a list of rules
29  * considered in order. The first
30  * rule to succeed stops the evaluation
31  * of rules.
32  *
33  * @author Yoav Shapira
34  */

35 public class RuleChain {
36     /**
37      * The list of rules to evaluate.
38      */

39     private List JavaDoc rules;
40
41     /**
42      * Constructor.
43      */

44     public RuleChain() {
45         rules = new ArrayList JavaDoc();
46     }
47
48     /**
49      * Returns the list of rules
50      * to evaluate.
51      *
52      * @return List
53      */

54     protected List JavaDoc getRules() {
55         return rules;
56     }
57
58     /**
59      * Returns an iterator over
60      * the list of rules to evaluate.
61      *
62      * @return Iterator
63      */

64     protected Iterator JavaDoc getRuleIterator() {
65         return getRules().iterator();
66     }
67
68     /**
69      * Adds a rule to evaluate.
70      *
71      * @param theRule The rule to add
72      */

73     public void addRule(Rule theRule) {
74         if (theRule == null) {
75             throw new IllegalArgumentException JavaDoc("The rule cannot be null.");
76         } else {
77             getRules().add(theRule);
78         }
79     }
80
81     /**
82      * Evaluates the given request to see if
83      * any of the rules matches. Returns the
84      * redirect URL for the first matching
85      * rule. Returns null if no rules match
86      * the request.
87      *
88      * @param request The request
89      * @return URL The first matching rule URL
90      * @see Rule#matches(HttpServletRequest)
91      */

92     public URL JavaDoc evaluate(HttpServletRequest JavaDoc request) {
93         Iterator JavaDoc 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 JavaDoc(currentRule.getRedirectUrl());
105                 } catch (Exception JavaDoc e) {
106                     throw new RuntimeException JavaDoc(e);
107                 }
108             }
109         }
110
111         return null;
112     }
113
114     /**
115      * Returns a String representation of this object.
116      *
117      * @return String
118      */

119     public String JavaDoc toString() {
120         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
121
122         buffer.append("[");
123         buffer.append(getClass().getName());
124         buffer.append(": ");
125
126         Iterator JavaDoc 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 // End of file: RuleChain.java
146
Popular Tags