GlobalVariablesManager.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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.gameserver.instancemanager;
  16. import java.sql.Connection;
  17. import java.sql.PreparedStatement;
  18. import java.sql.ResultSet;
  19. import java.sql.Statement;
  20. import java.util.Map;
  21. import java.util.logging.Logger;
  22. import com.l2jserver.L2DatabaseFactory;
  23. import com.l2jserver.util.L2FastMap;
  24. /**
  25. * @author Gigiikun
  26. */
  27. public class GlobalVariablesManager
  28. {
  29. private static final Logger _log = Logger.getLogger(GlobalVariablesManager.class.getName());
  30. private static final String LOAD_VAR = "SELECT var,value FROM global_variables";
  31. private static final String SAVE_VAR = "INSERT INTO global_variables (var,value) VALUES (?,?) ON DUPLICATE KEY UPDATE value=?";
  32. private final Map<String, String> _variablesMap = new L2FastMap<>(true);
  33. protected GlobalVariablesManager()
  34. {
  35. loadVars();
  36. }
  37. private final void loadVars()
  38. {
  39. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  40. Statement statement = con.createStatement();
  41. ResultSet rset = statement.executeQuery(LOAD_VAR))
  42. {
  43. String var, value;
  44. while (rset.next())
  45. {
  46. var = rset.getString(1);
  47. value = rset.getString(2);
  48. _variablesMap.put(var, value);
  49. }
  50. }
  51. catch (Exception e)
  52. {
  53. _log.warning(getClass().getSimpleName() + ": problem while loading variables: " + e);
  54. }
  55. }
  56. public final void saveVars()
  57. {
  58. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  59. PreparedStatement statement = con.prepareStatement(SAVE_VAR))
  60. {
  61. for (String var : _variablesMap.keySet())
  62. {
  63. statement.setString(1, var);
  64. statement.setString(2, _variablesMap.get(var));
  65. statement.setString(3, _variablesMap.get(var));
  66. statement.execute();
  67. statement.clearParameters();
  68. }
  69. }
  70. catch (Exception e)
  71. {
  72. _log.warning(getClass().getSimpleName() + ": problem while saving variables: " + e);
  73. }
  74. }
  75. public void storeVariable(String var, String value)
  76. {
  77. _variablesMap.put(var, value);
  78. }
  79. public boolean isVariableStored(String var)
  80. {
  81. return _variablesMap.containsKey(var);
  82. }
  83. public String getStoredVariable(String var)
  84. {
  85. return _variablesMap.get(var);
  86. }
  87. public static final GlobalVariablesManager getInstance()
  88. {
  89. return SingletonHolder._instance;
  90. }
  91. private static class SingletonHolder
  92. {
  93. protected static final GlobalVariablesManager _instance = new GlobalVariablesManager();
  94. }
  95. }