2
0

HtmCache.java 5.3 KB

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