private Map<String, Plugin> loadPluginsFromClasspath(Settings settings) { Map<String, Plugin> plugins = newHashMap(); Enumeration<URL> pluginUrls = null; try { pluginUrls = settings.getClassLoader().getResources("es-plugin.properties"); } catch (IOException e) { logger.warn("Failed to find plugins from classpath", e); } while (pluginUrls.hasMoreElements()) { URL pluginUrl = pluginUrls.nextElement(); Properties pluginProps = new Properties(); InputStream is = null; try { is = pluginUrl.openStream(); pluginProps.load(is); String sPluginClass = pluginProps.getProperty("plugin"); Class<?> pluginClass = settings.getClassLoader().loadClass(sPluginClass); Plugin plugin = (Plugin) pluginClass.newInstance(); plugins.put(plugin.name(), plugin); } catch (Exception e) { logger.warn("Failed to load plugin from [" + pluginUrl + "]", e); } finally { if (is != null) { try { is.close(); } catch (IOException e) { // ignore } } } } return plugins; }
@Override public Object parseClass(Class<?> c, Object o, MainView view) { if (c.isAnnotationPresent(Plugin.class)) { Plugin p = c.getAnnotation(Plugin.class); Log.getLogger() .log( Level.INFO, "[Plugin: " + p.name() + ", Version: " + p.version() + ", Author: " + p.author() + ", Description: " + p.description() + "]"); } return o; }
/** Get the Plugin annotation for the class */ static Plugin getPluginMetadata(final Class<?> cls) throws PluginException { // try to get plugin provider name final String pluginname; if (!cls.isAnnotationPresent(Plugin.class)) { throw new PluginException("No Plugin annotation was found for the class: " + cls.getName()); } final Plugin annotation = (Plugin) cls.getAnnotation(Plugin.class); pluginname = annotation.name(); if (null == pluginname || "".equals(pluginname)) { throw new PluginException( "Plugin annotation 'name' cannot be empty for the class: " + cls.getName()); } // get service name from annotation final String servicename = annotation.service(); if (null == servicename || "".equals(servicename)) { throw new PluginException( "Plugin annotation 'service' cannot be empty for the class: " + cls.getName()); } return annotation; }
/** * Attempt to create an instance of thea provider for the given service * * @param cls class * @return created instance */ static <T, X extends T> T createProviderForClass( final PluggableService<T> service, final Class<X> cls) throws PluginException, ProviderCreationException { debug("Try loading provider " + cls.getName()); final Plugin annotation = getPluginMetadata(cls); final String pluginname = annotation.name(); if (!service.isValidProviderClass(cls)) { throw new PluginException( "Class " + cls.getName() + " was not a valid plugin class for service: " + service.getName() + ". Expected class " + cls.getName() + ", with a public constructor with no parameter"); } debug("Succeeded loading plugin " + cls.getName() + " for service: " + service.getName()); return service.createProviderInstance(cls, pluginname); }
/** Return true if the ident matches the Plugin annotation for the class */ static ProviderIdent getProviderDeclaration(final Class<?> cls) throws PluginException { final Plugin annotation = getPluginMetadata(cls); return new ProviderIdent(annotation.service(), annotation.name()); }
/** Return true if the ident matches the Plugin annotation for the class */ static boolean matchesProviderDeclaration(final ProviderIdent ident, final Class<?> cls) throws PluginException { final Plugin annotation = getPluginMetadata(cls); return ident.getFirst().equals(annotation.service()) && ident.getSecond().equals(annotation.name()); }
@Override public final Set<String> getRegisteredProtocols(Plugin plugin) throws PluginNotFoundException { return PlayerManager.getInstance() .getPlayerInfo("core", plugin.name()) .getRegisteredProtocols(); }