GlobalVariablesManager.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (C) 2004-2015 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.SQLException;
  24. import java.sql.Statement;
  25. import java.util.Map.Entry;
  26. import java.util.logging.Level;
  27. import java.util.logging.Logger;
  28. import com.l2jserver.commons.database.pool.impl.ConnectionFactory;
  29. import com.l2jserver.gameserver.model.variables.AbstractVariables;
  30. /**
  31. * Global Variables Manager.
  32. * @author xban1x
  33. */
  34. public final class GlobalVariablesManager extends AbstractVariables
  35. {
  36. private static final Logger _log = Logger.getLogger(GlobalVariablesManager.class.getName());
  37. // SQL Queries.
  38. private static final String SELECT_QUERY = "SELECT * FROM global_variables";
  39. private static final String DELETE_QUERY = "DELETE FROM global_variables";
  40. private static final String INSERT_QUERY = "INSERT INTO global_variables (var, value) VALUES (?, ?)";
  41. protected GlobalVariablesManager()
  42. {
  43. restoreMe();
  44. }
  45. @Override
  46. public boolean restoreMe()
  47. {
  48. // Restore previous variables.
  49. try (Connection con = ConnectionFactory.getInstance().getConnection();
  50. Statement st = con.createStatement();
  51. ResultSet rset = st.executeQuery(SELECT_QUERY))
  52. {
  53. while (rset.next())
  54. {
  55. set(rset.getString("var"), rset.getString("value"));
  56. }
  57. }
  58. catch (SQLException e)
  59. {
  60. _log.log(Level.WARNING, getClass().getSimpleName() + ": Couldn't restore global variables");
  61. return false;
  62. }
  63. finally
  64. {
  65. compareAndSetChanges(true, false);
  66. }
  67. _log.log(Level.INFO, getClass().getSimpleName() + ": Loaded " + getSet().size() + " variables.");
  68. return true;
  69. }
  70. @Override
  71. public boolean storeMe()
  72. {
  73. // No changes, nothing to store.
  74. if (!hasChanges())
  75. {
  76. return false;
  77. }
  78. try (Connection con = ConnectionFactory.getInstance().getConnection();
  79. Statement del = con.createStatement();
  80. PreparedStatement st = con.prepareStatement(INSERT_QUERY))
  81. {
  82. // Clear previous entries.
  83. del.execute(DELETE_QUERY);
  84. // Insert all variables.
  85. for (Entry<String, Object> entry : getSet().entrySet())
  86. {
  87. st.setString(1, entry.getKey());
  88. st.setString(2, String.valueOf(entry.getValue()));
  89. st.addBatch();
  90. }
  91. st.executeBatch();
  92. }
  93. catch (SQLException e)
  94. {
  95. _log.log(Level.WARNING, getClass().getSimpleName() + ": Couldn't save global variables to database.", e);
  96. return false;
  97. }
  98. finally
  99. {
  100. compareAndSetChanges(true, false);
  101. }
  102. _log.log(Level.INFO, getClass().getSimpleName() + ": Stored " + getSet().size() + " variables.");
  103. return true;
  104. }
  105. /**
  106. * Gets the single instance of {@code GlobalVariablesManager}.
  107. * @return single instance of {@code GlobalVariablesManager}
  108. */
  109. public static final GlobalVariablesManager getInstance()
  110. {
  111. return SingletonHolder._instance;
  112. }
  113. private static class SingletonHolder
  114. {
  115. protected static final GlobalVariablesManager _instance = new GlobalVariablesManager();
  116. }
  117. }