HtmCache.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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.cache;
  20. import java.io.BufferedInputStream;
  21. import java.io.File;
  22. import java.io.FileInputStream;
  23. import java.util.HashMap;
  24. import java.util.Map;
  25. import java.util.concurrent.ConcurrentHashMap;
  26. import org.slf4j.Logger;
  27. import org.slf4j.LoggerFactory;
  28. import com.l2jserver.Config;
  29. import com.l2jserver.gameserver.util.Util;
  30. import com.l2jserver.util.file.filter.HTMLFilter;
  31. /**
  32. * @author Layane
  33. */
  34. public class HtmCache
  35. {
  36. private static final Logger _log = LoggerFactory.getLogger(HtmCache.class);
  37. private static final HTMLFilter HTML_FILTER = new HTMLFilter();
  38. private static final Map<String, String> _cache = Config.LAZY_CACHE ? new ConcurrentHashMap<>() : new HashMap<>();
  39. private int _loadedFiles;
  40. private long _bytesBuffLen;
  41. protected HtmCache()
  42. {
  43. reload();
  44. }
  45. public void reload()
  46. {
  47. reload(Config.DATAPACK_ROOT);
  48. }
  49. public void reload(File f)
  50. {
  51. if (!Config.LAZY_CACHE)
  52. {
  53. _log.info("Html cache start...");
  54. parseDir(f);
  55. _log.info("Cache[HTML]: " + String.format("%.3f", getMemoryUsage()) + " megabytes on " + getLoadedFiles() + " files loaded");
  56. }
  57. else
  58. {
  59. _cache.clear();
  60. _loadedFiles = 0;
  61. _bytesBuffLen = 0;
  62. _log.info("Cache[HTML]: Running lazy cache");
  63. }
  64. }
  65. public void reloadPath(File f)
  66. {
  67. parseDir(f);
  68. _log.info("Cache[HTML]: Reloaded specified path.");
  69. }
  70. public double getMemoryUsage()
  71. {
  72. return ((float) _bytesBuffLen / 1048576);
  73. }
  74. public int getLoadedFiles()
  75. {
  76. return _loadedFiles;
  77. }
  78. private void parseDir(File dir)
  79. {
  80. final File[] files = dir.listFiles();
  81. if (files != null)
  82. {
  83. for (File file : files)
  84. {
  85. if (!file.isDirectory())
  86. {
  87. loadFile(file);
  88. }
  89. else
  90. {
  91. parseDir(file);
  92. }
  93. }
  94. }
  95. }
  96. public String loadFile(File file)
  97. {
  98. if (!HTML_FILTER.accept(file))
  99. {
  100. return null;
  101. }
  102. final String relpath = Util.getRelativePath(Config.DATAPACK_ROOT, file);
  103. String content = null;
  104. try (FileInputStream fis = new FileInputStream(file);
  105. BufferedInputStream bis = new BufferedInputStream(fis))
  106. {
  107. final int bytes = bis.available();
  108. byte[] raw = new byte[bytes];
  109. bis.read(raw);
  110. content = new String(raw, "UTF-8");
  111. content = content.replaceAll("(?s)<!--.*?-->", ""); // Remove html comments
  112. String oldContent = _cache.put(relpath, content);
  113. if (oldContent == null)
  114. {
  115. _bytesBuffLen += bytes;
  116. _loadedFiles++;
  117. }
  118. else
  119. {
  120. _bytesBuffLen = (_bytesBuffLen - oldContent.length()) + bytes;
  121. }
  122. }
  123. catch (Exception e)
  124. {
  125. _log.warn("Problem with htm file {}!", file, e);
  126. }
  127. return content;
  128. }
  129. public String getHtmForce(String prefix, String path)
  130. {
  131. String content = getHtm(prefix, path);
  132. if (content == null)
  133. {
  134. content = "<html><body>My text is missing:<br>" + path + "</body></html>";
  135. _log.warn("Cache[HTML]: Missing HTML page: " + path);
  136. }
  137. return content;
  138. }
  139. public String getHtm(String prefix, String path)
  140. {
  141. String newPath = null;
  142. String content;
  143. if ((prefix != null) && !prefix.isEmpty())
  144. {
  145. newPath = prefix + path;
  146. content = getHtm(newPath);
  147. if (content != null)
  148. {
  149. return content;
  150. }
  151. }
  152. content = getHtm(path);
  153. if ((content != null) && (newPath != null))
  154. {
  155. _cache.put(newPath, content);
  156. }
  157. return content;
  158. }
  159. private String getHtm(String path)
  160. {
  161. if ((path == null) || path.isEmpty())
  162. {
  163. return ""; // avoid possible NPE
  164. }
  165. String content = _cache.get(path);
  166. if (Config.LAZY_CACHE && (content == null))
  167. {
  168. content = loadFile(new File(Config.DATAPACK_ROOT, path));
  169. }
  170. return content;
  171. }
  172. public boolean contains(String path)
  173. {
  174. return _cache.containsKey(path);
  175. }
  176. /**
  177. * @param path The path to the HTM
  178. * @return {@code true} if the path targets a HTM or HTML file, {@code false} otherwise.
  179. */
  180. public boolean isLoadable(String path)
  181. {
  182. return HTML_FILTER.accept(new File(Config.DATAPACK_ROOT, path));
  183. }
  184. public static HtmCache getInstance()
  185. {
  186. return SingletonHolder._instance;
  187. }
  188. private static class SingletonHolder
  189. {
  190. protected static final HtmCache _instance = new HtmCache();
  191. }
  192. }