public PluginRef regPlugin(Plugin plugin) { if (plugin == null) return null; plugin.setID(newPluginID()); try { plugin.init(this); } catch (Exception e) { _LOG.error(e.getMessage()); return null; // TODO } // reg ref before start PluginRef ref = null; synchronized (_plugins) { ref = containRef(plugin); if (ref == null) { ref = new DefPluginRef(this, plugin.getName()); _plugins.put(plugin.getID(), ref); } } ref.setPlugin(plugin); // handleAnnation regDispatch(ref); // set updating false if (ref.isUpdating()) ref.setUpdating(false); // start plugin try { plugin.start(); } catch (PluginException e) { unregPlugin(plugin); _LOG.error("Plugin start error: " + plugin.getName() + e.getLocalizedMessage()); return null; } return ref; }
public PluginRef unregPlugin(Plugin plugin) { try { if (plugin.getStatus() == PluginStatus.START) { plugin.stop(); } if (plugin.getStatus() == PluginStatus.STOP) { plugin.destroy(); } } catch (PluginException e) { _LOG.error(e.getLocalizedMessage()); } synchronized (_plugins) { PluginRef ref = _plugins.get(plugin.getID()); if (ref != null) { if (!ref.isUpdating()) { _plugins.remove(plugin.getID()); ref.dispose(); } removeDispatchSource(ref); // ref.setPlugin(null); } return ref; } }
public synchronized List<ProviderIdent> listProviders() { final ArrayList<ProviderIdent> providerIdents = new ArrayList<ProviderIdent>(); final String[] strings = getClassnames(); for (final String classname : strings) { try { providerIdents.add(getProviderDeclaration(loadClass(classname))); } catch (PluginException e) { e.printStackTrace(); } } return providerIdents; }
/** Return true if the file has a class that provides the ident. */ public synchronized boolean isLoaderFor(final ProviderIdent ident) { final String[] strings = getClassnames(); for (final String classname : strings) { try { if (matchesProviderDeclaration(ident, loadClass(classname))) { return true; } } catch (PluginException e) { e.printStackTrace(); } } return false; }
/** * Create and return a List of Strings found in the PluginFile. * * @param pluginFile the pluginFile to be read * @return a List of Strings representing the lines of this file */ public static List<String> readPluginFile(final PluginFile pluginFile) throws PluginException { if (!pluginFile.exists() || pluginFile.isDirectory()) { throw PluginException.create(PluginException.Type.DOES_NOT_EXIST, pluginFile.getFilename()); } List<String> list; try { list = Files.readAllLines(pluginFile.toPath(), Plugin.CHARSET); } catch (IOException | SecurityException e) { throw PluginException.create( PluginException.Type.FILE_READ_ERROR, e, pluginFile.getFilename()); } return list; }
/** * Returns the index of the first occurrence of the keyword, or -1 if not found. Throws an * exception is the keyword is not found. * * @param keyword keyword to be located * @param startNdx file index to start searching for keyword * @return index of the keyword, or -1 if not found. * @throws PluginException if the keyword is not found */ public int checkIndexOf(final PluginKeyword keyword, final int startNdx) throws PluginException { final int index = this.getIndexOf(keyword, startNdx); if (index < 0) { throw PluginException.create( PluginException.Type.MISSING_KEYWORD, filename, keyword.toString()); } return index; }
/** * Returns the parameter(s) found after the specified keyword on the specified line. Throws an * exception if the keyword is not found or is missing parameters. * * @param keyword keyword to be searched * @param line index of line to search * @return parameters found after the specified keyword * @throws PluginException if the keyword or parameters are not found * @throws IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size()) */ public String checkParamsFor(final PluginKeyword keyword, final int lineNdx) throws PluginException, IndexOutOfBoundsException { String[] words = this.getLine(lineNdx).split(PluginPattern.WHITESPACE.toString(), 2); this.checkString(keyword, words[0]); if (words.length < 2) { throw PluginException.create(PluginException.Type.MISSING_PARAMETER, this, words[0]); } return words[1]; }
/** * Construct a <tt>Plugin</tt> containing elements from the specified <tt>List</tt>. Plugins * ignore comments, book-end whitespace, and capitalization. * * @param lines A list of strings used to create this plugin * @param filename The PluginFilename to use * @throws PluginException if no keywords are found */ public Plugin(final List<String> lines, final PluginFilename pluginFilename) throws PluginException { if (lines.isEmpty()) { throw PluginException.create(PluginException.Type.MISSING_KEYWORD, pluginFilename); } this.lines = new ArrayList<String>(lines); this.filename = pluginFilename; processFileLines(this.lines, true); }
/** * Returns the parameter(s) found after the first occurrence of the specified keyword. Throws an * exception if the keyword is not found or is missing parameters. * * @param keyword keyword to be searched * @return parameters found after the specified keyword * @throws PluginException if the keyword or parameters are not found */ public String checkParamsFor(final PluginKeyword keyword) throws PluginException { this.checkIndexOf(keyword); final String s = this.getParamsFor(keyword); if (s == null || s.equals(PluginPattern.BLANK.toString()) || s.equals(PluginPattern.NULL.toString())) { throw PluginException.create( PluginException.Type.MISSING_PARAMETER, filename, keyword.toString()); } return s; }
/** * Check if the name parameter of this <tt>Plugin</tt> is correct. Returns the name parameter if * so. * * @return the name parameter if it is correct * @throws PluginException if the name of this Plugin is incorrect */ public String checkName() throws PluginException { final String nameParam = this.checkParamsFor(PluginKeyword.NAME); if (!nameParam.equals(filename.getRawName())) { throw PluginException.create( PluginException.Type.INVALID_PARAMETER, filename, PluginKeyword.NAME.toString(), nameParam); } return nameParam; }
/** * Returns the numeric parameter found after the the keyword on the specified line. Parameter must * be a whole number. * * @param keyword the keyword to be searched for * @param lineNdx index of the line to search * @return the numeric parameter found after the specified keyword, or -1 if no parameters are * found * @throws IndexOutOfBoundsException if the index is out of range (index < 0 || index >= size()) * @throws PluginException if the keyword is not found or parameters are found but not numeric */ public int checkNumericParams(final PluginKeyword keyword, final int lineNdx) throws PluginException, IndexOutOfBoundsException { int number = -1; final String param = this.getParamsFor(keyword, lineNdx); if (!param.equals(PluginPattern.BLANK.toString())) { try { number = Integer.valueOf(param); } catch (NumberFormatException e) { throw PluginException.create( PluginException.Type.INVALID_PARAMETER, e, this, keyword.toString(), param); } } return number; }
/** Load provider instance for the service */ @SuppressWarnings("unchecked") public synchronized <T> T load(final PluggableService<T> service, final String providerName) throws ProviderLoaderException { final ProviderIdent ident = new ProviderIdent(service.getName(), providerName); debug("loadInstance for " + ident + ": " + pluginJar); if (null == pluginProviderDefs.get(ident)) { final String[] strings = getClassnames(); for (final String classname : strings) { final Class<?> cls; try { cls = loadClass(classname); if (matchesProviderDeclaration(ident, cls)) { pluginProviderDefs.put(ident, cls); } } catch (PluginException e) { log.error( "Failed to load class from " + pluginJar + ": classname: " + classname + ": " + e.getMessage()); } } } final Class<T> cls = pluginProviderDefs.get(ident); if (null != cls) { try { return createProviderForClass(service, cls); } catch (PluginException e) { throw new ProviderLoaderException(e, service.getName(), providerName); } } return null; }
/** * Compare the specified keyword to the specified string. Returns <tt>true</tt> if they are the * same, ignoring case. * * @param string the string to compare * @param keyword the keyword to be compared * @return <tt>true</tt> if they are the same, ignoring case * @throws PluginException if the keyword and string do not match */ public void checkString(final PluginKeyword keyword, final String string) throws PluginException { if (!keyword.compareToString(string)) { throw PluginException.create( PluginException.Type.INVALID_KEYWORD, this, keyword.toString(), string); } }
/** * Check if this plugin is of the specified type. is the same. * * @param type plugin type to test against * @throws PluginException if this plugin does not equal that type */ public void checkType(final Plugin.Type type) throws PluginException { if (filename.getType() != type) { throw PluginException.create(PluginException.Type.INVALID_TYPE, filename); } }
public void run() { status.setIcon(ACTIVE_ICON); updating = true; setAvailableActions(); status.setText("Contacting plugin updates site"); InputStream in = null; try { URL url = context.getPluginUpdatesResource(); URL listURL = new URL(url + "/pluginlist.properties"); in = listURL.openStream(); status.setText("Downloading list of available plugins"); Properties pluginList = new Properties(); pluginList.load(in); HashMap plugins = new HashMap(); for (Iterator i = pluginList.keySet().iterator(); i.hasNext(); ) { String key = (String) i.next(); int idx = key.indexOf('.'); if (idx != -1) { String n = key.substring(0, idx); plugins.put(n, n); } else throw new PluginException( "pluginlist.properties is in incorrect format. Please " + "contact site administrator."); } for (Iterator i = plugins.keySet().iterator(); i.hasNext(); ) { String plugin = (String) i.next(); String propertiesLocation = pluginList.getProperty(plugin + ".properties", ""); if (propertiesLocation.length() == 0) throw new PluginException( "Each plugin in pluginlist.properties must have an entry " + "<plugin-name>.properties specifying the location of the " + "properties file."); String archiveLocation = pluginList.getProperty(plugin + ".archive", ""); if (archiveLocation.length() == 0) throw new PluginException( "Each plugin in pluginlist.properties must have an entry " + "<plugin-name>.archive specifying the location of the " + "archive file."); context.log(PluginHostContext.LOG_INFORMATION, "Found plugin " + plugin); URL propURL = null; try { propURL = new URL(propertiesLocation); } catch (MalformedURLException murle) { propURL = new URL(url + "/" + propertiesLocation); } context.log( PluginHostContext.LOG_DEBUG, "Properties location is " + propURL.toExternalForm()); URL archiveURL = null; try { archiveURL = new URL(archiveLocation); } catch (MalformedURLException murle) { archiveURL = new URL(url + "/" + archiveLocation); } context.log( PluginHostContext.LOG_DEBUG, "Archive location is " + archiveURL.toExternalForm()); try { status.setText("Loading properties for archive " + plugin); HashMap props = manager.loadPluginProperties(propURL); status.setText("Checking plugin archive " + plugin); for (Iterator j = props.keySet().iterator(); j.hasNext(); ) { String name = (String) j.next(); status.setText(" Found plugin " + name); Properties p = (Properties) props.get(name); p.setProperty(PLUGIN_DOWNLOAD_LOCATION, archiveURL.toExternalForm()); model.setRemoteProperties(name, p); } } catch (PluginException pe) { context.log( PluginHostContext.LOG_ERROR, "Could not load plugin.properties for " + plugin, pe); } } status.setText("Update complete"); status.setIcon(IDLE_ICON); } catch (IOException ioe) { status.setIcon(ERROR_ICON); status.setText("Failed! " + ioe.getMessage()); } catch (PluginException pe) { status.setIcon(ERROR_ICON); status.setText("Failed! " + pe.getMessage()); } finally { PluginUtil.closeStream(in); } updating = false; setAvailableActions(); }