Search in sources :

Example 1 with Plugin

use of org.jivesoftware.spark.plugin.Plugin in project Spark by igniterealtime.

the class PluginManager method loadInternalPlugins.

/**
 * Loads an internal plugin.
 *
 * @param reader the inputstreamreader for an internal plugin.
 */
private void loadInternalPlugins(InputStreamReader reader) {
    SAXReader saxReader = new SAXReader();
    Document pluginXML = null;
    try {
        pluginXML = saxReader.read(reader);
    } catch (DocumentException e) {
        Log.error(e);
    }
    List<Element> plugins = pluginXML.selectNodes("/plugins/plugin");
    for (final Element plugin : plugins) {
        EventQueue.invokeLater(() -> {
            String clazz = null;
            String name;
            try {
                name = plugin.selectSingleNode("name").getText();
                clazz = plugin.selectSingleNode("class").getText();
                Plugin pluginClass1 = (Plugin) Class.forName(clazz).newInstance();
                Log.debug(name + " has been loaded. Internal plugin.");
                registerPlugin(pluginClass1);
            } catch (Throwable ex) {
                Log.error("Unable to load plugin " + clazz + ".", ex);
            }
        });
    }
}
Also used : SAXReader(org.dom4j.io.SAXReader) DocumentException(org.dom4j.DocumentException) Element(org.dom4j.Element) Document(org.dom4j.Document) PublicPlugin(org.jivesoftware.spark.plugin.PublicPlugin) Plugin(org.jivesoftware.spark.plugin.Plugin)

Example 2 with Plugin

use of org.jivesoftware.spark.plugin.Plugin in project Spark by igniterealtime.

the class TranslatorPlugin method initialize.

/**
 * Called after Spark is loaded to initialize the new plugin.
 */
public void initialize() {
    // Retrieve ChatManager from the SparkManager
    final ChatManager chatManager = SparkManager.getChatManager();
    // Add to a new ChatRoom when the ChatRoom opens.
    chatManager.addChatRoomListener(new ChatRoomListenerAdapter() {

        public void chatRoomOpened(ChatRoom room) {
            // only do the translation for single chat
            if (room instanceof ChatRoomImpl) {
                final ChatRoomImpl roomImpl = (ChatRoomImpl) room;
                // Create a new ChatRoomButton.
                final JComboBox<TranslatorUtil.TranslationType> translatorBox = new JComboBox<>(TranslatorUtil.TranslationType.getTypes());
                translatorBox.addActionListener(e -> {
                    // Set the focus back to the message box.
                    roomImpl.getChatInputEditor().requestFocusInWindow();
                });
                roomImpl.addChatRoomComponent(translatorBox);
                // do the translation for outgoing messages.
                final MessageEventListener messageListener = new MessageEventListener() {

                    public void sendingMessage(Message message) {
                        String currentBody = message.getBody();
                        String oldBody = message.getBody();
                        TranslatorUtil.TranslationType type = (TranslatorUtil.TranslationType) translatorBox.getSelectedItem();
                        if (type != null && type != TranslatorUtil.TranslationType.None) {
                            message.setBody(null);
                            currentBody = TranslatorUtil.translate(currentBody, type);
                            TranscriptWindow transcriptWindow = chatManager.getChatRoom(XmppStringUtils.parseBareJid(message.getTo())).getTranscriptWindow();
                            if (oldBody.equals(currentBody.substring(0, currentBody.length() - 1))) {
                                transcriptWindow.insertNotificationMessage("Could not translate: " + currentBody, ChatManager.ERROR_COLOR);
                            } else {
                                transcriptWindow.insertNotificationMessage("-> " + currentBody, Color.gray);
                                message.setBody(currentBody);
                            }
                        }
                    }

                    public void receivingMessage(Message message) {
                    // do nothing
                    }
                };
                roomImpl.addMessageEventListener(messageListener);
            }
        }
    });
}
Also used : Color(java.awt.Color) ChatManager(org.jivesoftware.spark.ChatManager) TranscriptWindow(org.jivesoftware.spark.ui.TranscriptWindow) SparkManager(org.jivesoftware.spark.SparkManager) ChatRoom(org.jivesoftware.spark.ui.ChatRoom) ChatRoomImpl(org.jivesoftware.spark.ui.rooms.ChatRoomImpl) ChatRoomListenerAdapter(org.jivesoftware.spark.ui.ChatRoomListenerAdapter) MessageEventListener(org.jivesoftware.spark.ui.MessageEventListener) Message(org.jivesoftware.smack.packet.Message) JComboBox(javax.swing.JComboBox) Plugin(org.jivesoftware.spark.plugin.Plugin) XmppStringUtils(org.jxmpp.util.XmppStringUtils) JComboBox(javax.swing.JComboBox) Message(org.jivesoftware.smack.packet.Message) MessageEventListener(org.jivesoftware.spark.ui.MessageEventListener) ChatRoomImpl(org.jivesoftware.spark.ui.rooms.ChatRoomImpl) ChatRoomListenerAdapter(org.jivesoftware.spark.ui.ChatRoomListenerAdapter) ChatRoom(org.jivesoftware.spark.ui.ChatRoom) TranscriptWindow(org.jivesoftware.spark.ui.TranscriptWindow) ChatManager(org.jivesoftware.spark.ChatManager)

