ManagedScript.java 2.4 KB

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