CompiledScriptCache.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 javolution.util.FastMap;
  32. import com.l2jserver.Config;
  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. if (Config.DEBUG)
  61. {
  62. LOG.info("Compiling script: "+file);
  63. }
  64. Compilable eng = (Compilable) engine;
  65. BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
  66. // TODO lock file
  67. CompiledScript cs = eng.compile(reader);
  68. if (cs instanceof Serializable)
  69. {
  70. synchronized (_compiledScriptCache)
  71. {
  72. _compiledScriptCache.put(relativeName, new CompiledScriptHolder(cs, file));
  73. _modified = true;
  74. }
  75. }
  76. return cs;
  77. }
  78. public boolean isModified()
  79. {
  80. return _modified;
  81. }
  82. public void purge()
  83. {
  84. synchronized (_compiledScriptCache)
  85. {
  86. for (String path : _compiledScriptCache.keySet())
  87. {
  88. File file = new File(L2ScriptEngineManager.SCRIPT_FOLDER, path);
  89. if (!file.isFile())
  90. {
  91. _compiledScriptCache.remove(path);
  92. _modified = true;
  93. }
  94. }
  95. }
  96. }
  97. public void save() throws FileNotFoundException, IOException
  98. {
  99. synchronized (_compiledScriptCache)
  100. {
  101. ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File(L2ScriptEngineManager.SCRIPT_FOLDER, "CompiledScripts.cache")));
  102. oos.writeObject(this);
  103. _modified = false;
  104. }
  105. }
  106. }