您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ScriptManager.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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.dmdirc.commandline.CommandLineOptionsModule.Directory;
  19. import java.io.File;
  20. import java.util.Arrays;
  21. import java.util.HashMap;
  22. import java.util.LinkedList;
  23. import java.util.List;
  24. import java.util.Map;
  25. import javax.inject.Inject;
  26. import javax.script.ScriptEngineManager;
  27. import javax.script.ScriptException;
  28. import org.slf4j.Logger;
  29. import org.slf4j.LoggerFactory;
  30. import static com.dmdirc.util.LogUtils.USER_ERROR;
  31. public class ScriptManager {
  32. private static final Logger LOG = LoggerFactory.getLogger(ScriptManager.class);
  33. /** Script engine manager. */
  34. private final ScriptEngineManager scriptEngineManager;
  35. /** Script directory. */
  36. private final String scriptDirectory;
  37. /** Store Script State Name,Engine */
  38. private final Map<String, ScriptEngineWrapper> scripts = new HashMap<>();
  39. @Inject
  40. public ScriptManager(final ScriptEngineManager scriptEngineManager,
  41. @Directory(ScriptModule.SCRIPTS) final String scriptDirectory) {
  42. this.scriptEngineManager = scriptEngineManager;
  43. this.scriptDirectory = scriptDirectory;
  44. }
  45. /**
  46. * Get a clone of the scripts map.
  47. *
  48. * @return a clone of the scripts map
  49. */
  50. protected Map<String, ScriptEngineWrapper> getScripts() {
  51. return new HashMap<>(scripts);
  52. }
  53. /** Reload all scripts */
  54. public void rehash() {
  55. scripts.values().forEach(ScriptEngineWrapper::reload);
  56. // Advise the Garbage collector that now would be a good time to run
  57. System.gc();
  58. }
  59. /**
  60. * Call a function in all scripts.
  61. *
  62. * @param functionName Name of function
  63. * @param args Arguments for function
  64. */
  65. public void callFunctionAll(final String functionName, final Object... args) {
  66. for (final ScriptEngineWrapper engine : scripts.values()) {
  67. engine.callFunction(functionName, args);
  68. }
  69. }
  70. /**
  71. * Load a script file into a new jsEngine
  72. *
  73. * @param scriptFilename Path to script
  74. *
  75. * @return true for Success (or already loaded), false for fail. (Fail occurs if script already
  76. * exists, or if it has errors)
  77. */
  78. public boolean loadScript(final String scriptFilename) {
  79. if (!scripts.containsKey(scriptFilename)) {
  80. try {
  81. final ScriptEngineWrapper wrapper = new ScriptEngineWrapper(scriptEngineManager,
  82. scriptFilename);
  83. scripts.put(scriptFilename, wrapper);
  84. } catch (ScriptException e) {
  85. LOG.info(USER_ERROR, "Error loading '{}': {}", scriptFilename, e.getMessage(), e);
  86. return false;
  87. }
  88. }
  89. return true;
  90. }
  91. /**
  92. * Unload a script file.
  93. *
  94. * @param scriptFilename Path to script
  95. */
  96. public void unloadScript(final String scriptFilename) {
  97. if (scripts.containsKey(scriptFilename)) {
  98. // Tell it that its about to be unloaded.
  99. scripts.get(scriptFilename).callFunction("onUnload");
  100. // Remove the script
  101. scripts.remove(scriptFilename);
  102. // Advise the Garbage collector that now would be a good time to run
  103. System.gc();
  104. }
  105. }
  106. /**
  107. * Retrieves a list of all installed scripts. Any file under the main plugin directory
  108. * (~/.DMDirc/scripts or similar) that matches *.js is deemed to be a valid script.
  109. *
  110. * @return A list of all installed scripts
  111. */
  112. public List<String> getPossibleScripts() {
  113. final List<String> res = new LinkedList<>();
  114. final LinkedList<File> dirs = new LinkedList<>();
  115. dirs.add(new File(scriptDirectory));
  116. while (!dirs.isEmpty()) {
  117. final File dir = dirs.pop();
  118. if (dir.isDirectory()) {
  119. dirs.addAll(Arrays.asList(dir.listFiles()));
  120. } else if (dir.isFile() && dir.getName().endsWith(".js")) {
  121. final String target = dir.getPath();
  122. res.add(target.substring(scriptDirectory.length(), target.length()));
  123. }
  124. }
  125. return res;
  126. }
  127. }