Example 3 with Plugin

use of org.jivesoftware.spark.plugin.Plugin 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;
}
Also used : PluginDependency(org.jivesoftware.spark.plugin.PluginDependency) SAXReader(org.dom4j.io.SAXReader) Node(org.dom4j.Node) Element(org.dom4j.Element) PublicPlugin(org.jivesoftware.spark.plugin.PublicPlugin) Document(org.dom4j.Document) DocumentException(org.dom4j.DocumentException) MalformedURLException(java.net.MalformedURLException) DocumentException(org.dom4j.DocumentException) JarFile(java.util.jar.JarFile) ZipFile(java.util.zip.ZipFile) PublicPlugin(org.jivesoftware.spark.plugin.PublicPlugin) Plugin(org.jivesoftware.spark.plugin.Plugin)

Example 4 with Plugin

use of org.jivesoftware.spark.plugin.Plugin 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);
    }
}
Also used : PluginDependency(org.jivesoftware.spark.plugin.PluginDependency) PublicPlugin(org.jivesoftware.spark.plugin.PublicPlugin) DocumentException(org.dom4j.DocumentException) MalformedURLException(java.net.MalformedURLException) PublicPlugin(org.jivesoftware.spark.plugin.PublicPlugin) Plugin(org.jivesoftware.spark.plugin.Plugin)

Aggregations

Plugin (org.jivesoftware.spark.plugin.Plugin)4 DocumentException (org.dom4j.DocumentException)3 PublicPlugin (org.jivesoftware.spark.plugin.PublicPlugin)3 MalformedURLException (java.net.MalformedURLException)2 Document (org.dom4j.Document)2 Element (org.dom4j.Element)2 SAXReader (org.dom4j.io.SAXReader)2 PluginDependency (org.jivesoftware.spark.plugin.PluginDependency)2 Color (java.awt.Color)1 JarFile (java.util.jar.JarFile)1 ZipFile (java.util.zip.ZipFile)1 JComboBox (javax.swing.JComboBox)1 Node (org.dom4j.Node)1 Message (org.jivesoftware.smack.packet.Message)1 ChatManager (org.jivesoftware.spark.ChatManager)1 SparkManager (org.jivesoftware.spark.SparkManager)1 ChatRoom (org.jivesoftware.spark.ui.ChatRoom)1 ChatRoomListenerAdapter (org.jivesoftware.spark.ui.ChatRoomListenerAdapter)1 MessageEventListener (org.jivesoftware.spark.ui.MessageEventListener)1 TranscriptWindow (org.jivesoftware.spark.ui.TranscriptWindow)1