You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ScriptEngineWrapper.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.addons.scriptplugin;
  18. import com.google.common.base.Preconditions;
  19. import java.io.File;
  20. import java.io.FileReader;
  21. import java.io.IOException;
  22. import javax.script.Invocable;
  23. import javax.script.ScriptEngine;
  24. import javax.script.ScriptEngineManager;
  25. import javax.script.ScriptException;
  26. import org.slf4j.Logger;
  27. import org.slf4j.LoggerFactory;
  28. import static com.dmdirc.util.LogUtils.USER_ERROR;
  29. /**
  30. * Class to create script engines!
  31. */
  32. public class ScriptEngineWrapper {
  33. private static final Logger LOG = LoggerFactory.getLogger(ScriptEngineWrapper.class);
  34. /** The Script Engine this wrapper wraps */
  35. private ScriptEngine engine;
  36. /** The File this script is from */
  37. private final File file;
  38. /** Script-Local JS Helper */
  39. private final JavaScriptHelper localHelper = new JavaScriptHelper();
  40. /** Manager to get script engines. */
  41. private final ScriptEngineManager scriptEngineManager;
  42. /**
  43. * Create a new ScriptEngineWrapper
  44. *
  45. * @param scriptEngineManager Manager to get script engines
  46. * @param filename Filename of script
  47. *
  48. * @throws ScriptException If there was an error during creation
  49. */
  50. protected ScriptEngineWrapper(final ScriptEngineManager scriptEngineManager,
  51. final String filename) throws ScriptException {
  52. Preconditions.checkNotNull(filename, "File cannot be null");
  53. this.scriptEngineManager = scriptEngineManager;
  54. file = new File(filename);
  55. engine = createEngine();
  56. callFunction("onLoad");
  57. }
  58. /**
  59. * Get a reference to the ScriptEngine.
  60. *
  61. * @return a reference to the ScriptEngine
  62. */
  63. protected ScriptEngine getScriptEngine() {
  64. return engine;
  65. }
  66. /**
  67. * Get a reference to the JavaScriptHelper
  68. *
  69. * @return a reference to the JavaScriptHelper
  70. */
  71. protected JavaScriptHelper getJavaScriptHelper() {
  72. return localHelper;
  73. }
  74. /**
  75. * Get the file for this script
  76. *
  77. * @return The file for this script
  78. */
  79. protected File getFile() {
  80. return file;
  81. }
  82. /**
  83. * Create a new engine for this script
  84. *
  85. * @return Created script engine
  86. *
  87. * @throws ScriptException If there was an error during creation
  88. */
  89. protected ScriptEngine createEngine() throws ScriptException {
  90. final ScriptEngine result = scriptEngineManager.getEngineByName("JavaScript");
  91. try (FileReader fr = new FileReader(file)) {
  92. result.eval(fr);
  93. } catch (IOException ex) {
  94. throw new ScriptException(ex);
  95. }
  96. result.put("localHelper", localHelper);
  97. result.put("thisEngine", this);
  98. return result;
  99. }
  100. /**
  101. * Call a function in this script.
  102. *
  103. * @param functionName Name of function
  104. * @param args Arguments for function
  105. */
  106. protected void callFunction(final String functionName, final Object... args) {
  107. try {
  108. // Call Function
  109. final Invocable invEngine = (Invocable) engine;
  110. invEngine.invokeFunction(functionName, args);
  111. } catch (NoSuchMethodException nsme) {
  112. // There is no "methodExists" function, so we catch NoSuchMethodException
  113. // and do nothing rather that add an error every time a method is called
  114. // that doesn't exist (such as the action_* methods)
  115. } catch (ScriptException e) {
  116. LOG.info(USER_ERROR, "Error calling '{}' in '{}': {}",
  117. functionName, file.getPath(), e.getMessage(), e);
  118. }
  119. }
  120. /**
  121. * Try to reload this script.
  122. *
  123. * @return True if script was reloaded
  124. */
  125. protected boolean reload() {
  126. // Tell the current engine that its about to be obliterated.
  127. callFunction("onPreRehash");
  128. try {
  129. // Try making a new engine
  130. engine = createEngine();
  131. // Tell it that it has been rehashed
  132. callFunction("onRehashSucess");
  133. } catch (ScriptException e) {
  134. LOG.info(USER_ERROR, "Reloading '{}' failed: {}", file.getPath(), e.getMessage(), e);
  135. // Tell it that its rehash failed
  136. callFunction("onRehashFailed", e);
  137. return false;
  138. }
  139. return true;
  140. }
  141. }