ManagedScript.java 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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.File;
  17. import javax.script.ScriptException;
  18. /**
  19. * Abstract class for classes that are meant to be implemented by scripts.<BR>
  20. * @author KenM
  21. */
  22. public abstract class ManagedScript
  23. {
  24. private final File _scriptFile;
  25. private long _lastLoadTime;
  26. private boolean _isActive;
  27. public ManagedScript()
  28. {
  29. _scriptFile = L2ScriptEngineManager.getInstance().getCurrentLoadingScript();
  30. setLastLoadTime(System.currentTimeMillis());
  31. }
  32. /**
  33. * Attempts to reload this script and to refresh the necessary bindings with it ScriptControler.<BR>
  34. * Subclasses of this class should override this method to properly refresh their bindings when necessary.
  35. * @return true if and only if the script was reloaded, false otherwise.
  36. */
  37. public boolean reload()
  38. {
  39. try
  40. {
  41. L2ScriptEngineManager.getInstance().executeScript(getScriptFile());
  42. return true;
  43. }
  44. catch (ScriptException e)
  45. {
  46. return false;
  47. }
  48. }
  49. public abstract boolean unload();
  50. public void setActive(boolean status)
  51. {
  52. _isActive = status;
  53. }
  54. public boolean isActive()
  55. {
  56. return _isActive;
  57. }
  58. /**
  59. * @return Returns the scriptFile.
  60. */
  61. public File getScriptFile()
  62. {
  63. return _scriptFile;
  64. }
  65. /**
  66. * @param lastLoadTime The lastLoadTime to set.
  67. */
  68. protected void setLastLoadTime(long lastLoadTime)
  69. {
  70. _lastLoadTime = lastLoadTime;
  71. }
  72. /**
  73. * @return Returns the lastLoadTime.
  74. */
  75. protected long getLastLoadTime()
  76. {
  77. return _lastLoadTime;
  78. }
  79. public abstract String getScriptName();
  80. public abstract ScriptManager<?> getScriptManager();
  81. }