aboutsummaryrefslogtreecommitdiff
path: root/src/uk/org/ury/database
diff options
context:
space:
mode:
authorMatt Windsor <mbw500@student.cs.york.ac.uk>2011-02-20 23:49:42 +0000
committerMatt Windsor <mbw500@student.cs.york.ac.uk>2011-02-20 23:49:42 +0000
commitcd972012ab8c40b66bd65f78bbf9e3422413b1aa (patch)
tree726aa9cefa80fc4a2f3922ed6e05cf0074421a6d /src/uk/org/ury/database
parente2fc92f5c42dde942e8b71d38b9745b6f3c98053 (diff)
parentf89ef9ed36e8185f53c7d5f22f91935e2c4ccaa0 (diff)
Merge. DatabaseLogin over. Auth = Very Yes.
Diffstat (limited to 'src/uk/org/ury/database')
-rw-r--r--src/uk/org/ury/database/DatabaseDriver.java56
-rw-r--r--src/uk/org/ury/database/DatabaseLogin.java175
-rw-r--r--src/uk/org/ury/database/credentials/.gitignore1
3 files changed, 35 insertions, 197 deletions
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