HtmCache.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. private HtmCache()
  41. {
  42. if (Config.LAZY_CACHE)
  43. {
  44. _cache = new L2TIntObjectHashMap<String>();
  45. }
  46. else
  47. {
  48. _cache = new TIntObjectHashMap<String>();
  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(htmlFilter);
  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. final String relpath = Util.getRelativePath(Config.DATAPACK_ROOT, file);
  103. final int hashcode = relpath.hashCode();
  104. String content = null;
  105. if (htmlFilter.accept(file))
  106. {
  107. try (FileInputStream fis = new FileInputStream(file); BufferedInputStream bis = new BufferedInputStream(fis);)
  108. {
  109. final int bytes = bis.available();
  110. byte[] raw = new byte[bytes];
  111. bis.read(raw);
  112. content = new String(raw, "UTF-8");
  113. content = content.replaceAll("\r\n", "\n");
  114. String oldContent = _cache.get(hashcode);
  115. if (oldContent == null)
  116. {
  117. _bytesBuffLen += bytes;
  118. _loadedFiles++;
  119. }
  120. else
  121. {
  122. _bytesBuffLen = (_bytesBuffLen - oldContent.length()) + bytes;
  123. }
  124. _cache.put(hashcode, content);
  125. }
  126. catch (Exception e)
  127. {
  128. _log.log(Level.WARNING, "Problem with htm file " + e.getMessage(), e);
  129. }
  130. }
  131. return content;
  132. }
  133. public String getHtmForce(String prefix, String path)
  134. {
  135. String content = getHtm(prefix, path);
  136. if (content == null)
  137. {
  138. content = "<html><body>My text is missing:<br>" + path + "</body></html>";
  139. _log.warning("Cache[HTML]: Missing HTML page: " + path);
  140. }
  141. return content;
  142. }
  143. public String getHtm(String prefix, String path)
  144. {
  145. String newPath = null;
  146. String content;
  147. if ((prefix != null) && !prefix.isEmpty())
  148. {
  149. newPath = prefix + path;
  150. content = getHtm(newPath);
  151. if (content != null)
  152. {
  153. return content;
  154. }
  155. }
  156. content = getHtm(path);
  157. if ((content != null) && (newPath != null))
  158. {
  159. _cache.put(newPath.hashCode(), content);
  160. }
  161. return content;
  162. }
  163. private String getHtm(String path)
  164. {
  165. if ((path == null) || path.isEmpty())
  166. {
  167. return ""; // avoid possible NPE
  168. }
  169. final int hashCode = path.hashCode();
  170. String content = _cache.get(hashCode);
  171. if (Config.LAZY_CACHE && (content == null))
  172. {
  173. content = loadFile(new File(Config.DATAPACK_ROOT, path));
  174. }
  175. return content;
  176. }
  177. public boolean contains(String path)
  178. {
  179. return _cache.containsKey(path.hashCode());
  180. }
  181. /**
  182. * @param path The path to the HTM
  183. * @return {@code true} if the path targets a HTM or HTML file, {@code false} otherwise.
  184. */
  185. public boolean isLoadable(String path)
  186. {
  187. return htmlFilter.accept(new File(path));
  188. }
  189. @SuppressWarnings("synthetic-access")
  190. private static class SingletonHolder
  191. {
  192. protected static final HtmCache _instance = new HtmCache();
  193. }
  194. }