@SuppressWarnings("unchecked") private static void loadBots() { logger.info("Loading available bots"); final Collection<File> botJars = FileUtils.listFiles(botsDirecotry, new String[] {"jar"}, false); for (File botJar : botJars) { try { JarFile jar = new JarFile(botJar); ZipEntry entry = jar.getEntry("bot.yml"); if (entry == null) throw new RuntimeException("Bot has no bot.yml file!"); InputStream is = jar.getInputStream(entry); YAMLNode n = new YAMLNode(new Yaml().loadAs(is, Map.class), true); String mainClass = n.getString("mainClass"); String type = n.getString("type"); URLClassLoader loader = new URLClassLoader(new URL[] {botJar.toURI().toURL()}, Chatty.class.getClassLoader()); BOT_INFO.put(type, new BotClassInfo(loader.loadClass(mainClass).asSubclass(Bot.class), n)); logger.info("Loaded bot '{}'", type); } catch (Exception e) { logger.error("Failed to load bot from {}", botJar.getName()); e.printStackTrace(); } } logger.info("Loaded {} bot(s)", BOT_INFO.size()); }
@SuppressWarnings("unchecked") private static void startPlugins() { logger.info("Loading available plugins"); final Collection<File> botJars = FileUtils.listFiles(pluginsDirecotry, new String[] {"jar"}, false); for (File botJar : botJars) { try { JarFile jar = new JarFile(botJar); ZipEntry ze = jar.getEntry("plugin.yml"); if (ze == null) throw new RuntimeException("Plugin has no plugin.yml file!"); InputStream is = jar.getInputStream(ze); YAMLNode n = new YAMLNode(new Yaml().loadAs(is, Map.class), true); String mainClass = n.getString("mainClass"); String name = n.getString("name"); URLClassLoader loader = new URLClassLoader(new URL[] {botJar.toURI().toURL()}, Chatty.class.getClassLoader()); Plugin plugin = loader.loadClass(mainClass).asSubclass(Plugin.class).newInstance(); plugin.start(); PLUGIN_INFO.put(name, new PluginInfo(name, plugin, n)); logger.info("Loaded plugin '{}'", name); } catch (Exception e) { logger.error("Failed to load plugin from {}", botJar.getName()); e.printStackTrace(); } } logger.info("Loaded {} plugin(s)", PLUGIN_INFO.size()); }
private static void startBots() { logger.info("Starting bots"); List<YAMLNode> nodes = ChatBot.getSettingsManager().getMainConfig().getNodeList("bots", null); for (YAMLNode node : nodes) { final String botType = node.getString("type"); logger.info("Starting {} bot", botType); try { BotClassInfo botClassInfo = BOT_INFO.get(botType); if (botClassInfo == null) { logger.error("Cannot start bot '{}': the specified bot doesn't exist!", botType); continue; } Bot bot = botClassInfo.getBotClass().newInstance(); bot.info = botClassInfo; bot.start(); BOTS.add(bot); } catch (Exception e) { logger.error("Failed to start bot '{}'", botType); e.printStackTrace(); } } logger.info("Started {} bot(s)", BOTS.size()); }