diff options
author | Matt Windsor <mbw500@student.cs.york.ac.uk> | 2011-02-20 23:49:42 +0000 |
---|---|---|
committer | Matt Windsor <mbw500@student.cs.york.ac.uk> | 2011-02-20 23:49:42 +0000 |
commit | cd972012ab8c40b66bd65f78bbf9e3422413b1aa (patch) | |
tree | 726aa9cefa80fc4a2f3922ed6e05cf0074421a6d /src | |
parent | e2fc92f5c42dde942e8b71d38b9745b6f3c98053 (diff) | |
parent | f89ef9ed36e8185f53c7d5f22f91935e2c4ccaa0 (diff) |
Merge. DatabaseLogin over. Auth = Very Yes.
Diffstat (limited to 'src')
-rw-r--r-- | src/uk/org/ury/config/Auth.java | 27 | ||||
-rw-r--r-- | src/uk/org/ury/config/ConfigReader.java | 106 | ||||
-rw-r--r-- | src/uk/org/ury/config/Database.java | 26 | ||||
-rw-r--r-- | src/uk/org/ury/database/DatabaseDriver.java | 56 | ||||
-rw-r--r-- | src/uk/org/ury/database/DatabaseLogin.java | 175 | ||||
-rw-r--r-- | src/uk/org/ury/database/credentials/.gitignore | 1 | ||||
-rw-r--r-- | src/uk/org/ury/library/viewer/LibraryViewer.java | 10 |
7 files changed, 171 insertions, 230 deletions
diff --git a/src/uk/org/ury/config/Auth.java b/src/uk/org/ury/config/Auth.java index ff22c12..81be6e2 100644 --- a/src/uk/org/ury/config/Auth.java +++ b/src/uk/org/ury/config/Auth.java @@ -1,21 +1,36 @@ package uk.org.ury.config; -import uk.org.ury.database.UserClass; - +/** + * A login authorisation configuration + * + * @author Nathan Lasseter + */ public class Auth { private String user; private String pass; - private UserClass type; + /** + * Get the username of the login + * + * @return String username + */ public String getUser() { return user; } + /** + * Get the password of the login + * + * @return String password + */ public String getPass() { return pass; } - public UserClass getType() { return type; } - public Auth(String user, String pass, UserClass type) { + /** + * Create a login auth object + * @param user The username for the login + * @param pass The password for the login + */ + public Auth(String user, String pass) { this.user = user; this.pass = pass; - this.type = type; } }
\ No newline at end of file diff --git a/src/uk/org/ury/config/ConfigReader.java b/src/uk/org/ury/config/ConfigReader.java index 6b66e24..04deb49 100644 --- a/src/uk/org/ury/config/ConfigReader.java +++ b/src/uk/org/ury/config/ConfigReader.java @@ -1,49 +1,98 @@ package uk.org.ury.config; import java.io.File; -import org.w3c.dom.*; -import javax.xml.parsers.DocumentBuilderFactory; 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.UserClass; +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; - private Auth auth; + private Database database = null; + private Auth roAuth = null; + private Auth rwAuth = null; + /** + * Get the database configuration + * + * @return Database database + */ public Database getDatabase() { return database; } - public Auth getAuth() { return auth; } + /** + * 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; } - public ConfigReader() { + /** + * 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 (new File("res/conf.xml")); + Document doc = docBuilder.parse (new File(configFile)); doc.getDocumentElement().normalize(); - - System.out.println(doc.getDocumentElement().getNodeName()); - - String user = doc.getElementsByTagName("user").item(0).getTextContent(); - String pass = doc.getElementsByTagName("pass").item(0).getTextContent(); - UserClass type; - if(doc.getElementsByTagName("type").item(0).getTextContent().toLowerCase().equals("read_only")) { - type = UserClass.READ_ONLY; - } else if(doc.getElementsByTagName("type").item(0).getTextContent().toLowerCase().equals("read_write")) { - type = UserClass.READ_WRITE; - } else { - throw new IllegalArgumentException("Unused user class."); + + 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."); + } + } } - auth = new Auth(user, pass, type); - String host = doc.getElementsByTagName("host").item(0).getTextContent(); - int port = Integer.parseInt(doc.getElementsByTagName("port").item(0).getTextContent().trim()); - String db = doc.getElementsByTagName("db").item(0).getTextContent(); - database = new Database(host, port, db); + 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) { @@ -58,4 +107,11 @@ public class ConfigReader { 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(); + } } diff --git a/src/uk/org/ury/config/Database.java b/src/uk/org/ury/config/Database.java index c1074df..19fe9bc 100644 --- a/src/uk/org/ury/config/Database.java +++ b/src/uk/org/ury/config/Database.java @@ -1,15 +1,41 @@ package uk.org.ury.config; +/** + * A Database Server configuration + * + * @author Nathan Lasseter + */ public class Database { private String host; private int port; private String db; + /** + * Get the hostname of the database server + * + * @return String hostname + */ public String getHost() { return host; } + /** + * Get the port the database server is running on + * + * @return int port + */ public int getPort() { return port; } + /** + * Get the name of the database + * + * @return String database name + */ public String getDb() { return db; } + /** + * Create a database object + * @param host The hostname of the database server + * @param port The port that the database server listens on + * @param db The name of the database on the server + */ public Database(String host, int port, String db) { this.host = host; this.port = port; diff --git a/src/uk/org/ury/database/DatabaseDriver.java b/src/uk/org/ury/database/DatabaseDriver.java index bd70c8f..a7db32e 100644 --- a/src/uk/org/ury/database/DatabaseDriver.java +++ b/src/uk/org/ury/database/DatabaseDriver.java @@ -7,6 +7,7 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; +import uk.org.ury.config.ConfigReader; import uk.org.ury.database.exceptions.ConnectionFailureException; import uk.org.ury.database.exceptions.MissingCredentialsException; @@ -17,13 +18,14 @@ import uk.org.ury.database.exceptions.MissingCredentialsException; * of SQL queries. * * @author Matt Windsor + * @author Nathan Lasseter * */ public class DatabaseDriver { /* The JDBC path used to connect to the URY database. */ - private String DATABASE_PATH = "jdbc:postgresql://4574.co.uk/membership"; + private String DATABASE_PATH = "jdbc:postgresql://"; /* The database connection. */ private Connection conn; @@ -32,6 +34,7 @@ public class DatabaseDriver /** * Construct a new DatabaseDriver with the given user class. * + * @param config The config with login details. * @param userclass The user class to log in to the database with. * * @throws IllegalArgumentException if the user class is @@ -46,16 +49,11 @@ public class DatabaseDriver */ public - DatabaseDriver (UserClass userclass) - throws MissingCredentialsException, ConnectionFailureException + DatabaseDriver (ConfigReader config, UserClass type) throws MissingCredentialsException, ConnectionFailureException { - DatabaseLogin login = null; - - login = DatabaseLogin.getLoginFromFile (userclass.configName + ".txt"); - try { - connect (login); + connect (config, type); } catch (SQLException e) { @@ -68,27 +66,43 @@ public class DatabaseDriver /** * Connect to the URY database. * - * @param login The login tuple to use for the connection. + * @param config The config to use for the connection. + * @param type The access level of the connection * - * @throws SQLException if the database connection failed. + * @throws SQLException if the database connection failed. */ private void - connect (DatabaseLogin login) throws SQLException + connect (ConfigReader config, UserClass type) throws SQLException { - if (login == null) - throw new IllegalArgumentException ("Supplied null login."); + if (config == null) + throw new IllegalArgumentException ("Supplied null config."); - if (login.getUsername () == null) - throw new IllegalArgumentException ("Login has no associated username."); - - if (login.getPassword () == null) - throw new IllegalArgumentException ("Login has no associated password."); + if (config.getDatabase().getHost() == null) + throw new IllegalArgumentException ("config has no associated host."); + + if (config.getDatabase().getDb() == null) + throw new IllegalArgumentException ("config has no associated database."); + DATABASE_PATH = DATABASE_PATH + config.getDatabase().getHost() + "/" + config.getDatabase().getDb(); - conn = DriverManager.getConnection (DATABASE_PATH, - login.getUsername (), - login.getPassword ()); + if(type == UserClass.READ_ONLY) { + if(config.getRoAuth().getUser() == null) + throw new IllegalArgumentException ("config has no associated username."); + if(config.getRoAuth().getPass() == null) + throw new IllegalArgumentException ("config has no associated password."); + conn = DriverManager.getConnection (DATABASE_PATH, + config.getRoAuth().getUser(), + config.getRoAuth().getPass()); + } else if(type == UserClass.READ_WRITE) { + if(config.getRwAuth().getUser() == null) + throw new IllegalArgumentException ("config has no associated username."); + if(config.getRwAuth().getPass() == null) + throw new IllegalArgumentException ("config has no associated password."); + conn = DriverManager.getConnection (DATABASE_PATH, + config.getRwAuth().getUser(), + config.getRwAuth().getPass()); + } } diff --git a/src/uk/org/ury/database/DatabaseLogin.java b/src/uk/org/ury/database/DatabaseLogin.java deleted file mode 100644 index 2afa6ce..0000000 --- a/src/uk/org/ury/database/DatabaseLogin.java +++ /dev/null @@ -1,175 +0,0 @@ -/** - * - */ -package uk.org.ury.database; - -import java.io.BufferedReader; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.IOException; -import java.net.URL; - -import uk.org.ury.database.exceptions.MissingCredentialsException; - - -/** - * A login username/password pair. - * - * @author Matt Windsor - */ - -public class DatabaseLogin -{ - private String username; - private String password; - - - /** - * Create a new DatabaseLogin. - * - * This constructor is intentionally left private to prevent - * coders from hard-coding passwords. Please read database - * credentials from a source external to the program code. - * - * @param username The database username. - * @param password The database password. - * - * @see getLoginFromFile - * @see getLoginFromConfig - */ - - private - DatabaseLogin (String username, String password) - { - this.username = username; - this.password = password; - } - - - /** - * Retrieve login credentials from a plaintext file. - * - * The credentials should be listed in the following format: - * - * username <newline> - * password - * - * @param file The filename of the file - * - * @return a new DatabaseLogin containing the information - * retrieved from the file. - * - * @throws IllegalArgumentException if the filename is null. - * - * @throws MissingCredentialsException if the file does not exist - * or is of the wrong format. - */ - - public static DatabaseLogin - getLoginFromFile (String file) throws MissingCredentialsException - { - // Find the credentials file. - - if (file == null) - throw new IllegalArgumentException ("Supplied null credentials filename."); - - URL fileURL = DatabaseLogin.class.getResource ("credentials/" + file); - - if (fileURL == null) - throw new MissingCredentialsException ("Credentials file " - + file - + " not found."); - - - String username = null; - String password = null; - BufferedReader reader = null; - - - try - { - reader = new BufferedReader (new FileReader (fileURL.getFile ())); - } - catch (FileNotFoundException e) - { - throw new MissingCredentialsException ("Credentials file " - + file - + " not found."); - } - - try - { - username = reader.readLine (); - password = reader.readLine (); - } - catch (IOException e) - { - throw new MissingCredentialsException ("Credentials file " - + file - + "is invalid."); - } - finally - { - try - { - reader.close (); - } - catch (IOException e) - { - // TODO Auto-generated catch block - e.printStackTrace (); - } - } - - - DatabaseLogin login = new DatabaseLogin (username, password); - - return login; - } - - - /** - * Retrieve login credentials from the configuration file. - * - * @param configName The name of the tag in the configuration - * file containing the credentials. - * - * @return a new DatabaseLogin containing the information - * retrieved from the file. - * - * @throws IllegalArgumentException if the filename is - * null. - * - * @throws MissingCredentialsException if the tag or - * configuration file does not exist. - */ - - public static DatabaseLogin - getLoginFromConfig (String configName) - { - // TODO Auto-generated method stub - return null; - } - - - /** - * @return the username. - */ - - public String - getUsername () - { - return username; - } - - - /** - * @return the password. - */ - - public String - getPassword () - { - return password; - } -} diff --git a/src/uk/org/ury/database/credentials/.gitignore b/src/uk/org/ury/database/credentials/.gitignore deleted file mode 100644 index 2211df6..0000000 --- a/src/uk/org/ury/database/credentials/.gitignore +++ /dev/null @@ -1 +0,0 @@ -*.txt diff --git a/src/uk/org/ury/library/viewer/LibraryViewer.java b/src/uk/org/ury/library/viewer/LibraryViewer.java index 970235a..a631e89 100644 --- a/src/uk/org/ury/library/viewer/LibraryViewer.java +++ b/src/uk/org/ury/library/viewer/LibraryViewer.java @@ -31,6 +31,7 @@ public class LibraryViewer extends AbstractFrontendModule private List<LibraryItem> libraryList; private LibraryViewerPanel panel; private FrontendFrame frame; + private static ConfigReader config; /** @@ -43,7 +44,12 @@ public class LibraryViewer extends AbstractFrontendModule public static void main (String[] args) { - new ConfigReader(); + try { + config = new ConfigReader("res/conf.xml"); + } + catch(MissingCredentialsException e) { + System.out.println(e); + } LibraryViewer lv = new LibraryViewer (); lv.runFrontendInFrame (); } @@ -141,7 +147,7 @@ public class LibraryViewer extends AbstractFrontendModule try { - dd = new DatabaseDriver (UserClass.READ_ONLY); + dd = new DatabaseDriver(config, UserClass.READ_ONLY); } catch (MissingCredentialsException e) { |