Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Controller
- public class ModuleLoaderController {
- private File moduleDir;
- private Set<StormPlugin> enabledPlugins;
- private Set<StormPlugin> plugins;
- @Autowired
- private ApplicationContext context;
- public ModuleLoaderController(@Autowired @Qualifier("modules-dir") File moduleDir) {
- this.moduleDir = moduleDir;
- this.enabledPlugins = new HashSet<>();
- this.plugins = new HashSet<>();
- }
- @PostConstruct
- public void loadModules() {
- if (!moduleDir.exists()) {
- moduleDir.mkdir();
- }
- try (Stream<Path> pathStream = Files.walk(moduleDir.toPath().toAbsolutePath())) {
- pathStream.filter(path1 -> path1.toString().endsWith(".jar")).forEach(p -> {
- try {
- StormPlugin pluginModule = loadModule(p);
- context.getAutowireCapableBeanFactory().autowireBean(pluginModule);
- this.plugins.add(pluginModule);
- enableModule(pluginModule);
- } catch (Exception e) {
- e.printStackTrace();
- }
- });
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public StormPlugin loadModule(Path path) throws Exception {
- if (!path.toFile().exists()) {
- throw new Exception(String.format("Could not find module at %s", path.toAbsolutePath()));
- }
- JarFile file = new JarFile(path.toFile());
- ZipEntry moduleJson = file.getEntry("module.json");
- JarInputStream jarInputStream = new JarInputStream(new FileInputStream(path.toFile()));
- System.out.println(path.toUri().toURL());
- if (moduleJson == null) {
- throw new Exception(String.format("Could not find module json at %s", path.toAbsolutePath()));
- }
- String moduleJsonSTR = CharStreams
- .toString(new InputStreamReader(file.getInputStream(moduleJson), Charsets.UTF_8));
- Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
- .setPrettyPrinting().create();
- PluginConfig config = gson.fromJson(moduleJsonSTR, PluginConfig.class);
- // URLClassLoader classLoader = URLClassLoader.newInstance(new URL[]{path.toUri().toURL()});
- URLClassLoader classLoader = new URLClassLoader(new URL[]{path.toUri().toURL()},
- getClass().getClassLoader());
- Class mainClass = classLoader.loadClass(config.getMain());
- Enumeration<JarEntry> entries = file.entries();
- while (entries.hasMoreElements()) {
- JarEntry clazz = entries.nextElement();
- if (clazz.getName().equals(mainClass.getName())) {
- System.out.println("FOUND MAIN AND SKIPPING");
- continue;
- }
- if (clazz.isDirectory() || !clazz.getName().endsWith(".class")) {
- continue;
- }
- String className = clazz.getName().substring(0, clazz.getName().length() - 6);
- className = className.replace('/', '.');
- classLoader.loadClass(className);
- }
- StormPlugin module = (StormPlugin) mainClass.newInstance();
- System.out.println(module.getConfigurationClass().getName());
- module.setName(config.getName());
- /**
- * Field versionField =
- * module.getClass().getSuperclass().getDeclaredField("version");
- * versionField.setAccessible(true); versionField.set(module,
- * config.getVersion()); versionField.setAccessible(false);
- *
- * Field descField =
- * module.getClass().getSuperclass().getDeclaredField("description");
- * descField.setAccessible(true); descField.set(module,
- * config.getDescription()); versionField.setAccessible(false);
- **/
- /*
- * Field commandField =
- * module.getClass().getSuperclass().getDeclaredField("commands");
- * commandField.setAccessible(true); commandField.set(module,
- * config.getCommands()); versionField.setAccessible(false);
- */
- file.close();
- classLoader.close();
- return module;
- }
- public void enableModule(StormPlugin plugin) {
- if (enabledPlugins.contains(plugin)) {
- return;
- }
- plugin.enable();
- this.enabledPlugins.add(plugin);
- }
- public void disableModule(String name) {
- StormPlugin p = byName(name);
- disableModule(p);
- }
- public void disableModule(StormPlugin plugin) {
- if (plugin == null) {
- return;
- }
- plugin.disable();
- this.enabledPlugins.remove(plugin);
- }
- public File findModule(String name) {
- return Arrays.asList(moduleDir.listFiles()).stream().filter(file -> file.getName().contains(name)).findFirst()
- .orElse(null);
- }
- public Set<StormPlugin> getEnabledPlugins() {
- return enabledPlugins;
- }
- public StormPlugin byName(String name) {
- return enabledPlugins.stream().filter(p -> p.getName().equalsIgnoreCase(name)).findFirst().orElse(null);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement