View Javadoc

1   package net.sf.provisioner.config;
2   
3   import java.util.Hashtable;
4   import java.util.Iterator;
5   import java.util.List;
6   import java.util.Vector;
7   
8   import net.sf.provisioner.utils.PathHelper;
9   
10  import org.apache.log4j.Logger;
11  import org.jdom.Document;
12  import org.jdom.Element;
13  import org.jdom.input.SAXBuilder;
14  
15  
16  /**
17   * Esta clase representa la configuracion del sistema.
18   * Contiene las reglas (tipo de operaciones, servicios), 
19   * las logicas de ruteo y los datos necesarios para acceder 
20   * a la base de datos donde recide la cola de operaciones.
21   * 
22   *             
23   * @version $Revision: 1.1.2.1 $, $Date: 2007/11/12 01:57:22 $
24   * @author Gonzalo Espert
25   */
26  public class Configuration {
27  	
28  	/** Logger for this class and subclasses */
29  	Logger logger = Logger.getLogger(getClass());
30  	
31  	public Vector operationTypes = new Vector();
32  	
33  	public Hashtable rules = new Hashtable();
34  	
35  	public Hashtable services = new Hashtable();
36  	
37  	//for more than one database
38  	//public Hashtable dbs = new Hashtable();
39  	public DataBase db = new DataBase();
40  	
41  	String rulesFile = "rules.xml";
42  	
43  	String servicesFile = "services.xml";
44  	
45  	String dbsFile = "database.xml";
46  	
47  	String configPath;
48  	
49  
50  	/**
51       * .
52       * <p>
53       * 
54       * .
55       * <p>
56       * 
57       * 
58       * 
59       * @param 
60       *            
61       * @throws 
62       *            
63       *           
64       */
65  	public Configuration(String configPath) {
66  	
67  		this.configPath = configPath;
68  		
69  		logger.info("Loading system configuration...........");
70  		
71  		setDataBases();
72  		
73  		setOperationTypes();
74  		
75  		setRules();
76  		
77  		setServices();
78  	}
79  	
80  	/**
81       * Set operations types from rules.xml.
82       * <p>
83       * 
84       * .
85       * <p>
86       * 
87       * 
88       * 
89       * @param 
90       *            
91       * @throws 
92       *            
93       *           
94       */
95  	void setOperationTypes() {
96  	
97  		logger.info("Loading operation types...........");
98      	try {
99  		      Document d = new SAXBuilder().build(PathHelper.pathToStream(configPath + this.rulesFile)); 
100 		      List children = d.getContent();  
101 			  Iterator iterator = children.iterator();
102 			  while (iterator.hasNext()) {
103 				  Element child = (Element) iterator.next();
104 				  setOperations(child);
105 			  }
106 		      
107 	    } catch (Exception e) {
108 	    	logger.fatal("Error reading rules configuration file " + configPath + this.rulesFile);
109 	    	e.printStackTrace();
110 	    }
111 	}
112 	
113 	/**
114      * .
115      * <p>
116      * 
117      * .
118      * <p>
119      * 
120      * 
121      * 
122      * @param 
123      *            
124      * @throws 
125      *            
126      *           
127      */
128 	void setOperations(Element current) {
129 		
130 	    List children = current.getChildren();
131 	    Iterator iterator = children.iterator();
132 	    while (iterator.hasNext()) {
133 	      Element child = (Element) iterator.next();
134 	      if (child.getName().equalsIgnoreCase("rule"))
135 	    	  this.operationTypes.addElement(child.getAttributeValue("operation"));
136 	      setOperations(child);
137 	    }
138 	}
139 	
140 	/**
141      * .
142      * <p>
143      * 
144      * .
145      * <p>
146      * 
147      * 
148      * 
149      * @param 
150      *            
151      * @throws 
152      *            
153      *           
154      */
155 	void setRules() {
156 		for (int i = 0; i < this.operationTypes.size(); i++) {
157 			Rule rule = new Rule((String) operationTypes.elementAt(i), configPath + this.rulesFile);
158 			logger.trace("Created rule " + rule);
159 			this.rules.put(new Integer(i), rule);
160 		}
161 	}
162 	
163 	/**
164      * .
165      * <p>
166      * 
167      * .
168      * <p>
169      * 
170      * 
171      * 
172      * @param 
173      *            
174      * @throws 
175      *            
176      *           
177      */
178 	void setServices() {
179 		
180 		logger.info("Loading service routing logic configuration");
181 	    try {
182 		      Document d = new SAXBuilder().build(PathHelper.pathToStream(configPath + this.servicesFile)); 
183 		      List children = d.getContent();  
184 			  Iterator iterator = children.iterator();
185 			  while (iterator.hasNext()) {
186 			      Element child = (Element) iterator.next();
187 			      getCriteria(child, 0);
188 			  }
189 	    } catch (Exception e) {
190 	    	logger.fatal("Error reading services configuration file " + configPath + this.servicesFile);
191 	    	e.printStackTrace();
192 	    }
193 		
194 	}
195 	
196 	/**
197      * .
198      * <p>
199      * 
200      * .
201      * <p>
202      * 
203      * 
204      * 
205      * @param 
206      *            
207      * @throws 
208      *            
209      *           
210      */
211 	void getCriteria(Element current, int i) {
212 		
213 	    List children = current.getChildren();
214 	    Iterator iterator = children.iterator();
215 	    while (iterator.hasNext()) {
216 	      Element child = (Element) iterator.next();
217 	      if (child.getName().equalsIgnoreCase("service")) {
218 	    	  logger.info("Creating routing criteria for service: " + child.getAttributeValue("name"));
219 	    	  RoutingCriteria routingCriteria = new RoutingCriteria(
220 	    			  child.getAttributeValue("routing_table"),
221 	    			  child.getAttributeValue("routing_data")
222               );
223 	    	  ConfigService service = new ConfigService(child.getAttributeValue("name"), routingCriteria, configPath);
224 	    	  logger.trace("Created service " + service);
225 	    	  this.services.put(new Integer(i++), service);
226 	      }
227 	      getCriteria(child, i);
228 	    }   
229 	}
230 	
231 	/**
232      * Set operations types from rules.xml.
233      * <p>
234      * 
235      * .
236      * <p>
237      * 
238      * 
239      * 
240      * @param 
241      *            
242      * @throws 
243      *            
244      *           
245      */
246 	void setDataBases() {
247 	
248 		logger.info("Loading data base access configuration...........");
249     	try {
250 		      Document d = new SAXBuilder().build(PathHelper.pathToStream(configPath + this.dbsFile)); 
251 		      List children = d.getContent();  
252 			  Iterator iterator = children.iterator();
253 			  while (iterator.hasNext()) {
254 				  Element child = (Element) iterator.next();
255 				  setDataBase(child);
256 			  }
257 		      
258 	    } catch (Exception e) {
259 	    	logger.fatal("Error reading data base access configuration file " + configPath + this.dbsFile);
260 	    	e.printStackTrace();
261 	    }
262 	}
263 	
264 	/**
265      * .
266      * <p>
267      * 
268      * .
269      * <p>
270      * 
271      * 
272      * 
273      * @param current
274      *            
275      *           
276      */
277 	void setDataBase(Element current) {
278 		
279 	    List children = current.getChildren();
280 	    Iterator iterator = children.iterator();
281 	    while (iterator.hasNext()) {
282 	      Element child = (Element) iterator.next();
283 	      if (child.getName().equalsIgnoreCase("server")) {
284 	    	  db.server.name = child.getAttributeValue("name");
285 	    	  db.server.type = child.getAttributeValue("type");
286 	      }
287 
288 	      if (child.getName().equalsIgnoreCase("driver")) {
289 	    	  db.driver.className = child.getAttributeValue("className");
290 	    	  db.driver.type = child.getAttributeValue("type");
291 	      }
292 	      
293 	      if (child.getName().equalsIgnoreCase("database")) {
294 	    	  db.name = child.getAttributeValue("name");
295 	    	  db.user = child.getAttributeValue("user");
296 	    	  db.password = child.getAttributeValue("password");
297 	      }
298 	      
299 	      setDataBase(child);
300 	    }
301 	}
302 }