DBInstallerConsole.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright (C) 2004-2014 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.mysql.MySqlConnect;
  26. import com.l2jserver.util.CloseShieldedInputStream;
  27. /**
  28. * @author mrTJO
  29. */
  30. public class DBInstallerConsole implements DBOutputInterface
  31. {
  32. Connection _con;
  33. public DBInstallerConsole(String db, String dir, String cleanUp)
  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, cleanUp, true);
  67. }
  68. }
  69. else if (resp.equalsIgnoreCase("u"))
  70. {
  71. rt = new RunTasks(this, db, dir, cleanUp, false);
  72. }
  73. }
  74. if (rt != null)
  75. {
  76. rt.run();
  77. }
  78. else
  79. {
  80. System.exit(0);
  81. }
  82. }
  83. @Override
  84. public void appendToProgressArea(String text)
  85. {
  86. System.out.println(text);
  87. }
  88. @Override
  89. public Connection getConnection()
  90. {
  91. return _con;
  92. }
  93. @Override
  94. public void setProgressIndeterminate(boolean value)
  95. {
  96. }
  97. @Override
  98. public void setProgressMaximum(int maxValue)
  99. {
  100. }
  101. @Override
  102. public void setProgressValue(int value)
  103. {
  104. }
  105. @Override
  106. public void setFrameVisible(boolean value)
  107. {
  108. }
  109. @Override
  110. public int requestConfirm(String title, String message, int type)
  111. {
  112. System.out.print(message);
  113. String res = "";
  114. try (Scanner scn = new Scanner(new CloseShieldedInputStream(System.in)))
  115. {
  116. res = scn.next();
  117. }
  118. return res.equalsIgnoreCase("y") ? 0 : 1;
  119. }
  120. @Override
  121. public void showMessage(String title, String message, int type)
  122. {
  123. System.out.println(message);
  124. }
  125. }