GlobalVariablesManager.java 3.3 KB

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