ManagedScript.java 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright (C) 2004-2014 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.scripting;
  20. import java.io.File;
  21. import javax.script.ScriptException;
  22. /**
  23. * Abstract class for classes that are meant to be implemented by scripts.<BR>
  24. * @author KenM
  25. */
  26. public abstract class ManagedScript
  27. {
  28. private final File _scriptFile;
  29. private long _lastLoadTime;
  30. private boolean _isActive;
  31. public ManagedScript()
  32. {
  33. _scriptFile = L2ScriptEngineManager.getInstance().getCurrentLoadingScript();
  34. setLastLoadTime(System.currentTimeMillis());
  35. }
  36. /**
  37. * Attempts to reload this script and to refresh the necessary bindings with it ScriptControler.<BR>
  38. * Subclasses of this class should override this method to properly refresh their bindings when necessary.
  39. * @return true if and only if the script was reloaded, false otherwise.
  40. */
  41. public boolean reload()
  42. {
  43. try
  44. {
  45. L2ScriptEngineManager.getInstance().executeScript(getScriptFile());
  46. return true;
  47. }
  48. catch (ScriptException e)
  49. {
  50. return false;
  51. }
  52. }
  53. public abstract boolean unload();
  54. public void setActive(boolean status)
  55. {
  56. _isActive = status;
  57. }
  58. public boolean isActive()
  59. {
  60. return _isActive;
  61. }
  62. /**
  63. * @return Returns the scriptFile.
  64. */
  65. public File getScriptFile()
  66. {
  67. return _scriptFile;
  68. }
  69. /**
  70. * @param lastLoadTime The lastLoadTime to set.
  71. */
  72. protected void setLastLoadTime(long lastLoadTime)
  73. {
  74. _lastLoadTime = lastLoadTime;
  75. }
  76. /**
  77. * @return Returns the lastLoadTime.
  78. */
  79. protected long getLastLoadTime()
  80. {
  81. return _lastLoadTime;
  82. }
  83. public abstract String getScriptName();
  84. public abstract ScriptManager<?> getScriptManager();
  85. }