⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pluginmanager.java

📁 基于jxta的局域网P2P文件共享,可以实现局域网中的文件p2p共享,实现文件快速传输及交流
💻 JAVA
字号:
/**
 * -- Copyright (C) 2006 Hisham Khalil. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
 *
 * Author: Hisham Khalil <hishberlin@hotmail.com>
 */


package connex.session.plugin;

import java.net.*;
import org.w3c.dom.Document;

import javax.xml.parsers.DocumentBuilderFactory;

import java.io.*;

import java.util.jar.*;
import java.util.zip.*;
import org.xml.sax.SAXException;
import java.util.Collection;
import java.util.Enumeration;
import javax.swing.ImageIcon;
import java.awt.Image;
import org.w3c.dom.NodeList;
import org.w3c.dom.Element;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;

public class PluginManager {

    private static PluginManager instance;

    /**
     * @directed
     * @link aggregationByValue
     * @supplierCardinality 1
     * @clientCardinality 1
     */
    private PluginRegistry registry = new PluginRegistry();
    private URLClassLoader classLoader;

    /**
     * @directed
     */
private  PluginDescriptor pd;

    private PluginManager() {
        instance = this;
    }

    /**
     * it takes as parameter an id representing a pluginEntryId
     * and returns an instance of that PluginEntry
     * @param id Integer
     * @return PluginEntry
     */
    public Plugin loadConcretePlugin(String id) {
        Plugin plugin = registry.getPlugin(id);
        if (plugin != null) {
            return plugin;
        }
        PluginDescriptor pd = registry.getDescriptor(id);
        plugin = null;
        try {

            Object o = null;
            try {
                plugin = (Plugin) classLoader.loadClass(pd.getMainClass()).newInstance();
            } catch (ClassNotFoundException ex) {
                System.out.println("ClassNotFoundException");
            } catch (IllegalAccessException ex) {
            } catch (InstantiationException ex) {
            }

            registry.registerPlugin(pd.getId(), plugin);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return (Plugin) plugin;
    }

    public Collection getDescriptors() {
        return registry.getAllDescriptors();
    }

    public void unloadPlugin(String id) {
        registry.removePlugin(id);
    }

    public void unloadPlugins() {
        registry.clear();

    }


    public void loadPlugins(String pluginsPath) {

        try {
            File[] jars = (new File(pluginsPath)).listFiles(new
                    pluginFileFilter());
            if (jars.length == 0) {
                throw new Exception("no Plugin found");

            }

            URL[] url = new URL[jars.length];
            for (int i = 0; i < jars.length; i++) {

                url[i] = jars[i].toURL();

            }
            classLoader = new URLClassLoader(url);

            System.out.println("found Plugins : " + jars.length);

            for (int i = 0; i < jars.length; i++) {
                System.out.print("Loading...");

                System.out.println(jars[i].getName());

                Document doc = this.getPluginDescriptorDocument(jars[i]);
                if (doc != null) {
                    NodeList nodeList = null;
                    try {

                        nodeList = doc.getElementsByTagName("plugin");
                    } catch (Exception e) {
                        System.out.println(
                                "Invalid Discriptor: There are not <plugin> tags in descriptor.xml!");
                    }
                    if (nodeList.getLength() == 0) {
                        System.out.println(
                                "Invalid Discriptor: There are not <plugin> tags in descriptor.xml!");
                    }


                        Element entrypoint = (Element) nodeList.item(0);
                        if (!registry.isRegistered(entrypoint.getElementsByTagName("ID").
                                item(0).getFirstChild().getNodeValue())) {

                             pd = new PluginDescriptor();

                            try {
                                pd.setId(entrypoint.getElementsByTagName("ID").
                                         item(0).getFirstChild().getNodeValue());

                            } catch (Exception e) {
                                System.out.println(
                                        " Invalid Discriptor: <id> tag not found");

                                break;
                            }
                            try {
                                pd.setName(entrypoint.getElementsByTagName("name").
                                           item(0).getFirstChild().getNodeValue());
                            } catch (Exception e) {
                                System.out.println(
                                        "  Invalid Discriptor: <name> tag not found");

                                break;
                            }
                            try {
                                pd.setVersion(entrypoint.getElementsByTagName(
                                        "version").
                                              item(0).getFirstChild().getNodeValue());
                            } catch (Exception e) {
                                System.out.println("    <version> tag not Found...");
                            }

                            try {
                                pd.setType(entrypoint.getElementsByTagName("type").
                                           item(0).getFirstChild().getNodeValue());
                            } catch (Exception e) {
                                System.out.println(
                                        " Invalid Discriptor: <type> tag not found");

                                break;
                            }

                            try {
                                pd.setMainClass(entrypoint.getElementsByTagName("class").
                                                item(0).getFirstChild().
                                                getNodeValue());
                            } catch (Exception e) {
                                System.out.println(
                                        "    Invalid Discriptor: <class> tag not found");
                                break;
                            }
                            try {

                                pd.setIcon(this.getIcon(jars[i], entrypoint.getElementsByTagName("icon").
                                           item(0).getFirstChild().getNodeValue()));
                            } catch (Exception e) {
                                System.out.println("    <icon> tag not Found...");
                            }

                            System.out.println("    ID:" + pd.getId());
                            System.out.println("    Name:" + pd.getName());
                            System.out.println("    Version:" + pd.getVersion());
                            System.out.println("    Type:" + pd.getType());
                            System.out.println("    Class:" + pd.getMainClass());
                            System.out.println("    Icon:" + pd.getIcon());

                            System.out.println("");

                            registry.registerDescriptor(pd.getId(), pd);
                        }

                } else {
                    System.out.println(jars[i] + " does not contain descriptor.xml ");
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     *Returns a Dom Document object of descriptor.xml
     *null if it does not exist.
     */
    private Document getPluginDescriptorDocument(File file) {

        JarFile jarFile = null;
        ZipEntry discriptor = null;
        Document doc = null;
        try {

            jarFile = new JarFile(file);
            discriptor = jarFile.getEntry("descriptor.xml");

            if (discriptor != null) {
                InputStream is = jarFile.getInputStream(discriptor);
                DocumentBuilderFactory dbf = DocumentBuilderFactory.
                                             newInstance();
                DocumentBuilder db = null;
                try {
                    db = dbf.newDocumentBuilder();
                    doc = db.parse(is);

                } catch (ParserConfigurationException ex1) {
                } catch (IOException ex) {
                } catch (SAXException ex) {}
            }

            return doc;

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (jarFile != null) {
                try {
                    jarFile.close();
                } catch (IOException ioe) {}
            }
        }
        return null;
    }

    private ImageIcon getIcon(File file, String name) {
        JarFile jarFile = null;
        ZipEntry icon = null;
        ImageIcon doc = null;
        byte[] buffer = null;
        try {

            jarFile = new JarFile(file);
           icon = jarFile.getEntry(name);
           InputStream is = jarFile.getInputStream(icon);

           buffer=new byte[is.available()];
           is.read(buffer);

           doc= new ImageIcon(buffer);
           is.close();
           return doc;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (jarFile != null) {
                try {
                    jarFile.close();
                } catch (IOException ioe) {}
            }
        }
        return null;
    }

    class pluginFileFilter implements java.io.FilenameFilter {
        public boolean accept(File dir, String filename) {
            if (filename.endsWith(".jar")) {
                return true;
            }
            return false;
        }
    }


    public static PluginManager getInstance() {
        if (instance == null) {
            return instance = new PluginManager();
        }
        return instance;
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -