GlobalVariablesManager.java 3.3 KB

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