HtmCache.java 4.8 KB

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