?? pluginmanager.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 + -