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.

ScriptManager.java 5.2KB

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