HtmCache.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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.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. public boolean accept(File file)
  84. {
  85. if (!file.isDirectory())
  86. {
  87. return (file.getName().endsWith(".htm") || file.getName().endsWith(".html"));
  88. }
  89. return true;
  90. }
  91. }
  92. private void parseDir(File dir)
  93. {
  94. FileFilter filter = new HtmFilter();
  95. File[] files = dir.listFiles(filter);
  96. for (File file : files)
  97. {
  98. if (!file.isDirectory())
  99. loadFile(file);
  100. else
  101. parseDir(file);
  102. }
  103. }
  104. public String loadFile(File file)
  105. {
  106. final String relpath = Util.getRelativePath(Config.DATAPACK_ROOT, file);
  107. final int hashcode = relpath.hashCode();
  108. final HtmFilter filter = new HtmFilter();
  109. if (file.exists() && filter.accept(file) && !file.isDirectory())
  110. {
  111. String content;
  112. FileInputStream fis = null;
  113. try
  114. {
  115. fis = new FileInputStream(file);
  116. BufferedInputStream bis = new BufferedInputStream(fis);
  117. int bytes = bis.available();
  118. byte[] raw = new byte[bytes];
  119. bis.read(raw);
  120. content = new String(raw, "UTF-8");
  121. content = content.replaceAll("\r\n", "\n");
  122. String oldContent = _cache.get(hashcode);
  123. if (oldContent == null)
  124. {
  125. _bytesBuffLen += bytes;
  126. _loadedFiles++;
  127. }
  128. else
  129. {
  130. _bytesBuffLen = _bytesBuffLen - oldContent.length() + bytes;
  131. }
  132. _cache.put(hashcode, content);
  133. return content;
  134. }
  135. catch (Exception e)
  136. {
  137. _log.log(Level.WARNING, "Problem with htm file " + e.getMessage(), e);
  138. }
  139. finally
  140. {
  141. try
  142. {
  143. fis.close();
  144. }
  145. catch (Exception e1)
  146. {
  147. }
  148. }
  149. }
  150. return null;
  151. }
  152. public String getHtmForce(String prefix, String path)
  153. {
  154. String content = getHtm(prefix, path);
  155. if (content == null)
  156. {
  157. content = "<html><body>My text is missing:<br>" + path + "</body></html>";
  158. _log.warning("Cache[HTML]: Missing HTML page: " + path);
  159. }
  160. return content;
  161. }
  162. public String getHtm(String prefix, String path)
  163. {
  164. String newPath = null;
  165. String content;
  166. if (prefix != null && !prefix.isEmpty())
  167. {
  168. newPath = prefix + path;
  169. content = getHtm(newPath);
  170. if (content != null)
  171. return content;
  172. }
  173. content = getHtm(path);
  174. if (content != null && newPath != null)
  175. _cache.put(newPath.hashCode(), content);
  176. return content;
  177. }
  178. private String getHtm(String path)
  179. {
  180. if (path == null || path.isEmpty())
  181. return ""; // avoid possible NPE
  182. final int hashCode = path.hashCode();
  183. String content = _cache.get(hashCode);
  184. if (Config.LAZY_CACHE && content == null)
  185. content = loadFile(new File(Config.DATAPACK_ROOT, path));
  186. return content;
  187. }
  188. public boolean contains(String path)
  189. {
  190. return _cache.containsKey(path.hashCode());
  191. }
  192. /**
  193. * Check if an HTM exists and can be loaded
  194. * @param
  195. * path The path to the HTM
  196. * */
  197. public boolean isLoadable(String path)
  198. {
  199. File file = new File(path);
  200. HtmFilter filter = new HtmFilter();
  201. if (file.exists() && filter.accept(file) && !file.isDirectory())
  202. return true;
  203. return false;
  204. }
  205. @SuppressWarnings("synthetic-access")
  206. private static class SingletonHolder
  207. {
  208. protected static final HtmCache _instance = new HtmCache();
  209. }
  210. }