1 package net.sf.provisioner.config;
2
3 import java.util.*;
4
5 import net.sf.provisioner.utils.PathHelper;
6
7 import org.jdom.Document;
8 import org.jdom.Element;
9 import org.jdom.input.SAXBuilder;
10
11 import org.apache.log4j.Logger;
12
13
14
15
16
17
18
19
20
21 public class Rule {
22
23
24 Logger logger = Logger.getLogger(getClass());
25
26
27
28
29 public String operation;
30
31
32
33
34 String title;
35
36
37
38
39 public Hashtable requests = new Hashtable();
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 public Rule(String operation, String rulesFile) {
56
57
58
59
60 this.operation = operation;
61
62
63
64
65 logger.info("Configuring requests for " + operation + " operation.");
66 try {
67 Document d = new SAXBuilder().build(PathHelper.pathToStream(rulesFile));
68 List children = d.getContent();
69 Iterator iterator = children.iterator();
70 while (iterator.hasNext()) {
71 Element child = (Element) iterator.next();
72 getRuleRequest(child, "", 0);
73 }
74 } catch (Exception e) {
75 logger.fatal("Error reading rules configuration file " + rulesFile);
76 e.printStackTrace();
77 }
78 }
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95 void getRuleRequest(Element current, String operationID, int i) {
96
97 List children = current.getChildren();
98 Iterator iterator = children.iterator();
99 while (iterator.hasNext()) {
100 Element child = (Element) iterator.next();
101 if (child.getName().equalsIgnoreCase("rule"))
102 operationID = child.getAttributeValue("operation");
103
104
105
106
107 if (this.operation.equalsIgnoreCase(operationID)) {
108
109
110
111
112 if (child.getName().equalsIgnoreCase("title")) this.title = child.getText();
113
114
115
116
117 if (child.getName().equalsIgnoreCase("request")) {
118
119 this.requests.put(new Integer(i++),
120 new ConfigRequest(child.getAttributeValue("service"), child.getAttributeValue("type")));
121 }
122 }
123 getRuleRequest(child, operationID, 0);
124 }
125 }
126
127 public String toString() {
128 return "operation=" + operation + ", title=" + title +", requests=[" + requests + "]";
129 }
130 }