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 * Esta clase representa una regla. Contiene tantas instancias de la
15 * clase config.Request como lo indique la configuracion de la regla
16 * en el archivo de configuracion de reglas.
17 *
18 * @version $Revision: 1.1.2.1 $, $Date: 2007/11/12 01:57:22 $
19 * @author Gonzalo Espert
20 */
21 public class Rule {
22
23 /** Logger for this class and subclasses */
24 Logger logger = Logger.getLogger(getClass());
25
26 /**
27 * Codigo de operacion de la regla
28 */
29 public String operation;
30
31 /**
32 * Titulo de la regla
33 */
34 String title;
35
36 /**
37 * Tabla de requerimientos de la regla
38 */
39 public Hashtable requests = new Hashtable();
40
41 /**
42 * Constructor de una regla utilizando como entrada un codigo de
43 * operacion para saber que operacion leer del archivo de configuracion
44 * de reglas y el nombre de dicho archivo.
45 *
46 *
47 * @param operation
48 * Codigo de la operacion para hacer el fetch en el
49 * archivo de configuracion de reglas
50 * @param rulesFile
51 * Nombre del archivo donde residen las reglas del
52 * sistema
53 *
54 */
55 public Rule(String operation, String rulesFile) {
56
57 /**
58 * Setea el codigo de operacion
59 */
60 this.operation = operation;
61
62 /**
63 * Setea el titulo y carga los requerimientos
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 * Metodo que carga una por una los requests correspondientes
82 * a la regla utilizando como dato de entrada los nodos del
83 * archivo de configuracion de reglas y el codigo de operacion
84 * que corresponde a la regla.
85 *
86 *
87 * @param current
88 * Nodos del archivo Rules.xml
89 * @param operationID
90 * Codigo de operacion de la regla
91 * @param i
92 * Indice para cada request
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 * Si se trata del tipo de operacion correcta
106 */
107 if (this.operation.equalsIgnoreCase(operationID)) {
108
109 /**
110 * Setea el titulo de la regla
111 */
112 if (child.getName().equalsIgnoreCase("title")) this.title = child.getText();
113
114 /**
115 * Carga los requerimientos
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 } // end while
125 }
126
127 public String toString() {
128 return "operation=" + operation + ", title=" + title +", requests=[" + requests + "]";
129 }
130 }