HtmCache.java 5.0 KB

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