ManagedScript.java 2.4 KB

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