GlobalVariablesManager.java 3.1 KB

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