HtmCache.java 5.0 KB

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