HtmCache.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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.communityserver.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.Level;
  21. import java.util.logging.Logger;
  22. import javax.xml.parsers.DocumentBuilderFactory;
  23. import org.w3c.dom.Document;
  24. import org.w3c.dom.NamedNodeMap;
  25. import org.w3c.dom.Node;
  26. import javolution.util.FastMap;
  27. import com.l2jserver.communityserver.Config;
  28. /**
  29. * @author Layane
  30. *
  31. */
  32. public class HtmCache
  33. {
  34. private static Logger _log = Logger.getLogger(HtmCache.class.getName());
  35. private static HtmCache _instance;
  36. private FastMap<Integer, String> _cache;
  37. private FastMap<Integer, String> _serverTopDir;
  38. private int _loadedFiles;
  39. private long _bytesBuffLen;
  40. public static HtmCache getInstance()
  41. {
  42. if (_instance == null)
  43. _instance = new HtmCache();
  44. return _instance;
  45. }
  46. public HtmCache()
  47. {
  48. _cache = new FastMap<Integer, String>();
  49. _serverTopDir = new FastMap<Integer, String>();
  50. loadTopDirs();
  51. reload();
  52. }
  53. public void loadTopDirs()
  54. {
  55. try
  56. {
  57. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  58. factory.setValidating(false);
  59. factory.setIgnoringComments(true);
  60. File file = new File(Config.DATAPACK_ROOT + "/data/top/servertopdir.xml");
  61. if (!file.exists())
  62. {
  63. _log.info("The servertopdir.xml file is missing.");
  64. return;
  65. }
  66. Document doc = factory.newDocumentBuilder().parse(file);
  67. for (Node n = doc.getFirstChild(); n != null; n = n.getNextSibling())
  68. {
  69. if ("servers_list".equalsIgnoreCase(n.getNodeName()))
  70. {
  71. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  72. {
  73. if ("server".equalsIgnoreCase(d.getNodeName()))
  74. {
  75. NamedNodeMap attrs = d.getAttributes();
  76. int sqlDPId = Integer.parseInt(attrs.getNamedItem("sqlDPId").getNodeValue());
  77. String dir = attrs.getNamedItem("dir").getNodeValue();
  78. _serverTopDir.put(sqlDPId, dir);
  79. }
  80. }
  81. }
  82. }
  83. }
  84. catch (Exception e)
  85. {
  86. _log.log(Level.SEVERE, "Error parsing servertopdir.xml.", e);
  87. return;
  88. }
  89. }
  90. public void reload()
  91. {
  92. reload(Config.DATAPACK_ROOT);
  93. }
  94. public void reload(File f)
  95. {
  96. /* if (!Config.LAZY_CACHE)
  97. {
  98. _log.info("Html cache start...");
  99. parseDir(f);
  100. _log.info("Cache[HTML]: " + String.format("%.3f", getMemoryUsage()) + " megabytes on " + getLoadedFiles() + " files loaded");
  101. }
  102. else
  103. {*/
  104. _cache.clear();
  105. _loadedFiles = 0;
  106. _bytesBuffLen = 0;
  107. _log.info("Cache[HTML]: Running lazy cache");
  108. //}
  109. }
  110. public void reloadPath(File f)
  111. {
  112. parseDir(f);
  113. _log.info("Cache[HTML]: Reloaded specified path.");
  114. }
  115. public double getMemoryUsage()
  116. {
  117. return ((float) _bytesBuffLen / 1048576);
  118. }
  119. public static String getRelativePath(File base, File file)
  120. {
  121. return file.toURI().getPath().substring(base.toURI().getPath().length());
  122. }
  123. public int getLoadedFiles()
  124. {
  125. return _loadedFiles;
  126. }
  127. class HtmFilter implements FileFilter
  128. {
  129. public boolean accept(File file)
  130. {
  131. if (!file.isDirectory())
  132. {
  133. return (file.getName().endsWith(".htm") || file.getName().endsWith(".html"));
  134. }
  135. return true;
  136. }
  137. }
  138. private void parseDir(File dir)
  139. {
  140. FileFilter filter = new HtmFilter();
  141. File[] files = dir.listFiles(filter);
  142. for (File file : files)
  143. {
  144. if (!file.isDirectory())
  145. loadFile(file);
  146. else
  147. parseDir(file);
  148. }
  149. }
  150. public String loadFile(File file)
  151. {
  152. HtmFilter filter = new HtmFilter();
  153. if (file.exists() && filter.accept(file) && !file.isDirectory())
  154. {
  155. String content;
  156. FileInputStream fis = null;
  157. try
  158. {
  159. fis = new FileInputStream(file);
  160. BufferedInputStream bis = new BufferedInputStream(fis);
  161. int bytes = bis.available();
  162. byte[] raw = new byte[bytes];
  163. bis.read(raw);
  164. content = new String(raw, "UTF-8");
  165. content = content.replaceAll("\r\n", "\n");
  166. String relpath = getRelativePath(Config.DATAPACK_ROOT, file);
  167. int hashcode = relpath.hashCode();
  168. String oldContent = _cache.get(hashcode);
  169. if (oldContent == null)
  170. {
  171. _bytesBuffLen += bytes;
  172. _loadedFiles++;
  173. }
  174. else
  175. {
  176. _bytesBuffLen = _bytesBuffLen - oldContent.length() + bytes;
  177. }
  178. _cache.put(hashcode, content);
  179. return content;
  180. }
  181. catch (Exception e)
  182. {
  183. _log.warning("problem with htm file " + e);
  184. }
  185. finally
  186. {
  187. try
  188. {
  189. fis.close();
  190. }
  191. catch (Exception e1)
  192. {
  193. }
  194. }
  195. }
  196. return null;
  197. }
  198. public String getHtmForce(String path)
  199. {
  200. String content = getHtm(path);
  201. if (content == null)
  202. {
  203. content = "<html><body>My text is missing:<br>" + path + "</body></html>";
  204. _log.warning("Cache[HTML]: Missing HTML page: " + path);
  205. }
  206. return content;
  207. }
  208. public String getHtm(String path)
  209. {
  210. String content = _cache.get(path.hashCode());
  211. if (content == null)
  212. content = loadFile(new File(Config.DATAPACK_ROOT, path));
  213. return content;
  214. }
  215. public String getHtm(final int sqlDPId, String file)
  216. {
  217. String path = "data/top/" + _serverTopDir.get(sqlDPId) + "/" + file;
  218. String content = _cache.get(path.hashCode());
  219. if (content == null)
  220. content = loadFile(new File(Config.DATAPACK_ROOT, path));
  221. return content;
  222. }
  223. public boolean contains(String path)
  224. {
  225. return _cache.containsKey(path.hashCode());
  226. }
  227. /**
  228. * Check if an HTM exists and can be loaded
  229. * @param
  230. * path The path to the HTM
  231. * */
  232. public boolean isLoadable(String path)
  233. {
  234. File file = new File(path);
  235. HtmFilter filter = new HtmFilter();
  236. if (file.exists() && filter.accept(file) && !file.isDirectory())
  237. return true;
  238. return false;
  239. }
  240. }