use of org.jivesoftware.spark.plugin.PluginDependency in project Spark by igniterealtime.
the class PluginDependencyTest method testCompare.
@Test
public void testCompare() {
PluginDependency depend = new PluginDependency();
depend.setName("Test");
depend.setVersion("1.0.0");
assertTrue(depend.compareVersion("1.0.0"));
assertTrue(depend.compareVersion("1.1"));
assertTrue(depend.compareVersion("1.1.1"));
assertFalse(depend.compareVersion("0.9.0"));
assertTrue(depend.compareVersion("1.9.0.0"));
assertFalse(depend.compareVersion("0.9.1"));
assertTrue(depend.compareVersion("1.9.1"));
depend.setVersion("2.0.0.0");
assertTrue(depend.compareVersion("2.1"));
assertTrue(depend.compareVersion("2.1.1"));
assertFalse(depend.compareVersion("0.9.0"));
assertTrue(depend.compareVersion("2.9.0.0"));
assertFalse(depend.compareVersion("0.9.1"));
assertTrue(depend.compareVersion("2.9.1"));
depend.setVersion("2.1");
assertTrue(depend.compareVersion("2.1"));
assertTrue(depend.compareVersion("2.1.1"));
assertTrue(depend.compareVersion("2.1.0"));
assertFalse(depend.compareVersion("0.9.0"));
assertTrue(depend.compareVersion("2.9.0.0"));
assertFalse(depend.compareVersion("0.9.1"));
assertTrue(depend.compareVersion("2.9.1"));
}
use of org.jivesoftware.spark.plugin.PluginDependency in project Spark by igniterealtime.
the class PluginManager method loadPublicPlugin.
/**
* Loads public plugins.
*
* @param pluginDir the directory of the expanded public plugin.
* @return the new Plugin model for the Public Plugin.
*/
private Plugin loadPublicPlugin(File pluginDir) {
File pluginFile = new File(pluginDir, "plugin.xml");
SAXReader saxReader = new SAXReader();
Document pluginXML = null;
try {
pluginXML = saxReader.read(pluginFile);
} catch (DocumentException e) {
Log.error("Unable to read plugin XML file from " + pluginDir, e);
}
Plugin pluginClass = null;
List<? extends Node> plugins = pluginXML.selectNodes("/plugin");
for (Node plugin : plugins) {
PublicPlugin publicPlugin = new PublicPlugin();
String clazz = null;
String name;
String minVersion;
try {
name = plugin.selectSingleNode("name").getText();
clazz = plugin.selectSingleNode("class").getText();
try {
String lower = name.replaceAll("[^0-9a-zA-Z]", "").toLowerCase();
// Dont load the plugin if its on the Blacklist
if (_blacklistPlugins.contains(lower) || _blacklistPlugins.contains(clazz) || SettingsManager.getLocalPreferences().getDeactivatedPlugins().contains(name)) {
return null;
}
} catch (Exception e) {
Log.warning("An exception occurred while checking the plugin blacklist for " + name, e);
return null;
}
// Check for minimum Spark version
try {
minVersion = plugin.selectSingleNode("minSparkVersion").getText();
String buildNumber = JiveInfo.getVersion();
boolean ok = buildNumber.compareTo(minVersion) >= 0;
if (!ok) {
return null;
}
} catch (Exception e) {
Log.error("Unable to load plugin " + name + " due to missing <minSparkVersion>-Tag in plugin.xml.");
return null;
}
// Check for minimum Java version
try {
String javaversion = plugin.selectSingleNode("java").getText().replaceAll("[^0-9]", "");
javaversion = javaversion == null ? "0" : javaversion;
int jv = Integer.parseInt(attachMissingZero(javaversion));
String myversion = System.getProperty("java.version").replaceAll("[^0-9]", "");
int mv = Integer.parseInt(attachMissingZero(myversion));
boolean ok = (mv >= jv);
if (!ok) {
Log.error("Unable to load plugin " + name + " due to old JavaVersion.\n" + "It Requires " + plugin.selectSingleNode("java").getText() + " you have " + System.getProperty("java.version"));
return null;
}
} catch (NullPointerException e) {
Log.warning("Plugin " + name + " has no <java>-Tag, consider getting a newer Version");
}
// set dependencies
try {
List<? extends Node> dependencies = plugin.selectNodes("depends/plugin");
for (Node depend1 : dependencies) {
Element depend = (Element) depend1;
PluginDependency dependency = new PluginDependency();
dependency.setVersion(depend.selectSingleNode("version").getText());
dependency.setName(depend.selectSingleNode("name").getText());
publicPlugin.addDependency(dependency);
}
} catch (Exception e) {
Log.warning("An exception occurred during the setting of dependencies while loading plugin " + name, e);
}
// Do operating system check.
boolean operatingSystemOK = isOperatingSystemOK(plugin);
if (!operatingSystemOK) {
return null;
}
publicPlugin.setPluginClass(clazz);
publicPlugin.setName(name);
try {
String version = plugin.selectSingleNode("version").getText();
publicPlugin.setVersion(version);
String author = plugin.selectSingleNode("author").getText();
publicPlugin.setAuthor(author);
String email = plugin.selectSingleNode("email").getText();
publicPlugin.setEmail(email);
String description = plugin.selectSingleNode("description").getText();
publicPlugin.setDescription(description);
String homePage = plugin.selectSingleNode("homePage").getText();
publicPlugin.setHomePage(homePage);
} catch (Exception e) {
Log.debug("An ignorable exception occurred while loading plugin " + name + ": " + e.getMessage());
}
try {
pluginClass = (Plugin) getParentClassLoader().loadClass(clazz).newInstance();
Log.debug(name + " has been loaded.");
publicPlugin.setPluginDir(pluginDir);
publicPlugins.add(publicPlugin);
registerPlugin(pluginClass);
} catch (Throwable e) {
Log.error("Unable to load plugin " + clazz + ".", e);
}
} catch (Exception ex) {
Log.error("Unable to load plugin " + clazz + ".", ex);
}
}
return pluginClass;
}
use of org.jivesoftware.spark.plugin.PluginDependency in project Spark by igniterealtime.
the class PluginManager method initializePlugins.
/**
* Loads and initalizes all Plugins.
*
* @see Plugin
*/
public void initializePlugins() {
try {
int j;
boolean dependencyFound;
// Dependency check
for (int i = 0; i < publicPlugins.size(); i++) {
// if dependencies are available, check these
if ((publicPlugins.get(i)).getDependency().size() > 0) {
List<PluginDependency> dependencies = (publicPlugins.get(i)).getDependency();
// go trough all dependencies
for (PluginDependency dependency : dependencies) {
j = 0;
dependencyFound = false;
// look for the specific plugin
for (PublicPlugin plugin : publicPlugins) {
if (plugin.getName() != null && plugin.getName().equals(dependency.getName())) {
// if the version is compatible then reorder
if (dependency.compareVersion(plugin.getVersion())) {
dependencyFound = true;
// when depended Plugin hadn't been installed yet
if (j > i) {
// find the position of plugins-List because it has more entries
int counter = 0, x = 0, z = 0;
for (Plugin plug : plugins) {
// find the position of the aim-object
if (plug.getClass().toString().substring(6).equals(publicPlugins.get(j).getPluginClass())) {
x = counter;
} else // find the change-position
if (plug.getClass().toString().substring(6).equals(publicPlugins.get(i).getPluginClass())) {
z = counter;
}
counter++;
}
// change the order
publicPlugins.add(i, publicPlugins.get(j));
publicPlugins.remove(j + 1);
plugins.add(z, plugins.get(x));
plugins.remove(x + 1);
// start again, to check the other dependencies
i--;
}
} else // else don't load the plugin and show an error
{
Log.error("Depended Plugin " + dependency.getName() + " hasn't the right version (" + dependency.getVersion() + "<>" + plugin.getVersion());
}
break;
}
j++;
}
// If the depended Plugin wasn't found, then show error.
if (!dependencyFound) {
Log.error("Depended Plugin " + dependency.getName() + " is missing for the Plugin " + (publicPlugins.get(i)).getName());
// find the posiion of plugins-List because it has more entries
int counter = 0;
for (Plugin plug : plugins) {
// find the delete-position
if (plug.getClass().toString().substring(6).equals(publicPlugins.get(i).getPluginClass())) {
break;
}
counter++;
}
// delete the Plugin, because the depended Plugin is missing
publicPlugins.remove(i);
plugins.remove(counter);
i--;
break;
}
}
}
}
EventQueue.invokeLater(() -> {
for (Plugin plugin : plugins) {
long start = System.currentTimeMillis();
Log.debug("Trying to initialize " + plugin);
try {
plugin.initialize();
long end = System.currentTimeMillis();
Log.debug("Took " + (end - start) + " ms. to load " + plugin);
} catch (Throwable e) {
Log.error("An exception occurred while initializing plugin " + plugin, e);
}
}
});
} catch (Exception e) {
Log.error("An exception occurred while initializing plugins.", e);
}
}
Aggregations