aboutsummaryrefslogtreecommitdiff
path: root/src/uk/org/ury/config/ConfigReader.java
blob: ed2d8527c49670b0f04e1e0da1ae7e74ff537f7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package uk.org.ury.config;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

import uk.org.ury.database.exceptions.MissingCredentialsException;

/**
 * Reads in an XML config file and creates config objects
 * 
 * @author Nathan Lasseter
 */
public class ConfigReader {

	private Database database = null;
	private Auth roAuth = null;
	private Auth rwAuth = null;
	
	/**
	 * Get the database configuration
	 * 
	 * @return Database database
	 */
	public Database getDatabase() { return database; }
	/**
	 * Get the read only login auth configuration
	 * 
	 * @return Auth roAauth
	 */
	public Auth getRoAuth() { return roAuth; }
	/**
	 * Get the read write login auth configuration
	 * 
	 * @return Auth rwAauth
	 */
	public Auth getRwAuth() { return rwAuth; }
	
	/**
	 * Read in the config file and create the Database and Auth configuration objects.
	 * Specify a config file.
	 * @throws MissingCredentialsException 
	 */
	public ConfigReader(String configFile) throws MissingCredentialsException {
		
		try {
			DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
			Document doc = docBuilder.parse (configFile);
			doc.getDocumentElement().normalize();
						
			NodeList nList = doc.getElementsByTagName("auth");
			for(int i = 0; i < nList.getLength(); i++) {
				Node nNode = nList.item(i);
				if(nNode.getNodeType() == Node.ELEMENT_NODE) {
					Element eElement = (Element) nNode;
					String user = getTagValue("user", eElement);
					String pass = getTagValue("pass", eElement);
					String sType = getTagValue("type", eElement);
					if(sType.equalsIgnoreCase("read_only")) {
						if(roAuth != null) continue;
						roAuth = new Auth(user, pass);
					} else if(sType.equalsIgnoreCase("read_write")) {
						if(rwAuth != null) continue;
						rwAuth = new Auth(user, pass);
					} else {
						throw new IllegalArgumentException("Unused user class.");
					}
				}
			}
			
			nList = doc.getElementsByTagName("database");
			for(int i = 0; i < nList.getLength(); i++) {
				if(database != null) break;
				Node nNode = nList.item(i);
				if(nNode.getNodeType() == Node.ELEMENT_NODE) {
					Element eElement = (Element) nNode;
					String host = getTagValue("host", eElement);
					String port = getTagValue("port", eElement);
					String db = getTagValue("db", eElement);
					database = new Database(host, Integer.parseInt(port.trim()), db);
				}
			}
		}
		
		catch(NullPointerException n) {
			throw new MissingCredentialsException("An element node is empty.");
		}
		
		catch (SAXParseException err) {
			System.out.println ("** Parsing error" + ", line " + err.getLineNumber () + ", uri " + err.getSystemId ());
	        System.out.println(" " + err.getMessage ());
		}
		catch (SAXException e) {
	        Exception x = e.getException ();
	        ((x == null) ? e : x).printStackTrace ();
		}
		catch (Throwable t) {
	        t.printStackTrace ();
		}
	}
	
	private static String getTagValue(String sTag, Element eElement){
		NodeList nlList= eElement.getElementsByTagName(sTag).item(0).getChildNodes();
		Node nValue = (Node) nlList.item(0);
		
		return nValue.getNodeValue();
	}
}