DBInstallerConsole.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.tools.dbinstaller.console;
  16. import java.sql.Connection;
  17. import java.util.Scanner;
  18. import java.util.prefs.Preferences;
  19. import com.l2jserver.tools.dbinstaller.DBOutputInterface;
  20. import com.l2jserver.tools.dbinstaller.RunTasks;
  21. import com.l2jserver.tools.dbinstaller.util.mysql.MySqlConnect;
  22. import com.l2jserver.util.CloseShieldedInputStream;
  23. /**
  24. * @author mrTJO
  25. */
  26. public class DBInstallerConsole implements DBOutputInterface
  27. {
  28. Connection _con;
  29. public DBInstallerConsole(String db, String dir, String cleanUp)
  30. {
  31. System.out.println("Welcome to L2J DataBase installer");
  32. Preferences prop = Preferences.userRoot();
  33. RunTasks rt = null;
  34. try (Scanner scn = new Scanner(new CloseShieldedInputStream(System.in)))
  35. {
  36. while (_con == null)
  37. {
  38. System.out.printf("%s (%s): ", "Host", prop.get("dbHost_" + db, "localhost"));
  39. String dbHost = scn.nextLine();
  40. System.out.printf("%s (%s): ", "Port", prop.get("dbPort_" + db, "3306"));
  41. String dbPort = scn.nextLine();
  42. System.out.printf("%s (%s): ", "Username", prop.get("dbUser_" + db, "root"));
  43. String dbUser = scn.nextLine();
  44. System.out.printf("%s (%s): ", "Password", "");
  45. String dbPass = scn.nextLine();
  46. System.out.printf("%s (%s): ", "Database", prop.get("dbDbse_" + db, db));
  47. String dbDbse = scn.nextLine();
  48. dbHost = dbHost.isEmpty() ? prop.get("dbHost_" + db, "localhost") : dbHost;
  49. dbPort = dbPort.isEmpty() ? prop.get("dbPort_" + db, "3306") : dbPort;
  50. dbUser = dbUser.isEmpty() ? prop.get("dbUser_" + db, "root") : dbUser;
  51. dbDbse = dbDbse.isEmpty() ? prop.get("dbDbse_" + db, db) : dbDbse;
  52. MySqlConnect connector = new MySqlConnect(dbHost, dbPort, dbUser, dbPass, dbDbse, true);
  53. _con = connector.getConnection();
  54. }
  55. System.out.print("(C)lean install, (U)pdate or (E)xit? ");
  56. String resp = scn.next();
  57. if (resp.equalsIgnoreCase("c"))
  58. {
  59. System.out.print("Do you really want to destroy your db (Y/N)?");
  60. if (scn.next().equalsIgnoreCase("y"))
  61. {
  62. rt = new RunTasks(this, db, dir, cleanUp, true);
  63. }
  64. }
  65. else if (resp.equalsIgnoreCase("u"))
  66. {
  67. rt = new RunTasks(this, db, dir, cleanUp, false);
  68. }
  69. }
  70. if (rt != null)
  71. {
  72. rt.run();
  73. }
  74. else
  75. {
  76. System.exit(0);
  77. }
  78. }
  79. @Override
  80. public void appendToProgressArea(String text)
  81. {
  82. System.out.println(text);
  83. }
  84. @Override
  85. public Connection getConnection()
  86. {
  87. return _con;
  88. }
  89. @Override
  90. public void setProgressIndeterminate(boolean value)
  91. {
  92. }
  93. @Override
  94. public void setProgressMaximum(int maxValue)
  95. {
  96. }
  97. @Override
  98. public void setProgressValue(int value)
  99. {
  100. }
  101. @Override
  102. public void setFrameVisible(boolean value)
  103. {
  104. }
  105. @Override
  106. public int requestConfirm(String title, String message, int type)
  107. {
  108. System.out.print(message);
  109. String res = "";
  110. try (Scanner scn = new Scanner(new CloseShieldedInputStream(System.in)))
  111. {
  112. res = scn.next();
  113. }
  114. return res.equalsIgnoreCase("y") ? 0 : 1;
  115. }
  116. @Override
  117. public void showMessage(String title, String message, int type)
  118. {
  119. System.out.println(message);
  120. }
  121. }