HtmCache.java 5.6 KB

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