HtmCache.java 5.5 KB

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