/src/org/angelsl/bukkit/jxpl/ScriptLoader.java

https://github.com/stephank/jxpl · Java · 90 lines · 75 code · 14 blank · 1 comment · 5 complexity · 1358dc97979541c7e37e7881b9c3065c MD5 · raw file

  1. package org.angelsl.bukkit.jxpl;
  2. import org.bukkit.Server;
  3. import org.bukkit.plugin.*;
  4. import javax.script.*;
  5. import java.io.File;
  6. import java.io.FileReader;
  7. import java.util.HashMap;
  8. import java.util.Map;
  9. import java.util.logging.Level;
  10. import java.util.logging.Logger;
  11. import java.util.regex.Matcher;
  12. import java.util.regex.Pattern;
  13. public class ScriptLoader implements PluginLoader {
  14. static Logger l = Logger.getLogger("Minecraft.JxplPlugin");
  15. private final Plugin containing;
  16. private final Server server;
  17. private final File scriptsDir;
  18. private final HashMap<Pattern, ScriptEngineFactory> fileFilters;
  19. private final ScriptEngineManager manager = new ScriptEngineManager();
  20. public ScriptLoader(Plugin containing) {
  21. this.containing = containing;
  22. this.server = containing.getServer();
  23. this.scriptsDir = containing.getDescription().getDataFolder();
  24. fileFilters = new HashMap<Pattern, ScriptEngineFactory>();
  25. for (ScriptEngineFactory sef : manager.getEngineFactories()) {
  26. try {
  27. @SuppressWarnings("unused")
  28. Invocable t = (Invocable)sef.getScriptEngine();
  29. } catch (ClassCastException t) {
  30. // engine does not support invocable. pass.
  31. continue;
  32. }
  33. for (String ext : sef.getExtensions()) {
  34. l.log(Level.INFO, "Adding file extension \"." + ext + "\" for scripting engine \"" + sef.getEngineName() + "\".");
  35. fileFilters.put(Pattern.compile(Pattern.quote("." + ext) + "$"), sef);
  36. }
  37. }
  38. }
  39. public Plugin getContainingPlugin() {
  40. return containing;
  41. }
  42. public void discoverPlugins() {
  43. PluginManager pm = server.getPluginManager();
  44. File[] files = scriptsDir.listFiles();
  45. for (File file : files) {
  46. for (Map.Entry<Pattern, ScriptEngineFactory> entry : fileFilters.entrySet()) {
  47. Matcher matcher = entry.getKey().matcher(file.getName());
  48. if (matcher.find()) {
  49. ScriptDescription description = new ScriptDescription(this, file, entry.getValue());
  50. pm.register(description);
  51. break;
  52. }
  53. }
  54. }
  55. }
  56. public Plugin enablePlugin(PluginDescription abstractDescription) throws InvalidPluginException {
  57. ScriptDescription description = (ScriptDescription)abstractDescription;
  58. ScriptPlugin plugin = new ScriptPlugin(server, description);
  59. ScriptEngine engine = description.getScriptEngine();
  60. try {
  61. FileReader reader = new FileReader(description.getFile());
  62. engine.eval(reader);
  63. reader.close();
  64. }
  65. catch (Exception ex) {
  66. throw new InvalidPluginException(ex);
  67. }
  68. l.log(Level.INFO, "Loaded script " + description.getName());
  69. plugin.tryInvoke("onEnable");
  70. return plugin;
  71. }
  72. public void disablePlugin(Plugin abstractPlugin) {
  73. ScriptPlugin plugin = (ScriptPlugin)abstractPlugin;
  74. plugin.tryInvoke("onDisable");
  75. }
  76. }