HtmCache.java 4.7 KB

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