HtmCache.java 4.8 KB

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