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 4.8KB

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