Source code: org/apache/webapp/balancer/RulesParser.java
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 org.apache.tomcat.util.digester.Digester;
19
20 import java.io.InputStream;
21
22
23 /**
24 * The rules parser uses Digester
25 * to parse the rules definition
26 * file and return a RuleChain object.
27 *
28 * @author Yoav Shapira
29 */
30 public class RulesParser {
31 /**
32 * The resulting rule chain.
33 */
34 private RuleChain result;
35
36 /**
37 * Constructor.
38 *
39 * @param input To read the configuration
40 */
41 public RulesParser(InputStream input) {
42 try {
43 Digester digester = createDigester();
44 result = (RuleChain) digester.parse(input);
45 } catch (Exception e) {
46 throw new RuntimeException(e);
47 }
48 }
49
50 /**
51 * Returns the parsed rule chain.
52 *
53 * @return The resulting RuleChain
54 */
55 public RuleChain getResult() {
56 return result;
57 }
58
59 /**
60 * Creates the digester instance.
61 *
62 * @return Digester
63 */
64 protected Digester createDigester() {
65 Digester digester = new Digester();
66 digester.setUseContextClassLoader(true);
67
68 String rules = "rules";
69 String rule = "/rule";
70
71 // Construct rule chain
72 digester.addObjectCreate(rules, RuleChain.class);
73
74 // Construct rule
75 digester.addObjectCreate(rules + rule, null, "className");
76
77 // Set rule properties
78 digester.addSetProperties(rules + rule);
79
80 // Add rule to chain
81 digester.addSetNext(rules + rule, "addRule", "org.apache.webapp.balancer.Rule");
82
83 return digester;
84 }
85 }
86
87
88 // End of class: RulesParser.java