HtmCache.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright © 2004-2019 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.cache;
  20. import static com.l2jserver.gameserver.config.Configuration.general;
  21. import static com.l2jserver.gameserver.config.Configuration.server;
  22. import java.io.BufferedInputStream;
  23. import java.io.File;
  24. import java.io.FileInputStream;
  25. import java.util.HashMap;
  26. import java.util.Map;
  27. import java.util.concurrent.ConcurrentHashMap;
  28. import org.slf4j.Logger;
  29. import org.slf4j.LoggerFactory;
  30. import com.google.common.base.Objects;
  31. import com.l2jserver.gameserver.util.file.filter.HTMLFilter;
  32. /**
  33. * HTML Cache.
  34. * @author Layane
  35. * @author Zoey76
  36. */
  37. public class HtmCache {
  38. private static final Logger LOG = LoggerFactory.getLogger(HtmCache.class);
  39. private static final HTMLFilter HTML_FILTER = new HTMLFilter();
  40. private static final Map<String, String> HTML_CACHE = general().lazyCache() ? new ConcurrentHashMap<>() : new HashMap<>();
  41. private int _loadedFiles;
  42. private long _bytesBuffLen;
  43. protected HtmCache() {
  44. reload();
  45. }
  46. public void reload() {
  47. reload(server().getDatapackRoot());
  48. }
  49. public void reload(File f) {
  50. if (!general().lazyCache()) {
  51. LOG.info("Html cache start...");
  52. parseDir(f);
  53. LOG.info(String.format("%.3f", getMemoryUsage()) + " megabytes on " + getLoadedFiles() + " files loaded");
  54. } else {
  55. HTML_CACHE.clear();
  56. _loadedFiles = 0;
  57. _bytesBuffLen = 0;
  58. LOG.info("Running lazy cache.");
  59. }
  60. }
  61. public void reloadPath(File f) {
  62. parseDir(f);
  63. LOG.info("Reloaded specified path.");
  64. }
  65. public double getMemoryUsage() {
  66. return ((float) _bytesBuffLen / 1048576);
  67. }
  68. public int getLoadedFiles() {
  69. return _loadedFiles;
  70. }
  71. private void parseDir(File dir) {
  72. final File[] files = dir.listFiles();
  73. if (files != null) {
  74. for (File file : files) {
  75. if (!file.isDirectory()) {
  76. loadFile(file);
  77. } else {
  78. parseDir(file);
  79. }
  80. }
  81. }
  82. }
  83. public String loadFile(File file) {
  84. if (!HTML_FILTER.accept(file)) {
  85. return null;
  86. }
  87. String content = null;
  88. try (var fis = new FileInputStream(file);
  89. var bis = new BufferedInputStream(fis)) {
  90. final int bytes = bis.available();
  91. byte[] raw = new byte[bytes];
  92. bis.read(raw);
  93. content = new String(raw, "UTF-8");
  94. content = content.replaceAll("(?s)<!--.*?-->", ""); // Remove html comments
  95. String oldContent = HTML_CACHE.put(file.getCanonicalPath(), content);
  96. if (oldContent == null) {
  97. _bytesBuffLen += bytes;
  98. _loadedFiles++;
  99. } else {
  100. _bytesBuffLen = (_bytesBuffLen - oldContent.length()) + bytes;
  101. }
  102. } catch (Exception e) {
  103. LOG.warn("Problem with htm file {}!", file, e);
  104. }
  105. return content;
  106. }
  107. public String getHtm(String prefix, String path) {
  108. final var newPath = Objects.firstNonNull(prefix, "") + path;
  109. var content = HTML_CACHE.get(newPath);
  110. if (general().lazyCache() && (content == null)) {
  111. content = loadFile(new File(server().getDatapackRoot(), newPath));
  112. if (content == null) {
  113. content = loadFile(new File(server().getScriptRoot(), newPath));
  114. }
  115. }
  116. return content;
  117. }
  118. public boolean contains(String path) {
  119. return HTML_CACHE.containsKey(path);
  120. }
  121. /**
  122. * @param path The path to the HTM
  123. * @return {@code true} if the path targets a HTM or HTML file, {@code false} otherwise.
  124. */
  125. public boolean isLoadable(String path) {
  126. return HTML_FILTER.accept(new File(server().getDatapackRoot(), path));
  127. }
  128. public static HtmCache getInstance() {
  129. return SingletonHolder.INSTANCE;
  130. }
  131. private static class SingletonHolder {
  132. protected static final HtmCache INSTANCE = new HtmCache();
  133. }
  134. }