Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.20 KB | None | 0 0
  1. @Controller
  2. public class ModuleLoaderController {
  3.  
  4.     private File moduleDir;
  5.     private Set<StormPlugin> enabledPlugins;
  6.     private Set<StormPlugin> plugins;
  7.     @Autowired
  8.     private ApplicationContext context;
  9.  
  10.     public ModuleLoaderController(@Autowired @Qualifier("modules-dir") File moduleDir) {
  11.         this.moduleDir = moduleDir;
  12.         this.enabledPlugins = new HashSet<>();
  13.         this.plugins = new HashSet<>();
  14.     }
  15.  
  16.     @PostConstruct
  17.     public void loadModules() {
  18.  
  19.         if (!moduleDir.exists()) {
  20.             moduleDir.mkdir();
  21.         }
  22.  
  23.         try (Stream<Path> pathStream = Files.walk(moduleDir.toPath().toAbsolutePath())) {
  24.             pathStream.filter(path1 -> path1.toString().endsWith(".jar")).forEach(p -> {
  25.                 try {
  26.                     StormPlugin pluginModule = loadModule(p);
  27.                     context.getAutowireCapableBeanFactory().autowireBean(pluginModule);
  28.                     this.plugins.add(pluginModule);
  29.                     enableModule(pluginModule);
  30.                 } catch (Exception e) {
  31.                     e.printStackTrace();
  32.                 }
  33.             });
  34.         } catch (IOException e) {
  35.             e.printStackTrace();
  36.         }
  37.     }
  38.  
  39.     public StormPlugin loadModule(Path path) throws Exception {
  40.         if (!path.toFile().exists()) {
  41.             throw new Exception(String.format("Could not find module at %s", path.toAbsolutePath()));
  42.         }
  43.  
  44.         JarFile file = new JarFile(path.toFile());
  45.         ZipEntry moduleJson = file.getEntry("module.json");
  46.  
  47.         JarInputStream jarInputStream = new JarInputStream(new FileInputStream(path.toFile()));
  48.  
  49.         System.out.println(path.toUri().toURL());
  50.  
  51.         if (moduleJson == null) {
  52.             throw new Exception(String.format("Could not find module json at %s", path.toAbsolutePath()));
  53.         }
  54.  
  55.         String moduleJsonSTR = CharStreams
  56.                 .toString(new InputStreamReader(file.getInputStream(moduleJson), Charsets.UTF_8));
  57.         Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
  58.                 .setPrettyPrinting().create();
  59.  
  60.         PluginConfig config = gson.fromJson(moduleJsonSTR, PluginConfig.class);
  61.  
  62. //        URLClassLoader classLoader = URLClassLoader.newInstance(new URL[]{path.toUri().toURL()});
  63.         URLClassLoader classLoader = new URLClassLoader(new URL[]{path.toUri().toURL()},
  64.                 getClass().getClassLoader());
  65.         Class mainClass = classLoader.loadClass(config.getMain());
  66.  
  67.         Enumeration<JarEntry> entries = file.entries();
  68.  
  69.         while (entries.hasMoreElements()) {
  70.             JarEntry clazz = entries.nextElement();
  71.  
  72.             if (clazz.getName().equals(mainClass.getName())) {
  73.                 System.out.println("FOUND MAIN AND SKIPPING");
  74.                 continue;
  75.             }
  76.  
  77.             if (clazz.isDirectory() || !clazz.getName().endsWith(".class")) {
  78.                 continue;
  79.             }
  80.             String className = clazz.getName().substring(0, clazz.getName().length() - 6);
  81.             className = className.replace('/', '.');
  82.             classLoader.loadClass(className);
  83.         }
  84.  
  85.         StormPlugin module = (StormPlugin) mainClass.newInstance();
  86.         System.out.println(module.getConfigurationClass().getName());
  87.  
  88.         module.setName(config.getName());
  89.  
  90.         /**
  91.          * Field versionField =
  92.          * module.getClass().getSuperclass().getDeclaredField("version");
  93.          * versionField.setAccessible(true); versionField.set(module,
  94.          * config.getVersion()); versionField.setAccessible(false);
  95.          *
  96.          * Field descField =
  97.          * module.getClass().getSuperclass().getDeclaredField("description");
  98.          * descField.setAccessible(true); descField.set(module,
  99.          * config.getDescription()); versionField.setAccessible(false);
  100.          **/
  101.  
  102.         /*
  103.          * Field commandField =
  104.          * module.getClass().getSuperclass().getDeclaredField("commands");
  105.          * commandField.setAccessible(true); commandField.set(module,
  106.          * config.getCommands()); versionField.setAccessible(false);
  107.          */
  108.  
  109.         file.close();
  110.         classLoader.close();
  111.         return module;
  112.     }
  113.  
  114.     public void enableModule(StormPlugin plugin) {
  115.         if (enabledPlugins.contains(plugin)) {
  116.             return;
  117.         }
  118.  
  119.         plugin.enable();
  120.         this.enabledPlugins.add(plugin);
  121.     }
  122.  
  123.     public void disableModule(String name) {
  124.         StormPlugin p = byName(name);
  125.         disableModule(p);
  126.     }
  127.  
  128.     public void disableModule(StormPlugin plugin) {
  129.         if (plugin == null) {
  130.             return;
  131.         }
  132.         plugin.disable();
  133.         this.enabledPlugins.remove(plugin);
  134.     }
  135.  
  136.     public File findModule(String name) {
  137.         return Arrays.asList(moduleDir.listFiles()).stream().filter(file -> file.getName().contains(name)).findFirst()
  138.                 .orElse(null);
  139.     }
  140.  
  141.     public Set<StormPlugin> getEnabledPlugins() {
  142.         return enabledPlugins;
  143.     }
  144.  
  145.     public StormPlugin byName(String name) {
  146.         return enabledPlugins.stream().filter(p -> p.getName().equalsIgnoreCase(name)).findFirst().orElse(null);
  147.     }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement