Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

ScriptEngineWrapper.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright (c) 2006-2011 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.ErrorLevel;
  24. import com.dmdirc.logger.Logger;
  25. import com.dmdirc.util.StreamUtil;
  26. import java.io.File;
  27. import java.io.FileNotFoundException;
  28. import java.io.FileReader;
  29. import javax.script.Invocable;
  30. import javax.script.ScriptEngine;
  31. import javax.script.ScriptException;
  32. /**
  33. * Class to create script engines!
  34. *
  35. * @author Shane 'Dataforce' McCormack
  36. */
  37. public class ScriptEngineWrapper {
  38. /** The Script Engine this wrapper wraps */
  39. private ScriptEngine engine;
  40. /** The File this script is from */
  41. private final File file;
  42. /** Script-Local JS Helper */
  43. private JavaScriptHelper localHelper = new JavaScriptHelper();
  44. /** The script plugin that owns this wrapper. */
  45. private final ScriptPlugin plugin;
  46. /**
  47. * Create a new ScriptEngineWrapper
  48. *
  49. * @param plugin The script plugin that owns this wrapper
  50. * @param filename Filename of script
  51. */
  52. protected ScriptEngineWrapper(final ScriptPlugin plugin, final String filename) throws FileNotFoundException, ScriptException {
  53. this.plugin = plugin;
  54. file = (filename != null) ? new File(filename) : null;
  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() { return engine; }
  64. /**
  65. * Get a reference to the JavaScriptHelper
  66. *
  67. * @return a reference to the JavaScriptHelper
  68. */
  69. protected JavaScriptHelper getJavaScriptHelper() { return localHelper; }
  70. /**
  71. * Get the file for this script
  72. *
  73. * @return The file for this script
  74. */
  75. protected File getFile() { return file; }
  76. /**
  77. * Create a new engine for this script
  78. */
  79. protected ScriptEngine createEngine() throws FileNotFoundException, ScriptException {
  80. final ScriptEngine result = plugin.getScriptFactory().getEngineByName("JavaScript");
  81. if (file != null) {
  82. FileReader fr = null;
  83. try {
  84. fr = new FileReader(file);
  85. result.eval(fr);
  86. } finally {
  87. StreamUtil.close(fr);
  88. }
  89. }
  90. result.put("localHelper", localHelper);
  91. result.put("thisEngine", this);
  92. return result;
  93. }
  94. /**
  95. * Call a function in this script.
  96. *
  97. * @param functionName Name of function
  98. * @param args Arguments for function
  99. */
  100. protected void callFunction(final String functionName, final Object... args) {
  101. try {
  102. // Call Function
  103. final Invocable invEngine = (Invocable) engine;
  104. invEngine.invokeFunction(functionName, args);
  105. } catch (NoSuchMethodException nsme) {
  106. // There is no "methodExists" function, so we catch NoSuchMethodException
  107. // and do nothing rather that add an erropr every time a method is called
  108. // that doesn't exist (such as the action_* methods)
  109. } catch (Exception e) {
  110. Logger.userError(ErrorLevel.LOW, "Error calling '"+functionName+"' in '"+file.getPath()+"': "+e.getMessage(), e);
  111. }
  112. }
  113. /**
  114. * Try to reload this script.
  115. */
  116. protected boolean reload() {
  117. // Tell the current engine that its about to be obliterated.
  118. callFunction("onPreRehash");
  119. try {
  120. // Try making a new engine
  121. engine = createEngine();
  122. // Tell it that it has been rehashed
  123. callFunction("onRehashSucess");
  124. } catch (Exception e) {
  125. Logger.userError(ErrorLevel.LOW, "Reloading '"+file.getPath()+"' failed: "+e.getMessage(), e);
  126. // Tell it that its rehash failed
  127. callFunction("onRehashFailed", e);
  128. return false;
  129. }
  130. return true;
  131. }
  132. }