测试发帖 本帖最后由 meichen8050753 于 2011-09-08 14:41:23 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 /** * 导入插件 */ public void add(String path) { try { JarFile jar = new JarFile(path); InputStream in =jar.getInputStream(jar.getEntry("com/chinasoft/exam/properties/plugin.xml")); DocumentBuilderFactory domfac=DocumentBuilderFactory.newInstance(); //加载插件信息 DocumentBuilder dombuilder = domfac.newDocumentBuilder(); Document doc = dombuilder.parse(in); // 获取要节点对象 Element root = doc.getDocumentElement(); NodeList nodeList = root.getChildNodes(); if(nodeList != null && infoSet.get(root.getAttribute("name"))== null) { //储存插件信息 for (int i = 0; i<nodeList.getLength(); i++) { HashMap<String, String> item = new HashMap<String, String>(); String key = null; String value = null; if (nodeList.item(i).getNodeType() == Node.ELEMENT_NODE && nodeList.item(i).getNodeName().equals("item")) { for (Node node = nodeList.item(i).getFirstChild(); node != null; node = node.getNextSibling()) { if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals("key")) { key = node.getFirstChild().getNodeValue(); } if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals("value")) { value = node.getFirstChild().getNodeValue(); } } if(key != null || value != null) { item.put(key, value); if(item.get(key) != null) { infoList.add(item); } } } else if (nodeList.item(i).getNodeType() == Node.ELEMENT_NODE && nodeList.item(i).getNodeName().equals("author")) { for (Node node = nodeList.item(i).getFirstChild(); node != null; node = node.getNextSibling()) { if (node.getNodeType() == Node.ELEMENT_NODE && !node.getNodeName().equals("#text")) { key = node.getNodeName(); value = node.getFirstChild().getNodeValue(); item.put(key, value); if(infoList.indexOf(item) <0 ) { infoList.add(item); } } } } } //保存插件信息 infoSet.put(root.getAttribute("name"), infoList); //加载插件调用方法的类 File file = new File(path); URL url = file.toURI().toURL(); URLClassLoader loader = new URLClassLoader(new URL[] { url }); Class classDefine = Class.forName( "com.huawei.MyPlugin", true, loader); pluginSet.put(root.getAttribute("name"), classDefine); System.out.println("\n\n插件导入成功"); } else { System.out.println("\n\n插件已存在。"); } } catch(Exception e) { System.out.println("\n\n文件不存在。"); } } /** * 显示插件信息 * @param name 插件名称 */ public void showInfo(String pluginName) { infoList = infoSet.get(pluginName); if(infoList != null) { System.out.println("\n\n" + pluginName + "插件的信息:"); for(HashMap<String, String> infomap : infoList) { Iterator i = infomap.keySet().iterator(); while(i.hasNext()){ Object o = i.next(); String key = o.toString(); //System.out.print(key +", "); System.out.println(key + ": " + infomap.get(key)); } } } else { System.out.println("\n\n插件不存在"); } } /** * 显示插件列表 */ public void showList() { if(!infoSet.isEmpty()) { Iterator i = infoSet.keySet().iterator(); System.out.println("\n\n插件列表为:"); while(i.hasNext()) { Object o = i.next(); String pluginName = o.toString(); System.out.println(pluginName); } } else { System.out.println("\n\n没有已经导入的插件"); } } /** * 移除插件 * @param name 插件名称 */ public void removePlugin(String pluginName) { infoList = infoSet.get(pluginName); if(infoList != null) { infoSet.remove(pluginName); System.out.println("\n\n插件移除成功"); } else { System.out.println("\n\n插件不存在"); } } /** * 调用引入的插件方法 * @param pluginName 插件名称 * @param parm 方法参数 */ public void callMethod(String pluginName ,String parm) { Class<?> pluginObject = pluginSet.get(pluginName); if(pluginObject != null) { Class[] paramTypes = new Class[] { String.class};// 参数类型数组 Object[] params = new Object[] {parm};// 参数值数组 try { Method method = pluginObject.getDeclaredMethod("call", paramTypes);// 获取方法实例 method.invoke(pluginObject.newInstance(), params);// 调用call方法并返回Plugin实例 } catch (Exception err) { err.printStackTrace(); } } else { System.out.println("\n\n插件不存在"); } }} CommandManager.java package com.mc.test;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.Scanner;public class CommandManager { private Plugin plugin; private boolean isExit = false; public CommandManager() { plugin = Plugin.getInstance(); } public void getCommand() { Scanner scanner = new Scanner(System.in); while(!isExit) { System.out.print(">>"); InputStreamReader inputStreamReader=new InputStreamReader(System.in); BufferedReader bufferedReader=new BufferedReader(inputStreamReader); try { this.handleCommand(bufferedReader.readLine()); }catch(IOException err) { System.out.print("本系统还很弱小,请不要乱按键"); } } } private void handleCommand(String command) { String[] commands = command.split(" "); String commandKey = commands[0]; if(commandKey.equals("add") && commands.length >= 2) { plugin.add(commands[1]); } else if(commandKey.equals("list")) { plugin.showList(); } else if(commandKey.equals("info") && commands.length >= 2) { plugin.showInfo(commands[1]); } else if(commandKey.equals("remove") && commands.length >= 2) { plugin.removePlugin(commands[1]); } else if(commandKey.equals("call") && commands.length >= 3) { plugin.callMethod(commands[1], commands[2]); } else { System.out.println("\n\n命令无法识别"); } } private void exitSystem() { this.isExit = true; System.out.print("\n\n欢迎再次使用本系统"); } public static void main(String[] args) { CommandManager commandManager = new CommandManager(); commandManager.getCommand(); }} com/chinasoft/exam/properties/plugin.xml管理系统插件??????????????? Syntax error on token "}", delete this token 肯定没有少}啊 关于读取指定url内容 swing 中 表格 显示数据库内容 借宝地问个问题 怎样构造一个JTree,根据数据库的内容构造!!在线!急. 求助!!!高手进~~~~~~~~ Math.round(Math.random() 大哥大姐帮帮忙 关于jdbc的连接问题 请问response.encodeURL("j_security_check")是什么意思? 简单的问题,字符串分解 HaspMap中的那个hash函数是干啥用的?
* 导入插件
*/
public void add(String path)
{
try
{
JarFile jar = new JarFile(path);
InputStream in =jar.getInputStream(jar.getEntry("com/chinasoft/exam/properties/plugin.xml"));
DocumentBuilderFactory domfac=DocumentBuilderFactory.newInstance();
//加载插件信息
DocumentBuilder dombuilder = domfac.newDocumentBuilder();
Document doc = dombuilder.parse(in);
// 获取要节点对象
Element root = doc.getDocumentElement();
NodeList nodeList = root.getChildNodes();
if(nodeList != null && infoSet.get(root.getAttribute("name"))== null)
{
//储存插件信息
for (int i = 0; i<nodeList.getLength(); i++)
{
HashMap<String, String> item = new HashMap<String, String>();
String key = null;
String value = null;
if (nodeList.item(i).getNodeType() == Node.ELEMENT_NODE && nodeList.item(i).getNodeName().equals("item"))
{
for (Node node = nodeList.item(i).getFirstChild(); node != null; node = node.getNextSibling())
{
if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals("key"))
{
key = node.getFirstChild().getNodeValue();
}
if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals("value"))
{
value = node.getFirstChild().getNodeValue();
}
}
if(key != null || value != null)
{
item.put(key, value);
if(item.get(key) != null)
{
infoList.add(item);
}
}
}
else if (nodeList.item(i).getNodeType() == Node.ELEMENT_NODE && nodeList.item(i).getNodeName().equals("author"))
{
for (Node node = nodeList.item(i).getFirstChild(); node != null; node = node.getNextSibling())
{
if (node.getNodeType() == Node.ELEMENT_NODE && !node.getNodeName().equals("#text"))
{
key = node.getNodeName();
value = node.getFirstChild().getNodeValue();
item.put(key, value);
if(infoList.indexOf(item) <0 )
{
infoList.add(item);
}
}
}
}
}
//保存插件信息
infoSet.put(root.getAttribute("name"), infoList);
//加载插件调用方法的类
File file = new File(path);
URL url = file.toURI().toURL();
URLClassLoader loader = new URLClassLoader(new URL[] { url });
Class classDefine = Class.forName( "com.huawei.MyPlugin", true, loader);
pluginSet.put(root.getAttribute("name"), classDefine);
System.out.println("\n\n插件导入成功");
}
else
{
System.out.println("\n\n插件已存在。");
}
}
catch(Exception e)
{
System.out.println("\n\n文件不存在。");
}
}
* 显示插件信息
* @param name 插件名称
*/
public void showInfo(String pluginName)
{
infoList = infoSet.get(pluginName);
if(infoList != null)
{
System.out.println("\n\n" + pluginName + "插件的信息:");
for(HashMap<String, String> infomap : infoList)
{
Iterator i = infomap.keySet().iterator();
while(i.hasNext()){
Object o = i.next();
String key = o.toString();
//System.out.print(key +", ");
System.out.println(key + ": " + infomap.get(key));
}
}
}
else
{
System.out.println("\n\n插件不存在");
}
}
/**
* 显示插件列表
*/
public void showList()
{
if(!infoSet.isEmpty())
{
Iterator i = infoSet.keySet().iterator();
System.out.println("\n\n插件列表为:");
while(i.hasNext())
{
Object o = i.next();
String pluginName = o.toString();
System.out.println(pluginName);
}
}
else
{
System.out.println("\n\n没有已经导入的插件");
}
}
/**
* 移除插件
* @param name 插件名称
*/
public void removePlugin(String pluginName)
{
infoList = infoSet.get(pluginName);
if(infoList != null)
{
infoSet.remove(pluginName);
System.out.println("\n\n插件移除成功");
}
else
{
System.out.println("\n\n插件不存在");
}
}
/**
* 调用引入的插件方法
* @param pluginName 插件名称
* @param parm 方法参数
*/
public void callMethod(String pluginName ,String parm)
{
Class<?> pluginObject = pluginSet.get(pluginName);
if(pluginObject != null)
{
Class[] paramTypes = new Class[] { String.class};// 参数类型数组
Object[] params = new Object[] {parm};// 参数值数组
try
{
Method method = pluginObject.getDeclaredMethod("call", paramTypes);// 获取方法实例
method.invoke(pluginObject.newInstance(), params);// 调用call方法并返回Plugin实例
}
catch (Exception err)
{
err.printStackTrace();
}
}
else
{
System.out.println("\n\n插件不存在");
}
}
}
package com.mc.test;import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;public class CommandManager
{
private Plugin plugin;
private boolean isExit = false;
public CommandManager()
{
plugin = Plugin.getInstance();
}
public void getCommand()
{
Scanner scanner = new Scanner(System.in);
while(!isExit)
{
System.out.print(">>");
InputStreamReader inputStreamReader=new InputStreamReader(System.in);
BufferedReader bufferedReader=new BufferedReader(inputStreamReader);
try
{
this.handleCommand(bufferedReader.readLine());
}catch(IOException err)
{
System.out.print("本系统还很弱小,请不要乱按键");
}
}
}
private void handleCommand(String command)
{
String[] commands = command.split(" ");
String commandKey = commands[0];
if(commandKey.equals("add") && commands.length >= 2)
{
plugin.add(commands[1]);
}
else if(commandKey.equals("list"))
{
plugin.showList();
}
else if(commandKey.equals("info") && commands.length >= 2)
{
plugin.showInfo(commands[1]);
}
else if(commandKey.equals("remove") && commands.length >= 2)
{
plugin.removePlugin(commands[1]);
}
else if(commandKey.equals("call") && commands.length >= 3)
{
plugin.callMethod(commands[1], commands[2]);
}
else
{
System.out.println("\n\n命令无法识别");
}
}
private void exitSystem()
{
this.isExit = true;
System.out.print("\n\n欢迎再次使用本系统");
}
public static void main(String[] args)
{
CommandManager commandManager = new CommandManager();
commandManager.getCommand();
}
}
管理系统插件???????????????