2
0

CompiledScriptCache.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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.scripting;
  16. import java.io.BufferedReader;
  17. import java.io.File;
  18. import java.io.FileInputStream;
  19. import java.io.FileNotFoundException;
  20. import java.io.FileOutputStream;
  21. import java.io.IOException;
  22. import java.io.InputStreamReader;
  23. import java.io.ObjectOutputStream;
  24. import java.io.Serializable;
  25. import java.util.Map;
  26. import java.util.logging.Logger;
  27. import javax.script.Compilable;
  28. import javax.script.CompiledScript;
  29. import javax.script.ScriptEngine;
  30. import javax.script.ScriptException;
  31. import com.l2jserver.Config;
  32. import javolution.util.FastMap;
  33. /**
  34. * Cache of Compiled Scripts
  35. *
  36. * @author KenM
  37. */
  38. public class CompiledScriptCache implements Serializable
  39. {
  40. /**
  41. * Version 1
  42. */
  43. private static final long serialVersionUID = 1L;
  44. private static final Logger LOG = Logger.getLogger(CompiledScriptCache.class.getName());
  45. private Map<String, CompiledScriptHolder> _compiledScriptCache = new FastMap<String, CompiledScriptHolder>();
  46. private transient boolean _modified = false;
  47. public CompiledScript loadCompiledScript(ScriptEngine engine, File file) throws FileNotFoundException, ScriptException
  48. {
  49. int len = L2ScriptEngineManager.SCRIPT_FOLDER.getPath().length() + 1;
  50. String relativeName = file.getPath().substring(len);
  51. CompiledScriptHolder csh = _compiledScriptCache.get(relativeName);
  52. if (csh != null && csh.matches(file))
  53. {
  54. if (Config.DEBUG)
  55. {
  56. LOG.fine("Reusing cached compiled script: "+file);
  57. }
  58. return csh.getCompiledScript();
  59. }
  60. else
  61. {
  62. if (Config.DEBUG)
  63. {
  64. LOG.info("Compiling script: "+file);
  65. }
  66. Compilable eng = (Compilable) engine;
  67. BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
  68. // TODO lock file
  69. CompiledScript cs = eng.compile(reader);
  70. if (cs instanceof Serializable)
  71. {
  72. synchronized (_compiledScriptCache)
  73. {
  74. _compiledScriptCache.put(relativeName, new CompiledScriptHolder(cs, file));
  75. _modified = true;
  76. }
  77. }
  78. return cs;
  79. }
  80. }
  81. public boolean isModified()
  82. {
  83. return _modified;
  84. }
  85. public void purge()
  86. {
  87. synchronized (_compiledScriptCache)
  88. {
  89. for (String path : _compiledScriptCache.keySet())
  90. {
  91. File file = new File(L2ScriptEngineManager.SCRIPT_FOLDER, path);
  92. if (!file.isFile())
  93. {
  94. _compiledScriptCache.remove(path);
  95. _modified = true;
  96. }
  97. }
  98. }
  99. }
  100. public void save() throws FileNotFoundException, IOException
  101. {
  102. synchronized (_compiledScriptCache)
  103. {
  104. ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File(L2ScriptEngineManager.SCRIPT_FOLDER, "CompiledScripts.cache")));
  105. oos.writeObject(this);
  106. _modified = false;
  107. }
  108. }
  109. }