Guest User

Untitled

a guest
Dec 16th, 2016
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. package eg.edu.alexu.csd.oop.jdbc.jdbc;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.sql.Connection;
  6. import java.sql.Driver;
  7. import java.sql.DriverPropertyInfo;
  8. import java.sql.SQLException;
  9. import java.sql.SQLFeatureNotSupportedException;
  10. import java.util.ArrayList;
  11. import java.util.Properties;
  12. import java.util.Scanner;
  13.  
  14. import org.apache.logging.log4j.LogManager;
  15. import org.apache.logging.log4j.Logger;
  16.  
  17. public class JDBCDriver implements Driver {
  18.  
  19. private ArrayList<String> users, passwords;
  20.  
  21. private final Logger log = LogManager.getLogger(JDBCDriver.class);
  22.  
  23. public JDBCDriver() {
  24. this.users = new ArrayList<String>();
  25. this.passwords = new ArrayList<String>();
  26. }
  27.  
  28. public JDBCDriver(String userPath, String passwordPath) {
  29. this.users = new ArrayList<String>();
  30. this.passwords = new ArrayList<String>();
  31. setUserInfo(userPath, passwordPath);
  32. }
  33.  
  34. private void setUserInfo(String userPath, String passwordPath) {
  35. File userNames = new File(userPath);
  36. File password = new File(passwordPath);
  37. try {
  38. Scanner s1 = new Scanner(userNames);
  39. Scanner s2 = new Scanner(password);
  40. while (s1.hasNextLine()) {
  41. users.add(s1.nextLine());
  42. passwords.add(s2.nextLine());
  43. }
  44. s1.close();
  45. s2.close();
  46. } catch (FileNotFoundException e) {
  47. }
  48. }
  49.  
  50. @Override
  51. public boolean acceptsURL(final String url) throws SQLException {
  52. String xml, alternative;
  53. xml = "jdbc:xmldb://localhost";
  54. alternative = "jdbc:altdb://localhost";
  55. boolean accepted = (url.equals(xml) || url.equals(alternative));
  56. if (accepted) {
  57. log.info("URL " + url + " was accepted.");
  58. } else {
  59. log.warn("URL " + url + " was rejected.");
  60. }
  61. return accepted;
  62. }
  63.  
  64. @Override
  65. public Connection connect(final String url, final Properties info)
  66. throws SQLException {
  67. Connection connection = null;
  68. if (!acceptsURL(url)) {
  69. return connection;
  70. }
  71. File path = (File) info.get("path");
  72. String[] arr = url.split(":");
  73. try {
  74. connection = new JDBCConnection(path.getAbsolutePath(), arr[1]);
  75. } catch (Exception e) {
  76. log.error(e.getMessage());
  77. throw new SQLException(e.getMessage());
  78. }
  79. log.info("Connection is created to " + arr[1] + ".");
  80. return connection;
  81. }
  82.  
  83. @Override
  84. public int getMajorVersion() {
  85. throw new UnsupportedOperationException();
  86. }
  87.  
  88. @Override
  89. public int getMinorVersion() {
  90. throw new UnsupportedOperationException();
  91. }
  92.  
  93. @Override
  94. public java.util.logging.Logger getParentLogger()
  95. throws SQLFeatureNotSupportedException {
  96. throw new UnsupportedOperationException();
  97. }
  98.  
  99. @Override
  100. public DriverPropertyInfo[] getPropertyInfo(final String url,
  101. final Properties info)
  102. throws SQLException {
  103. int size = users.size();
  104. DriverPropertyInfo[] ret = new DriverPropertyInfo[size];
  105. for (int i = 0; i < size; i++) {
  106. ret[i] = new DriverPropertyInfo(users.get(i), passwords.get(i));
  107. }
  108. return ret;
  109. }
  110.  
  111. @Override
  112. public boolean jdbcCompliant() {
  113. throw new UnsupportedOperationException();
  114. }
  115.  
  116. }
Add Comment
Please, Sign In to add comment