2
0

HtmCache.java 4.8 KB

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