DBInstallerConsole.java 4.9 KB

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